473,468 Members | 1,458 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

increment and decrement operator

4 New Member
please told me the answer and reason for the value of a and b
Expand|Select|Wrap|Line Numbers
  1. #include<iostream.h>
  2. #include<conio.h>
  3. void main()
  4. {int a=3;
  5. int b = ++a + a++ + a-- + --a;
  6. cout<<"\n enter a:"<<a;
  7. cout<<"\n enter b:"<<b;
  8. getch();
  9. }
Sep 23 '13 #1

✓ answered by weaknessforcats

The way to handle these problems is to never change a variable more than once in any statement.

There is no way to know what the compiler is doing since different compilers behave differently.

5 1618
weaknessforcats
9,208 Recognized Expert Moderator Expert
The answer is indeterminate.

Variables may be evaluated by the compiler in any order within a statement. If you change the value of a variable more than once inside the statement, the change may be applied in an order that's different from the one in your mind.
Sep 23 '13 #2
JATINCSE
4 New Member
please suggest me the way to handle these type of problems.
i can predict the answer of these type of statement but the way of predicting is not clear to me that what actually the compiler is doing
Sep 28 '13 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
The way to handle these problems is to never change a variable more than once in any statement.

There is no way to know what the compiler is doing since different compilers behave differently.
Sep 28 '13 #4
HSharma1729
2 New Member
Output will be:
enter a:3
enter b:12

Reason:
First evaluate the values of prefix operations and then solve the problem by using using that value.
Here,
a=3
b=++a + a++ + a-- + --a
so, a=3+1-1=3 (1 prefix increment and 1 prefix decrement)
hence, b=3+3+3+3=12
a=3+1+1-1-1=3
Dec 13 '13 #5
donbock
2,426 Recognized Expert Top Contributor
@HSharma1729, those might be the answers obtained for your compiler but I assure you there are other compilers out there that will give different answers.

The C Standard explicitly says the results are undefined for an expression that changes the value of a variable more than once.

You can test your compiler to get a feel for what it does for specific instances of undefined behavior, but you can never be sure. For example, behavior frequently varies based on optimization setting. What you find out about how one compiler reacts to undefined behavior tells you nothing about how another compiler will react.

Avoid undefined behavior.
Dec 13 '13 #6

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

Similar topics

1
by: Hendrix | last post by:
Hi, I would like to pick four elements from a multidemensional array and use the content of those elements. I want to do this without all the subscript calls required for individual access, just...
4
by: John | last post by:
I was wondering why the STL RB Tree implementation is used when the iterators of map are incremented/decremented to yield successors and predecessors? This takes O(log n) time (and a lot of memory...
8
by: lovecreatesbeauty | last post by:
Hello experts, Why can this difference between prefix increment/decrement and postfix increment/decrement reside in built-in operators for built-in data types? Thanks. // test.cpp // //...
5
by: Ian Pilcher | last post by:
I'm trying to figure out if an increment to a variable of an integer type, followed by a decrement, (or vice versa) is guaranteed to restore the variable to its initial value, even if the first...
2
by: rahul8143 | last post by:
hello, I am confused with following programs snippet code about how the answer comes? statement/expression is evaluated? 1) int i=5; i=i++*i++*i++*i++; why the answer is not 5*6*7*8= 1680 but...
8
by: Angel Tsankov | last post by:
Should pre/post increment/decrement return const or non-const? What about other functions?
5
by: Stuart | last post by:
Hi all, Iv'e got a page that has a mass amount of input fields, all of which require a decimal figure. To make it easier when it comes to inputting data, I'm trying to setup + and - links that...
6
by: Kevin Walzer | last post by:
This code: #include <stdio.h> int main(void) { int n1, n2; //two integers n1 = 1; n2 = 1;
8
by: cpptutor2000 | last post by:
Could some C guru please help ? I have: int x = 20, y = 35; x = x++ + y++; y = ++x + ++y; printf("%d %d\n", x, y); The answers are 57, 94 I tried reasoning using the standard C semantics...
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.