943,940 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1216
  • C RSS
Dec 11th, 2005
0

i can't figure out this code :( help please

Expand Post »
Hello im practicing C language
and i ran into this code and i can't figure out the outputs
can u help me please ?
I'm confused in operator precedence.
and What does short circuit evaluation means in c?

when i run this code the output is:
000 and 001
How come this way ? please share ur knowledge with me .

  1.  
  2. #include<stdio.h>
  3.  
  4. void main()
  5. {
  6. int a = 0, b = 0, x;
  7.  
  8. x = 0 && (a = b = 777);
  9. printf("%d %d %d\n", a, b, x);
  10.  
  11. x = 777 || (a = ++b);
  12. printf("%d %d %d\n", a, b, x);
  13. }

Thanks much...
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
free_eagle is offline Offline
41 posts
since Jan 2005
Dec 11th, 2005
0

Re: i can't figure out this code :( help please

Don't use void main().

Quote ...
and What does short circuit evaluation means in c?
  1. if(a() && b())
In this case, if a is false, b won't be evaluated, because no matter what b is, the expression is false.
Reputation Points: 185
Solved Threads: 28
Posting Whiz in Training
dwks is offline Offline
269 posts
since Nov 2005
Dec 11th, 2005
0

Re: i can't figure out this code :( help please

hi DWKS
thanks for short clear explanation.

So u in this case when we use logical operations we don't assign numbers but we think it like true and false right and we can think of "if" statement to be considered?!

aah yes i understood that i didn't think about condition i thought may be we assign 777 to a and b

but look this operators;

"=" and "==" x = 0 && (a = b = 777)

i though as the operator is assignment it will give 0 to x and ... umm okay i'll try to find out...

THANK YOU!!!
Reputation Points: 10
Solved Threads: 0
Light Poster
free_eagle is offline Offline
41 posts
since Jan 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Socket communication and event handling <C>
Next Thread in C Forum Timeline: data sorts in reverse order!! help!!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC