 |
22-08-2007, 04:55 PM
|
#1 (permalink)
|
|
In The Zone
Join Date: May 2004
Location: aapnu ahmedabad...
Posts: 205
|
Explain the anomaly in the c prog
#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
|
|
|
|
Advertisements. Register and be a member of the community to get rid of them.
|
|
Advertisement
|
|
22-08-2007, 06:48 PM
|
#2 (permalink)
|
|
God of Mistakes...
Join Date: Dec 2005
Location: Pune, Maharashtra
Posts: 1,924
|
Re: Explain the anomaly in the c prog
^^ 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",11 + 10);
Am I right ???
|
|
|
22-08-2007, 08:33 PM
|
#3 (permalink)
|
|
"The RaCaLaNGeL"©
Join Date: Dec 2005
Location: Goa/Pune
Posts: 389
|
Re: Explain the anomaly in the c prog
@shirish....[quote]& output ...
Code:
Printing from Expression : 21
This should be 20./[quote]
dude after k++,k=11;
then k-- =11-1;
so 10+11=21.....
__________________
Without sorrows happiness would be boring!
|
|
|
22-08-2007, 10:06 PM
|
#4 (permalink)
|
|
Hey here is the aks
Join Date: Jan 2006
Location: punjab
Posts: 805
|
Re: Explain the anomaly in the c prog
all outputs are correct
what is the anomaly
__________________
yours truly
arun
The aks!!
have a nice day
Last edited by arunks; 22-08-2007 at 10:06 PM.
Reason: Automerged Doublepost
|
|
|
22-08-2007, 10:16 PM
|
#5 (permalink)
|
|
The Lord of Death
Join Date: May 2005
Location: यमलोक
Posts: 253
|
Re: Explain the anomaly in the c prog
Quote:
|
Originally Posted by manmay
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.
|
|
|
22-08-2007, 10:16 PM
|
#6 (permalink)
|
|
God of Mistakes...
Join Date: Dec 2005
Location: Pune, Maharashtra
Posts: 1,924
|
Re: Explain the anomaly in the c prog
Quote:
|
Originally Posted by romeo_8693
dude after k++,k=11;
then k-- =11-1;
so 10+11=21.....
|
Thats what I've said !!!
|
|
|
22-08-2007, 10:25 PM
|
#7 (permalink)
|
|
The Lord of Death
Join Date: May 2005
Location: यमलोक
Posts: 253
|
Re: Explain the anomaly in the c prog
Quote:
|
Originally Posted by shirish_nagar
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",11 + 10);
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.
|
|
|
22-08-2007, 10:29 PM
|
#8 (permalink)
|
|
Hey here is the aks
Join Date: Jan 2006
Location: punjab
Posts: 805
|
Re: Explain the anomaly in the c prog
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
__________________
yours truly
arun
The aks!!
have a nice day
|
|
|
22-08-2007, 10:34 PM
|
#9 (permalink)
|
|
The Lord of Death
Join Date: May 2005
Location: यमलोक
Posts: 253
|
Re: Explain the anomaly in the c prog
Quote:
|
Originally Posted by arunks
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.
|
|
|
22-08-2007, 10:48 PM
|
#10 (permalink)
|
|
Hey here is the aks
Join Date: Jan 2006
Location: punjab
Posts: 805
|
Re: Explain the anomaly in the c prog
ok if u say that i note it... actually i run it into turbo c++ compiler thats why i was saying that
__________________
yours truly
arun
The aks!!
have a nice day
|
|
|
22-08-2007, 11:18 PM
|
#11 (permalink)
|
|
D i s t i n c t l y Ahead
Join Date: Sep 2006
Location: New delhi
Posts: 495
|
Re: Explain the anomaly in the c prog
Quote:
|
Originally Posted by manmay
#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
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
|
|
|
22-08-2007, 11:26 PM
|
#12 (permalink)
|
|
The Lord of Death
Join Date: May 2005
Location: यमलोक
Posts: 253
|
Re: Explain the anomaly in the c prog
Quote:
|
Originally Posted by saurabh kakkar
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 by Yamaraj; 22-08-2007 at 11:32 PM.
|
|
|
22-08-2007, 11:54 PM
|
#13 (permalink)
|
|
D i s t i n c t l y Ahead
Join Date: Sep 2006
Location: New delhi
Posts: 495
|
Re: Explain the anomaly in the c prog
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 by saurabh kakkar; 22-08-2007 at 11:59 PM.
|
|
|
23-08-2007, 01:03 AM
|
#14 (permalink)
|
|
Wahahaha~!
Join Date: Dec 2006
Location: Pune/there
Posts: 7,686
|
Re: Explain the anomaly in the c prog
Quote:
|
Originally Posted by Yamaraj
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.
|
|
|
24-08-2007, 04:24 PM
|
#15 (permalink)
|
|
In The Zone
Join Date: May 2004
Location: aapnu ahmedabad...
Posts: 205
|
Re: Explain the anomaly in the c prog
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
|
|
|
25-08-2007, 03:37 PM
|
#16 (permalink)
|
|
||uLtiMaTE WinNER||
Join Date: Nov 2006
Location: Kathmandu,Nepal
Posts: 698
|
Re: Explain the anomaly in the c prog
Guys wht is the difference between c and c++
__________________
||uLtiMaTE WinNER||
|
|
|
25-08-2007, 07:49 PM
|
#17 (permalink)
|
|
God of Mistakes...
Join Date: Dec 2005
Location: Pune, Maharashtra
Posts: 1,924
|
Re: Explain the anomaly in the c prog
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...
BTW, check THIS
And if not satisfied, then try this
|
|
|
25-08-2007, 07:56 PM
|
#18 (permalink)
|
|
left this forum longback
Join Date: Sep 2005
Location: -
Posts: 7,536
|
Re: Explain the anomaly in the c prog
__________________
left this forum long back.Admin Can Delete this Account and posts Permanantly.Thank You
Get GNU/Linux - http://getgnulinux.org
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|