-1

My code is :

#include<stdio.h>
int main()
{
    char c='8';
    int d=8;
    printf("%d %d %d",d,d+=c>='0'&&c<='9',c++);
    return(0);
}

The output of this question is : 9 9 56. I'm unable to understand this. Please somebody help me and please explain me the output. Thanks.

Gaurang Tandon
  • 6,504
  • 11
  • 47
  • 84

3 Answers3

5

You are observing undefined behaviour. d is passed as an argument twice, and once with side effects. If done in sequence your code should be equivalent to

char c='8';
int d=8;
printf("%d",d);

d+= (c>='0)' && (c<='9');
printf(" %d", d);
printf(" %d",c);
c++;

But since it is undefined in what order the arguments are computed when passing them with side effects to a function you are seeing here:

char c='8';
int d=8;

d+= (c>='0)' && (c<='9');
printf("%d",d);
printf(" %d", d);
printf(" %d",c);
c++;
Sergey L.
  • 21,822
  • 5
  • 49
  • 75
  • Is it undefined? The [list of sequence points](http://stackoverflow.com/questions/3575350/sequence-points-in-c) seems to suggest arguments are evaluated *before* the function call. – Gaminic Nov 22 '13 at 14:10
  • 2
    @Gaminic the order in which parameters evaluated isn't defined – Deepthought Nov 22 '13 at 14:16
  • Yes, but in this case, the "odd print" is the regular 'd'. Given that all parameters are evaluated before the actual call, the value of 'd' is entirely predictable/defined, isn't it? – Gaminic Nov 25 '13 at 15:19
2

Basically printf reads the argument from right to left so the first print would be of

 " printf("%d %d %d",d,d+=c>='0'&&c<='9',c++);  " 

 -  1st output  = c++ -> which is nothing but ascii value of '8' = 56 
   and then c is incremented by 1 . 

 - next is the d -> it boils down to d = d + if(c>='0'&&c<='9') ... 
     so d = d+1  so  9 here

 - next d again so it is again  Hope it is clear !
Srikanth
  • 447
  • 2
  • 8
  • -1 , *printf reads the argument from right to left* is not correct, http://stackoverflow.com/a/9566538/2327831 – this Nov 22 '13 at 14:26
  • Ok , I correct my statment . The output may vary from compiler to compiler . But in this specific scenario it was reading from right-to-left and hence we could see this behavior . On other day in another compiler it might be completely different . – Srikanth Nov 22 '13 at 14:33
2

Ok, the first thing you have to know is, a char is stored in the memory as Ascii Table. So in the memory the char c will have a integer value of '8'. From the table we know that the integer value of '8' is 56. According to the table we'll get:

'8': 56

'0': 48

'9': 57

So let's get started.

c++: It is a statement which adds c by one and return the value of current c.

Example:

int a,c;
c=1;
a=c++;
printf("a=%d,c=%d",a,c);

The result of this code is "a=1,c=2"

so %d of c++ is still 56.

d+=c>'0'&&c<='9': According to C's priority this statement will be like:

d+=(c>'0'&&c<='9')

So let's start with c>'0'&&c<='9' first. It is a condition statement. Is c's Ascii value great or equal to 0's Ascii value AND less or equal to 9's Ascii value?( Notice c's Ascii is 56 or 57 now because the evaluation order of printf is undefined. So it will be 56 if this statement is evaluated before c++ or 57 if after c++. But both way, c<='9' is true ) YES. So the statement is true. In C the TRUE is 1.

So d+=c>'0'&&c<='9' will be d+=1 which means d=d+1. So %d of d is 9.

So the result is "9 9 56"

Archeosudoerus
  • 1,101
  • 9
  • 24
  • 1
    "the printf is be evaluated from right to left": the order in which parameters evaluated isn't defined http://stackoverflow.com/questions/376278/parameter-evaluation-order-before-a-function-calling-in-c – Deepthought Nov 22 '13 at 14:18
  • @Deepthought didn't notice that. I'll change my answer. Thanks. – Archeosudoerus Nov 22 '13 at 14:19
  • Please explain this : "c++: It means add c and 1 and store the result in a temp variable. After this statement copy the result form the temp value to c. So inside the current statement, the c value remain unchanged. so %d of c++ is still 56." – Gaurang Tandon Nov 22 '13 at 14:21
  • @GaurangTandon sorry my first explaination is not accurate. the temp variable will store the current value of c and c is added by one. But the temp value will be the return value of the `++` statement. This is the true thing behind screen. I have changed my answer. Sorry – Archeosudoerus Nov 22 '13 at 14:29
  • @zyc , it's okay :-). I got the answer – Gaurang Tandon Nov 22 '13 at 14:31