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

increment and decrement operator

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 1613
weaknessforcats
9,208 Expert Mod 8TB
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
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 Expert Mod 8TB
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
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 Expert 2GB
@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: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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:
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...
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?
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...

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.