Thread: Benefits of putting ++ after a variable

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    99

    Benefits of putting ++ after a variable

    what the Benefits of putting ++ after a variable. I have tested it but give same result. Look at my both program's
    Code:
    #include <stdio.h>int main(void)
    {
        int a = 5;
        
        printf("Value of a is %d \n",a);
        
        return 0;
    }
    Value of a is 5

    Code:
    #include <stdio.h>int main(void)
    {
        int a = 5;
        
        printf("Value of a is %d \n",a++);
        
        return 0;
    }
    Value of a is 5
    Last edited by vead; 01-25-2018 at 07:28 AM.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Postfix ++ has a specific side-effect. Have you tried printing its value again?
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    99
    Quote Originally Posted by GReaper View Post
    Postfix ++ has a specific side-effect. Have you tried printing its value again?
    Code:
    #include <stdio.h>
    int main(void)
    {
     int a = 5;
     
     printf("Value of a is %d \n",a++);
     
     printf("Value of a is %d \n",a++);
     
        return 0;
    }
    Value of a is 5
    Value of a is 6

    I didn't get point, what happen when program start to execute?

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The idea is that post-fix increment returns the value before the increment as an intermediate result. It's called postfix because the value is fixed after using the variable.

    For example.
    Code:
    a = 0;
    if (a++ > 0)
       printf("The if was true. a=%d\n", a);
    
    printf("a is greater than 0 (a=%d)\n", a);
    The behavior of post-fix in this code explains the output, nothing else will. a++ > 0 is not true because a started at 0, and 0 was the intermediate result. However, a was still incremented to 1.

    If you write a statement like a++; it makes no difference how you increment. Choosing between post-fix or prefix is only important if you have some reason to use a particular value of a variable.
    Last edited by whiteflags; 01-25-2018 at 09:19 AM.

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    99
    @whiteflags @Reaper

    thanks to both of you for clearing doubt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 10-31-2012, 03:08 PM
  2. Putting text after inputting a variable
    By JDK721 in forum C++ Programming
    Replies: 2
    Last Post: 12-13-2006, 04:03 PM
  3. Replies: 4
    Last Post: 07-02-2004, 04:08 PM
  4. VC++.NET Benefits
    By aym_7 in forum C++ Programming
    Replies: 2
    Last Post: 05-12-2002, 05:55 PM
  5. benefits of c++
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 12-18-2001, 09:12 PM

Tags for this Thread