473,386 Members | 1,679 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,386 software developers and data experts.

my c++ query for pre and post operatois

hi,
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,k;
clrscr();
k=0;
k++;
i=0;
j=i+++i++;
printf("%d %d %d\n",k,i,j);

k++;
i=0;
//j=i++ +++i; this line gives an compiler error
printf("%d %d %d\n",k,i,j);
k++;
i=0;
//j=++i++i; this line gives an compiler error
printf("%d %d %d\n",k,i,j);
k++;
i=0;
j=++i+ ++i;
printf("%d %d %d\n",k,i,j);
k++;
i=0;
//j=++i++;this line gives an compiler error
printf("%d %d %d\n",k,i,j);
k++;
i=0;
j=++i;
printf("%d %d %d\n",k,i,j);
k++;
i=0;
j=+++i;
printf("%d %d %d\n",k,i,j);
k++;
i=0;
j=i+++ ++i;
printf("%d %d %d\n",k,i,j);
k++;
i=0;
j=i+++ i++;
printf("%d %d %d\n",k,i,j);
getch();
return 0;
}
given this program,i fail 2 understand how c++/c compiler handles the pre and post increment operators.
can some one explain me the logic compiler follows?
regards,
kirit ved
Jul 9 '07 #1
3 1687
scruggsy
147 100+
What do you want to know - what values will be stored in j in each case, or why the compiler rejects the commented lines?
Expand|Select|Wrap|Line Numbers
  1. j=++i++i
...doesn't compile because it doesn't make sense.
The pre-increment operator increments the variable's value first, then uses it.
Expand|Select|Wrap|Line Numbers
  1. i = 1; j = ++i; //now i==2, j==2
The post-increment operator uses the value, then increments it.
Expand|Select|Wrap|Line Numbers
  1. i = 1; j = i++; //now i==2, j==1
The behavior can be undefined, when you increment a variable more than once within one expression.
When in doubt, or when dealing with non-trivial expressions, you're better off doing the increment outside of the expression.
Jul 9 '07 #2
I would suggest using parenthesis to control these statements. That way it won't really matter how the compiler wants to interpret the pre and post increment operators. Just make it interpret them the way you want.
Jul 9 '07 #3
JosAH
11,448 Expert 8TB
I would suggest using parenthesis to control these statements. That way it won't really matter how the compiler wants to interpret the pre and post increment operators. Just make it interpret them the way you want.
That won't help you much, e.g.

Expand|Select|Wrap|Line Numbers
  1. int i=42;
  2. i= (++i)+(i++);
  3.  
The outcome is still undefined because according to the Standard, altering a
modifiable lvalue more than once before a sequence point is reached causes
undifined behaviour.

Parentheses can only alter default precedence of operators, not the order in
which operands are evaluated.

kind regards,

Jos
Jul 9 '07 #4

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

Similar topics

5
by: ensnare | last post by:
I'm attempting to create a threaded comment system involving PHP / MySQL. Currently, I have a table called comments which looks like: Table Comments: (comment_id, comment_root_id,...
14
by: Bob | last post by:
Hi there, Need a little help with a certain query that's causing a lot of acid in my stomach... Have a table that stores sales measures for a given client. The sales measures are stored per...
3
by: Harvey | last post by:
Hi, I try to write an asp query form that lets client search any text-string and display all pages in my web server that contain the text. I have IIS 6.0 on a server 2003. The MSDN site says...
6
by: jjturon | last post by:
Can anyone help me?? I am trying to pass a Select Query variable to a table using Dlookup and return the value to same select query but to another field. Ex. SalesManID ...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.