Find the answer to your Linux question:
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; ...
  1. #1
    Linux 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
    Code:
    #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);
    }
    and i am getting the below output
    Code:
    9
    10
    9
    10
    10
    i am very much confused. how it is parsing and how i will get this output?please explain me
    Thanks in advance...

  2. #2
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    Here's your program with a few corrections, plus when its compiled with strict settings it produced a few warnings.

    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;
    }
    Now this is what the compiler had to say...
    warning: 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
    Undefined behavior - Wikipedia, the free encyclopedia

    By the way here's how I compiled the above code:

    gcc testit.c -Wall -ansi -pedantic -o testit
    Make mine Arch Linux

  3. #3
    Linux 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...

  4. #4
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    Quote Originally Posted by shariefbe View Post
    No man. again i am getting the same output which i got from my program
    That's because the operation(s) may produce undefined behaviour.
    Make mine Arch Linux

  5. #5
    Linux Newbie
    Join Date
    Sep 2004
    Location
    UK
    Posts
    160
    Out of interest tried a similar thing in java
    Code:
    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);
       }
    }
    It produces what I would have expected

    Code:
    10
    12
    12
    10
    10
    Don't have a windows/other compiler to see what we get.
    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?

  6. #6
    Linux Newbie
    Join Date
    Apr 2007
    Posts
    119
    Quote Originally Posted by shariefbe View Post
    No man. again i am getting the same output which i got from my program
    There is no way to tell what the state of your variable "a" is at the time of execution, that is why the behavior is undefined.

    You don't know which order the variable will be processed.

  7. #7
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    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

  8. #8
    Linux Guru Rubberman's Avatar
    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!

  9. #9
    Just Joined!
    Join Date
    Jan 2011
    Posts
    1
    Quote Originally Posted by shariefbe View Post
    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;
    
    printf("\n%d\n%d\n%d\n%d\n%d\n",a++,++a,a--,--a,a);
    }
    and i am getting the below output
    Code:
    9
    10
    9
    10
    10
    i am very much confused. how it is parsing and how i will get this output?please explain me

    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.

  10. #10
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    This thread is 8 months old. Closing.
    DISTRO=Arch
    Registered Linux User #388732

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...