Forum     

Go Back   Digit Technology Discussion Forum > Software > Software Q&A
Register FAQ Calendar Mark Forums Read

Software Q&A Having trouble with software? Find solutions here


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 22-08-2007, 04:55 PM   #1 (permalink)
In The Zone
 
Join Date: May 2004
Location: aapnu ahmedabad...
Posts: 205
Default 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
manmay is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 22-08-2007, 06:48 PM   #2 (permalink)
God of Mistakes...
 
Garbage's Avatar
 
Join Date: Dec 2005
Location: Pune, Maharashtra
Posts: 1,924
Default 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 ???
__________________
Registered Linux User #468778
----------------------------------
http://twitter.com/_Garbage_
Garbage is online now  
Old 22-08-2007, 08:33 PM   #3 (permalink)
"The RaCaLaNGeL"©
 
romeo_8693's Avatar
 
Join Date: Dec 2005
Location: Goa/Pune
Posts: 389
Default 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!
romeo_8693 is offline  
Old 22-08-2007, 10:06 PM   #4 (permalink)
Hey here is the aks
 
arunks's Avatar
 
Join Date: Jan 2006
Location: punjab
Posts: 805
Default 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
arunks is offline  
Old 22-08-2007, 10:16 PM   #5 (permalink)
The Lord of Death
 
Yamaraj's Avatar
 
Join Date: May 2005
Location: यमलोक
Posts: 253
Default 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.
Yamaraj is offline  
Old 22-08-2007, 10:16 PM   #6 (permalink)
God of Mistakes...
 
Garbage's Avatar
 
Join Date: Dec 2005
Location: Pune, Maharashtra
Posts: 1,924
Default 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 !!!
__________________
Registered Linux User #468778
----------------------------------
http://twitter.com/_Garbage_
Garbage is online now  
Old 22-08-2007, 10:25 PM   #7 (permalink)
The Lord of Death
 
Yamaraj's Avatar
 
Join Date: May 2005
Location: यमलोक
Posts: 253
Default 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.
Yamaraj is offline  
Old 22-08-2007, 10:29 PM   #8 (permalink)
Hey here is the aks
 
arunks's Avatar
 
Join Date: Jan 2006
Location: punjab
Posts: 805
Default Re: Explain the anomaly in the c prog

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
__________________
yours truly
arun
The aks!!

have a nice day
arunks is offline  
Old 22-08-2007, 10:34 PM   #9 (permalink)
The Lord of Death
 
Yamaraj's Avatar
 
Join Date: May 2005
Location: यमलोक
Posts: 253
Default 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.
Yamaraj is offline  
Old 22-08-2007, 10:48 PM   #10 (permalink)
Hey here is the aks
 
arunks's Avatar
 
Join Date: Jan 2006
Location: punjab
Posts: 805
Default 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
arunks is offline  
Old 22-08-2007, 11:18 PM   #11 (permalink)
D i s t i n c t l y Ahead
 
saurabh kakkar's Avatar
 
Join Date: Sep 2006
Location: New delhi
Posts: 495
Smile 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

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
saurabh kakkar is offline  
Old 22-08-2007, 11:26 PM   #12 (permalink)
The Lord of Death
 
Yamaraj's Avatar
 
Join Date: May 2005
Location: यमलोक
Posts: 253
Default 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.
Yamaraj is offline  
Old 22-08-2007, 11:54 PM   #13 (permalink)
D i s t i n c t l y Ahead
 
saurabh kakkar's Avatar
 
Join Date: Sep 2006
Location: New delhi
Posts: 495
Default 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.
saurabh kakkar is offline  
Old 23-08-2007, 01:03 AM   #14 (permalink)
Wahahaha~!
 
Faun's Avatar
 
Join Date: Dec 2006
Location: Pune/there
Posts: 7,686
Default 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.
__________________
Blog | Flickr | Battlelog
Spoiler:
Asus Z68 V-Pro|i5 2500k|TRUE Black|Ripjaws X|U2311H|N560GTX|D7000|XONAR STX|RE272|RE0|CC51|XE200PRO Walnut| TD II V2| Ultraphile|N5800

Mono
Faun is online now  
Old 24-08-2007, 04:24 PM   #15 (permalink)
In The Zone
 
Join Date: May 2004
Location: aapnu ahmedabad...
Posts: 205
Default 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
manmay is offline  
Old 25-08-2007, 03:37 PM   #16 (permalink)
Ron
||uLtiMaTE WinNER||
 
Ron's Avatar
 
Join Date: Nov 2006
Location: Kathmandu,Nepal
Posts: 698
Default Re: Explain the anomaly in the c prog

Guys wht is the difference between c and c++
__________________
||uLtiMaTE WinNER||
Ron is offline  
Old 25-08-2007, 07:49 PM   #17 (permalink)
God of Mistakes...
 
Garbage's Avatar
 
Join Date: Dec 2005
Location: Pune, Maharashtra
Posts: 1,924
Default 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
__________________
Registered Linux User #468778
----------------------------------
http://twitter.com/_Garbage_
Garbage is online now  
Old 25-08-2007, 07:56 PM   #18 (permalink)
left this forum longback
 
praka123's Avatar
 
Join Date: Sep 2005
Location: -
Posts: 7,536
Default Re: Explain the anomaly in the c prog

heard objective C?
__________________
left this forum long back.Admin Can Delete this Account and posts Permanantly.Thank You
Get GNU/Linux - http://getgnulinux.org
praka123 is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
C prog-Fact of 1,00,000 Pragadheesh QnA (read only) 5 04-10-2006 05:31 PM
running c++ prog in linux shivi4 Open Source 4 01-07-2006 08:16 PM
telnet & socket prog.. sridhar8310 QnA (read only) 4 20-05-2005 11:32 PM
Recording TV prog using PC SmoothCriminal QnA (read only) 7 17-10-2004 11:54 AM

 
Latest Threads
- by chris
- by icebags
- by Tenida
- by Who

Advertisement




All times are GMT +5.5. The time now is 12:05 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2