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

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2005
Posts: 41
Reputation: free_eagle is an unknown quantity at this point 
Solved Threads: 0
free_eagle's Avatar
free_eagle free_eagle is offline Offline
Light Poster

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

 
0
  #1
Dec 11th, 2005
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...
Learn to forgive ,answer with kindness to evel...
Live happily and love sincerely...
[/COLOR]
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 251
Reputation: dwks has a spectacular aura about dwks has a spectacular aura about 
Solved Threads: 25
dwks's Avatar
dwks dwks is offline Offline
Posting Whiz in Training

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

 
0
  #2
Dec 11th, 2005
Don't use void main().

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.
dwk

Seek and ye shall find.

"Only those who will risk going too far can possibly find out how far one can go."
-- TS Eliot.

"I have not failed. I've just found 10,000 ways that won't work."
-- Thomas Alva Edison

"The only real mistake is the one from which we learn nothing."
-- John Powell
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 41
Reputation: free_eagle is an unknown quantity at this point 
Solved Threads: 0
free_eagle's Avatar
free_eagle free_eagle is offline Offline
Light Poster

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

 
0
  #3
Dec 11th, 2005
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!!!
Learn to forgive ,answer with kindness to evel...
Live happily and love sincerely...
[/COLOR]
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC