DIGIT-ARC read-only archive · snapshot: July 23, 2026

Explain the anomaly in the c prog

18 posts · originally from https://geek.digit.in/community/threads/explain-the-anomaly-in-the-c-prog.66256/

M
manmay Original post #1
Aug 22, 2007 at 4:55 PM
#include<stdio.h>
#include<conio.h>
void main(){
clrscr();
int i,j,k;
i=10;
k=10;
j=i++ + i--;
printf("\nValue of i after expression : %d",i);
printf("\nPrinting from Variable : %d",j);
printf("\nPrinting from Expression : %d",k++ + k--);
printf("\nValue of k after expression : %d",k);
getch();
}

the output is:

Value of i after expression : 10
Printing from Variable : 20
Printing from Expression : 21
Value of k after expression : 10

i hope you have understood the question.


Thanks
Manmay
Garbage
Garbage #2
Aug 22, 2007 at 6:48 PM
^^ Firstly this "C" program won't compile bcoz of clrscr() called before variable declaration !

After changing their order, now, this sentence keep me wondering....
Code:
printf("\nPrinting from Expression : %d",k++ + k--);
& output ...
Code:
Printing from Expression : 21

This should be 20.

Now, I think that it is evaluated from left to right. As there is NO ASSIGNMENT operator, the value is immediately incremented & decremented.
So, it is replaced as..
Code:
printf("\nPrinting from Expression : %d",[b]11 + 10[/b]);

Am I right ???
arunks
arunks #4
Aug 22, 2007 at 10:06 PM
all outputs are correct

what is the anomaly
Yamaraj
Yamaraj #5
Aug 22, 2007 at 10:16 PM
manmay said:
j=i++ + i--;
...
printf("\nPrinting from Expression : %d",k++ + k--);
Statements or expressions like these two trigger an undefined behavior as specified by the ISO C standard.
Garbage
Garbage #6
Aug 22, 2007 at 10:16 PM
romeo_8693 said:
dude after k++,k=11;
then k-- =11-1;
so 10+11=21.....

Thats what I've said !!!:D
Yamaraj
Yamaraj #7
Aug 22, 2007 at 10:25 PM
shirish_nagar said:
This should be 20.

Now, I think that it is evaluated from left to right. As there is NO ASSIGNMENT operator, the value is immediately incremented & decremented.
So, it is replaced as..
Code:
printf("\nPrinting from Expression : %d",[b]11 + 10[/b]);

Am I right ???
No! C only guarantees that k will have a value incremented and decremented once, but only after the sequence point. That means the printf() statement can use either of the incremented or decremented, or the original value of k. The answer, therefore, can be 19, 20 or 21 depending on the compiler and the implementation.
arunks
arunks #8
Aug 22, 2007 at 10:29 PM
Code:
j=i++ + i--;
above statement assigns value of i i.e.10 to both i++ and i-- individually ..and both have post increment and decrement operators so it becomes 10+10=20

Code:
  printf("\nValue of i after expression : %d",i);
ur value of i not affected so it prints 10


Code:
  printf("\nPrinting from Variable : %d",j);
j has 20 so it prints 20

Code:
  printf("\nPrinting from Expression : %d",k++ + k--);

in this statement first k's value is used i.e. 10 then increment to 11 then added to k-- .. means 10+11 and so prints 21


Code:
printf("\nValue of k after expression : %d",k);
k's value has not been changed anywhere so it prints 10 here
Yamaraj
Yamaraj #9
Aug 22, 2007 at 10:34 PM
arunks said:
Code:
 printf("\nPrinting from Expression : %d",k++ + k--);

in this statement first k's value is used i.e. 10 then increment to 11 then added to k-- .. means 10+11 and so prints 21
O'reilly? Did you even test your explanation before posting it on the net? I get 20 with both GCC and VC++ compilers.
arunks
arunks #10
Aug 22, 2007 at 10:48 PM
ok if u say that i note it... actually i run it into turbo c++ compiler thats why i was saying that
saurabh kakkar
Aug 22, 2007 at 11:18 PM
manmay said:
#include<stdio.h>
#include<conio.h>
void main(){
clrscr();
int i,j,k;
i=10;
k=10;
j=i++ + i--;
printf("\nValue of i after expression : %d",i);
printf("\nPrinting from Variable : %d",j);
printf("\nPrinting from Expression : %d",k++ + k--);
printf("\nValue of k after expression : %d",k);
getch();
}

the output is:

Value of i after expression : 10
Printing from Variable : 20
Printing from Expression : 21
Value of k after expression : 10

i hope you have understood the question.


Thanks
Manmay

i have executed ur prog. in TC and found it to be correct and after scanning

through Let Us C i can tell u Y this is happenning :D

Code:
j=i++ + i--;
execution from left to right

refer page 32-36 of Let us C

j=11+9;
j=20;

Code:
printf("\nPrinting from Expression : %d",k++ + k--);
OR
Code:
printf("\nPrinting from Expression : %d",k++ + k);

BOTH WILL GIVE SAME OUTPUT SHOWING THAT K-- (decrement operator)
HAVE NO ROLE TO PLAY


Now execution will take from right to left

refer to page 172 of Let us C

now output will be like this

output=11+10=21 :)
Yamaraj
Yamaraj #12
Aug 22, 2007 at 11:26 PM
saurabh kakkar said:
BOTH WILL GIVE SAME OUTPUT SHOWING THAT K-- (decrement operator)
HAVE NO ROLE TO PLAY


Now execution will take from right to left

refer to page 172 of Let us C

now output will be like this

output=11+10=21 :)
If your feet stand firmly on the ground after a flight of self-appreciation, I suggest you throw out that piece-of-shite otherwise known as "Let Us C".
saurabh kakkar
Aug 22, 2007 at 11:54 PM
thanks for ur suggestion :) but i respect ANY BOOK of ANY author to the fullest and cant even

think of what u say throwing it
Faun
Faun #14
Aug 23, 2007 at 1:03 AM
Yamaraj said:
Statements or expressions like these two trigger an undefined behavior as specified by the ISO C standard.

yeah its the only explanation, different compiler different output.
M
manmay #15
Aug 24, 2007 at 4:24 PM
well i've to agree with yamaraj....as this prog is giving different outputs for diff compilers...i tried this one in gcc java and tc and vc++....so this is compiler dependent......

but still you cant throw "let us c"....its not that bad of a book.....
i agree that it jus covers the fundamental aspects of c.....but even after so many years sometimes i cant avoid getting back to it for something or the other.....

@ saurabh kakkar
this was the explaination that came up in my head the first time....but after some heavy discussions with my friends, and few trials with other compilers....i got a bit confused and hence the post.....

well anyway....thanks for your views guys....

bye
Ron
Ron #16
Aug 25, 2007 at 3:37 PM
Guys wht is the difference between c and c++
Garbage
Garbage #17
Aug 25, 2007 at 7:49 PM
ohh **** !!!

okkk... In short, C is procedure oriented language & C is (not fully but..) Object Oriented Language.

Now, u might ask What is mean by Procedure Oriented & Object Oriented !!!
huh... ask someone else please... :D

BTW, check THIS
And if not satisfied, then try this
praka123
praka123 #18
Aug 25, 2007 at 7:56 PM
heard objective C? :-|