473,406 Members | 2,312 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,406 software developers and data experts.

about ++ operator

9
I have Red Hat Linux enterprise-4 edition on my pc. Problem is this.


int main()
{
int i=1;
i=i++;
printf("%d",i);
return 0;
}
Out put of this program is 1 in my pc & 2 in turbo c. what is the correct result.
Please help me.
Nov 17 '06 #1
3 966
horace1
1,510 Expert 1GB
I have Red Hat Linux enterprise-4 edition on my pc. Problem is this.


int main()
{
int i=1;
i=i++;
printf("%d",i);
return 0;
}
Out put of this program is 1 in my pc & 2 in turbo c. what is the correct result.
Please help me.
The value of i after
i=i++;
depends on whether the increment of i occurs before or after the assignment. in C it's unspecified; in some implementations the value will be 1, in some it will be 2. Simillarly
(i * j) + (i * j++)
will the value of j in (i * j) be that before or after the increment in (i * j++) ? again it is unspecified.
The advice is do not use a variable more than once in an expression if one or more of the references has a ++ or -- operator attached to it.
Nov 17 '06 #2
Ganon11
3,652 Expert 2GB
Indeed. The statement

i=i++;

is like saying

i = i = i + 1;

Why not just say

i++;

and be done with it? There's no need to reassign i to itself, since ++ stands for

i = i + 1;
Nov 17 '06 #3
horace1
1,510 Expert 1GB
have a look at the C FAQ on URL
http://c-faq.com/

in particular the discussion about "i=i++;" on URL
http://c-faq.com/expr/ieqiplusplus.html

and " will a[i] = i++; work" on URL
http://c-faq.com/expr/evalorder1.html

there is also a good discussion on expressions and "side effects" on URL
http://www.csci.csusb.edu/dick/c++std/cd2/expr.html

where it states in paragraph 4
"4 Except where noted, the order of evaluation of operands of individual
operators and subexpressions of individual expressions, and the order
in which side effects take place, is unspecified. ....."
Nov 17 '06 #4

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

Similar topics

5
by: Tony Johansson | last post by:
Hello experts! I have two class template below with names Array and CheckedArray. The class template CheckedArray is derived from the class template Array which is the base class This program...
7
by: Eckhard Lehmann | last post by:
Hi, I try to recall some C++ currently. Therefore I read the "Standard C++ Bible" by C. Walnum, A. Stevens and - of course there are chapters about operator overloading. Now I have a class...
81
by: Matt | last post by:
I have 2 questions: 1. strlen returns an unsigned (size_t) quantity. Why is an unsigned value more approprate than a signed value? Why is unsighned value less appropriate? 2. Would there...
36
by: utab | last post by:
Dear, I have experince in C( numerical projects, like engineering problems, scientific applications) I have the basic notion of C++ also, I have read Accelerated C++ until Chapter 7, however it...
56
by: spibou | last post by:
In the statement "a *= expression" is expression assumed to be parenthesized ? For example if I write "a *= b+c" is this the same as "a = a * (b+c)" or "a = a * b+c" ?
1
by: Gonçalo Rodrigues | last post by:
Hi all, I am a little confused about the delete operator, so I have a question. Suppose we have something like class Base { public: void* operator new(std::size_t size); void operator...
15
by: Jeroen | last post by:
Hi all, I've got a very specific question about the evaluation order in C++. Assume some kind of custom array class, with an overloaded subscript operator. In the following code: { my_array...
8
by: Wayne Shu | last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition). When I read chapter 11 Operator Overloading, I have two questions. 1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular,...
7
by: vlsidesign | last post by:
I am not sure how to think about parentheses. It seems when they are used alongside characters it indicates a function, like addTwoNums(). But is also is used to enforce which operators and...
8
by: Tony Johansson | last post by:
Hello! I think that this text is complete enough so you can give me an explanation what they mean with it. I wonder if somebody understand what this means. It says the following : "You can't...
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: 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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.