Results 1 to 10 of 10
Hello friends,
I have an big doubt in increment and decrement in c programming
i am trying to compile the below code in gcc
Code:
#include<stdio.h>
main()
{
int a=10,b=12;
...
- 05-26-2010 #1Linux User
- Join Date
- Aug 2008
- Location
- Trichy,India
- Posts
- 308
C programming increment and decrement doubt
Hello friends,
I have an big doubt in increment and decrement in c programming
i am trying to compile the below code in gcc
and i am getting the below outputCode:#include<stdio.h> main() { int a=10,b=12; printf("\n%d\n%d\n%d\n%d\n%d\n",a++,++a,a--,--a,a); }
i am very much confused. how it is parsing and how i will get this output?please explain meCode:9 10 9 10 10
Thanks in advance...
- 05-26-2010 #2
Here's your program with a few corrections, plus when its compiled with strict settings it produced a few warnings.
Now this is what the compiler had to say...Code:#include<stdio.h> int main() { int a = 10; printf("\n%d\n%d\n%d\n%d\n%d\n",a++,++a,a--,--a,a); return 0; }
Undefined behavior - Wikipedia, the free encyclopediawarning: operation on ‘a’ may be undefined
warning: operation on ‘a’ may be undefined
warning: operation on ‘a’ may be undefined
warning: operation on ‘a’ may be undefined
By the way here's how I compiled the above code:
gcc testit.c -Wall -ansi -pedantic -o testitMake mine Arch Linux
- 05-26-2010 #3Linux User
- Join Date
- Aug 2008
- Location
- Trichy,India
- Posts
- 308
No man. again i am getting the same output which i got from my program
Thanks in advance...
- 05-26-2010 #4
- 05-26-2010 #5Linux Newbie
- Join Date
- Sep 2004
- Location
- UK
- Posts
- 160
Out of interest tried a similar thing in java
It produces what I would have expectedCode:public class test { public static void main(String[] args) { int a = 10; print(a++, ++a, a--, --a, a); } public static void print(int a, int b, int c, int d, int e) { System.out.println(a + "\n" + b + "\n" + c + "\n" + d + "\n" + e); } }
Don't have a windows/other compiler to see what we get.Code:10 12 12 10 10
Could'nt find a compiler switch to make it do operations in strict order.In a world without walls and fences, who needs Windows and Gates?
- 05-27-2010 #6Linux Newbie
- Join Date
- Apr 2007
- Posts
- 119
- 05-27-2010 #7
markcole is correct. When you have multiple operations on a single line, you don't know the order that they will occur. This is why decrements seem to be happening before increments, even though the increments appear leftmost.
Java may have different rules, which is why you are seeing different behaviour there.
To get what you want, you would have to separate it into multiple lines:
Code:printf("\n%d", a++); printf("\n%d", ++a); ...DISTRO=Arch
Registered Linux User #388732
- 05-30-2010 #8Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
First, the order of evaluation of arguments is compiler-defined. NEVER, NEVER, NEVER do what you did here!
I can't even begin to guess how many applications that other engineers wrote that I had to debug because of this sort of cruft!
Second, in an expression, prefix increment/decrement operators are applied before the variable is used, and postfix operators are applied after the variable is used. So, in this case "a = ++b" the value of a will be set to b AFTER b has been incremented and after the expression a and b will be equal, whereas in the case "a = b--", a will be set before b has been decremented, so after the expression, b will be one less than a.Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 01-28-2011 #9Just Joined!
- Join Date
- Jan 2011
- Posts
- 1
The reason for such an output is nothing but, LIFO concept. That is "Last In First Out" . Try evaluating it from the right hand side. You will get it.
The last is a whose value is 10, that is displayed at the last line. Then come to the next and so on . This is how that output is generated.
- 01-29-2011 #10
This thread is 8 months old. Closing.
DISTRO=Arch
Registered Linux User #388732



