why o/p like this ?

S

Sweety

#include<stdio.h>


void main()
{
int a=0,b=6,c=7,d=8,e;
e = a ? c , b : c , d;
printf("%d",e);
}
o/p->7
 
M

Martin Ambuhl

Sweety wrote:

[crap indicating that "Sweety" never checks a FAQ, never checks past
traffic in a news group, never has read a non-Schildt textbook on C; or
else is just trolling.]

Go away.
 
T

Till Crueger

Sweety wrote:

[crap indicating that "Sweety" never checks a FAQ, never checks past
traffic in a news group, never has read a non-Schildt textbook on C; or
else is just trolling.]

I don't quite get the output of his programm either. I changed it a bit,
and got similar output I don't quite understand.
I did the following

#include <stdio.h>

int main(void) {
int e;
e= 0 ? 1,2 : 3,4;
printf("%d", e);
return 0;
}

I get the output 3, however I think it should be for, no matter in which
order you evaluate. I also did the following:

#include <stdio.h>

int main(void) {
int e;
e= (0 ? 1,2 : 3),4;
printf("%d", e);
return 0;
}

// Output: 3

and:

#include <stdio.h>

int main(void) {
int e;
e= 0 ? (1,2) : (3,4);
printf("%d", e);
return 0;
}

// output 4

and even:

#include <stdio.h>

int main(void) {
int e;
(e= 0 ? 1,2 : 3),4;
printf("%d", e);
return 0;
}

// Output 3

Is it correct that the comma operator takes even less precedence than the
= operator?
Till
 
T

Tak-Shing Chan

I don't quite get the output of his programm either. I changed it a bit,
and got similar output I don't quite understand.
I did the following

#include <stdio.h>

int main(void) {
int e;
e= 0 ? 1,2 : 3,4;
printf("%d", e);
return 0;
}

I get the output 3, however I think it should be for, no matter in which
order you evaluate. I also did the following:

[snip]

Is it correct that the comma operator takes even less precedence than the
= operator?

According to the C syntax, the above expression should be
parsed as follows:

(1) expression:
expression , assignment-expr
e= 0 ? 1,2 : 3 , 4

(2) expression:
assignment-expr:
unary-expr assignment-operator assignment-expr
e = 0 ? 1,2 : 3

(3) assignment-expr:
conditional-expr:
logical-OR-expr ? expression : conditional-expr
0 ? 1,2 : 3

(4) expression:
expression , assignment-expr
1 , 2

Therefore the output 3 is correct.

Tak-Shing
 
R

Ralmin

Till Crueger said:
I don't quite get the output of his programm either. I changed
it a bit, and got similar output I don't quite understand.
I did the following

#include <stdio.h>

int main(void) {
int e;
e= 0 ? 1,2 : 3,4;
printf("%d", e);
return 0;
}

I get the output 3, however I think it should be for, no
matter in which order you evaluate.

The assignment operator has higher precedence than the comma operator. The
line is parsed like this:
(e = 0 ? 1,2 : 3), 4;
That is, first e is assigned the value 3, then the statement has the value
4.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top