473,387 Members | 1,641 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.

what is the value of a,b,c,d after increment operation?

void main()
{
int a=1,c=1,d;
int b=++a + ++a + ++a;
d=++c + ++c + ++c;
printf("%d%d%d%d",a,b,c,d);
}
Sep 15 '14 #1
3 1097
horace1
1,510 Expert 1GB
The C standard does not specify the order in which the operands (suh as ++a) of an operator (such as the + in ++a + ++a) are evaluated and there is no guarantee when an affected variable will change its value.

consider
Expand|Select|Wrap|Line Numbers
  1. int a=1,c=1,d;
  2. int b=++a + ++a + ++a;
however, when are the ++a operands evaluated?
1. the ++a operands of (++a + ++a) evaluated and the result added then the final ++a evaluated and added? the result is a=4 and b=10
2. all the ++a operands evaluated and the then the + operations executed giving a=4 and b=12
running the above code Visual C++ gives b=12 and gcc gives b=10 (both give a=4)

the advice is do not use a variable more than once in an expression if one (or more) of the references has one of ++ or -- operators attached to it.
Sep 15 '14 #2
carbon
9
Well correct me if I'm wrong. But I think it's a undefined behavior.

Between two sequence points a variable must not be modified more than once.

To understand what's going on you have to learn about undefined-behavior and sequence-points.
Checkout this question on stackoverflow.com.
Sep 16 '14 #3
horace1
1,510 Expert 1GB
yes, in the case of the the expression
Expand|Select|Wrap|Line Numbers
  1. int b=++a + ++a + ++a;
the sequence point is the ; at the end of the statement when "it is guaranteed that all side effects of previous evaluations will have been performed, and no side effects from subsequent evaluations have yet been performed", i.e when are ++a operands evaluated? you don't know they are all evaluated until the sequence point and different compilers etc give different results
Sep 19 '14 #4

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

Similar topics

15
by: Greenhorn | last post by:
Hi, when a function doesn't specify a return type ,value what value is returned. In the below programme, the function sample()is returning the value passed to 'k'. sample(int); main() { int...
4
by: Jimbo1873 | last post by:
Hi can someone help me with these problems. 1. what value is assigned to the type 'double' variable 'x' by the statement x = 25.0 * / 2.5; 2. what value is assigned to 'x' by the following...
7
by: Al | last post by:
Hi, can anyone tell me what the following means? x is a float, and j an integer. What value is in j afterwards? j=0; j=(int)(x)&512; Now if I use 128 instead of 512 what's the difference? ...
50
by: LaundroMat | last post by:
Suppose I have this function: def f(var=1): return var*2 What value do I have to pass to f() if I want it to evaluate var to 1? I know that f() will return 2, but what if I absolutely want to...
6
by: news.pace.co.uk | last post by:
Is the following legal C: count++++; Although I would expect to be shot for using such dodgy syntax, the question is, is it legal syntax? I was recently led to belive this should compile but...
13
by: bintom | last post by:
Is there any reason why the following C++ code behaves as it does ? int i; i=1; cout << (++i)++; Output: 2 i=1; cout << ++(++i); Output: 3
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: 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:
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
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...

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.