Explain the anomaly in the c prog

Status
Not open for further replies.

manmay

Journeyman
#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

God of Mistakes...
^^ 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 ???
 

Yamaraj

The Lord of Death
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.
 

Yamaraj

The Lord of Death
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

Hey here is the aks
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

The Lord of Death
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

Hey here is the aks
ok if u say that i note it... actually i run it into turbo c++ compiler thats why i was saying that
 

saurabh kakkar

D i s t i n c t l y Ahead
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

The Lord of Death
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".
 
Last edited:

saurabh kakkar

D i s t i n c t l y Ahead
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
 
Last edited:

Faun

Wahahaha~!
Staff member
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.
 
OP
M

manmay

Journeyman
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
 

Garbage

God of Mistakes...
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
 
Status
Not open for further replies.
Top Bottom