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

pre and post increment

int main()
{
int a=5,s;
s=++a + ++a;
printf("%d",a);
printf("%d",s);
}

output is 7 and 14

BUT

int main()
{
int a, s;
printf("Enter value of a");
scanf ("%d",&a);
s=++a + ++a;
printf("%d",a);
printf("%d",s);
}

input user gives is 5
output is 7 and 13

WHY?
Aug 17 '10 #1
2 1396
zahraZ
17
hello,
i test your codes but they have same result!(7 & 14)
Aug 17 '10 #2
newb16
687 512MB
Because the result of ++a + ++a is undefined per the language standard. (see two similar topics below). Value of 'a' is guaranteed to be 7 after evaluating of this expression.
Aug 17 '10 #3

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

Similar topics

9
by: Mark Turney | last post by:
I was reading "Practical C++ Programming" yesterday, and it mentioned that the order of execution for post-increment and post-decrement operators was ambiguous. I had previously learned that a...
15
by: Robert Swan | last post by:
I'd like to know why the following program outputs 1, and not 0. #include <iostream> class a { int v; public: a():v(0){} a& operator++(int) { v++;
8
by: Angel Tsankov | last post by:
Should pre/post increment/decrement return const or non-const? What about other functions?
1
by: mohsin | last post by:
hi everyone well i m little bit confused about the use of pre an post increment infact my thinking contradict with the logic of programe lets consider an example x=5; y=x++; z=x; after the...
11
by: divya_rathore_ | last post by:
The code: int aaa = 100; printf("%d %d %d\n", --aaa, aaa, aaa--); printf("%d\n", aaa); prints: 99 100 100 98
2
by: mohammad ismail aakhil | last post by:
i have overladed unary ++ both pre and postfix increment operator using member function. both the operator function differ by arguements.v use a additional argunent of type int. how does complier...
2
by: psmita | last post by:
Hi, Can anyone explain me the major diference in overloading preincrement and post increment operators? Regards, Smita
13
by: jehugaleahsa | last post by:
Hello: In C++, you had to distinguish between post and pre increments when overloading. Could someone give me a short demonstration of how to write these? I get the impression that are...
3
by: Stang1 | last post by:
The following statement: line_buf = ' '; is equivalent to: line_buf = ' '; line_len++;
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: 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
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
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,...

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.