473,387 Members | 1,348 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

some1 tell me wats goin on....

Hi there...im new to C programmin and ive gotten a little stuck with precedence of C operators...it wud be great if some1 cud tell me how the compiler goes about solving these kinds of expressions....

i=0;
7||0&&++i;
why doesnt the value of i change???
Feb 18 '07 #1
4 1196
horace1
1,510 Expert 1GB
Hi there...im new to C programmin and ive gotten a little stuck with precedence of C operators...it wud be great if some1 cud tell me how the compiler goes about solving these kinds of expressions....

i=0;
7||0&&++i;
why doesnt the value of i change???
&& (AND) has a higher precedence than || (OR) so the expression
Expand|Select|Wrap|Line Numbers
  1. 7||0&&++i; 
  2.  
is evaluated
Expand|Select|Wrap|Line Numbers
  1. 7||(0&&++i); 
remember unlike arithmetic operators C guarantees that the operands of binary logical operators are evaluated from left to right (after any precedence) so in 0&&++i the 0 (false) will be evaluated first. In addition, if the final result of the expression can be determined from the value of first operand (in this case false) the second operand (int this case ++i) is not evaluated. Thus the value of i is not changed because the ++i is never evaluated because 0 the first operand of && is false
Feb 18 '07 #2
&& (AND) has a higher precedence than || (OR) so the expression
Expand|Select|Wrap|Line Numbers
  1. 7||0&&++i; 
  2.  
is evaluated
Expand|Select|Wrap|Line Numbers
  1. 7||(0&&++i); 
remember unlike arithmetic operators C guarantees that the operands of binary logical operators are evaluated from left to right (after any precedence) so in 0&&++i the 0 (false) will be evaluated first. In addition, if the final result of the expression can be determined from the value of first operand (in this case false) the second operand (int this case ++i) is not evaluated. Thus the value of i is not changed because the ++i is never evaluated because 0 the first operand of && is false
Hi horace!!
I get what you said and in fact i came up with this explanation myself initially....what got me really confused is this theory doesnt seem to hold for the following code; its pretty much the same code, with the zero changed to a 1:
int i=0;
7||1&&++i;
of what i gathered from your post, the compiler goes about this expression in the following way :
&& has higher precedence compared to ||, so we have 7||(1&&++i). while tryin to evaluate 1&&++i (i reckon theres is no short circuit evaluation that comes into the picture here) the value of i should have changed. However its value still remains 0!!This is precisely what is confusing me.....
Feb 18 '07 #3
horace1
1,510 Expert 1GB
Hi horace!!
I get what you said and in fact i came up with this explanation myself initially....what got me really confused is this theory doesnt seem to hold for the following code; its pretty much the same code, with the zero changed to a 1:
int i=0;
7||1&&++i;
of what i gathered from your post, the compiler goes about this expression in the following way :
&& has higher precedence compared to ||, so we have 7||(1&&++i). while tryin to evaluate 1&&++i (i reckon theres is no short circuit evaluation that comes into the picture here) the value of i should have changed. However its value still remains 0!!This is precisely what is confusing me.....
thinking about it again we rememebr C guarantees that the operands of binary logical operators are evaluated from left to right. so
Expand|Select|Wrap|Line Numbers
  1. 7||(0&&++i);
  2. 7||(1&&++i).
  3.  
in both cases the || is evaluated and it true so (1&&++i) or (0&&++i) is never eveluated, so i = 0
now consider
Expand|Select|Wrap|Line Numbers
  1. 0||1&&++i;
  2.  
0 is false so 1&&++i is evaluated
1 is true so ++i is evaluated (i is incremented to 1)
and the overall expression is true
now
Expand|Select|Wrap|Line Numbers
  1. 0||0&&++i;
0 is false so 0&&++i is evaluated
0 is false so ++i is not evaluate
the overall expression is false

do you rekon we are there now?
Feb 18 '07 #4
thinking about it again we rememebr C guarantees that the operands of binary logical operators are evaluated from left to right. so
Expand|Select|Wrap|Line Numbers
  1. 7||(0&&++i);
  2. 7||(1&&++i).
  3.  
in both cases the || is evaluated and it true so (1&&++i) or (0&&++i) is never eveluated, so i = 0
now consider
Expand|Select|Wrap|Line Numbers
  1. 0||1&&++i;
  2.  
0 is false so 1&&++i is evaluated
1 is true so ++i is evaluated (i is incremented to 1)
and the overall expression is true
now
Expand|Select|Wrap|Line Numbers
  1. 0||0&&++i;
0 is false so 0&&++i is evaluated
0 is false so ++i is not evaluate
the overall expression is false

do you rekon we are there now?
oh right.......got it now....thanks a lot.....
Feb 18 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Peter Abel | last post by:
Hi all, I'm working under W2k with Python 2.2.2 (#37, Oct 14 2002, 17:02:34) on win32 I have a file *test_data.txt* with the following content: 0123456789 0123456789 abcdefghi...
3
by: N. Volbers | last post by:
Hello everyone, I noticed that when you open a zipped tarball using 'tarfile' and if you then get the pseudo-file descriptor fd for a file via 'extractfile', then fd.tell() is broken in the...
1
by: hitendra | last post by:
wats eauivalent of IIF in C#?
1
by: jazzMan007 | last post by:
Hi All - I'm using the new UrlMappings feature in an ASP.NET 2.0 application. I'm using the unique urls, mapped to shared files, in the site map. So for example, I have pages A, B, & C...all of...
13
by: Jiho Han | last post by:
Here's the issue. You have a class, Class Person { public int id; public string firstname; public string lastname; }
0
by: Nishit | last post by:
wats the difference between debuggin and compiling?
1
by: Skerdi | last post by:
Hi, I want to print a number in a Access Report but in Barcode font. Can anyone tell me how to do it in any vb code or something? I've tried with a EAN13 FONT but my barcode reader cannot read it
3
by: Magdoll | last post by:
I was trying to map various locations in a file to a dictionary. At first I read through the file using a for-loop, but tell() gave back weird results, so I switched to while, then it worked. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.