4 Replies - 3792 Views - Last Post: 16 May 2015 - 08:13 AM Rate Topic: -----

#1 shadow125  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 53
  • Joined: 21-April 15

C language: What's the difference between a++ and ++a ?

Posted 16 May 2015 - 07:53 AM

What's up folks

I am reading a C book, and one of the exercises got me really confused. The exercise gives a code and asks to predict what will be shown in the screen, so here it is:

#include <stdio.h>

int main (void)
{
    int a, b = 0, c = 0;

    a = ++b + ++c;
    printf ("%d %d %d\n", a, b, c);
    a = b++ + c++;
    printf ("%d %d %d\n", a, b, c);
    a = ++b + c++;
    printf ("%d %d %d\n", a, b, c);
    a = b-- + --c;
    printf ("%d %d %d\n", a, b, c);

    return 0;
}



The output:
2 1 1
2 2 2
5 3 3
5 2 2



So I was wondering, if b is first set as 0, why does it return the value of 2 on the first printf? And what is the difference between ++a and a++?
Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: C language: What's the difference between a++ and ++a ?

#2 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3714
  • Posts: 13,468
  • Joined: 08-August 08

Re: C language: What's the difference between a++ and ++a ?

Posted 16 May 2015 - 08:03 AM

I rarely find it necessary to use pre or postfix in an equation. Good thing, because I think doing so makes the code far less readable.
This link has a decent explanation of the difference:
http://www.programiz...-prefix-postfix
Was This Post Helpful? 1
  • +
  • -

#3 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2130
  • View blog
  • Posts: 4,196
  • Joined: 30-May 10

Re: C language: What's the difference between a++ and ++a ?

Posted 16 May 2015 - 08:03 AM

> 07 a = ++b + ++c;
The prefix form means the increment happens first, and then the incremented value is used in the rest of the expression.
So you end up with a = 1 + 1 in this case.

The postfix form means the OLD value is used in the expression, and then the variable is incremented afterwards.
Was This Post Helpful? 1
  • +
  • -

#4 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3714
  • Posts: 13,468
  • Joined: 08-August 08

Re: C language: What's the difference between a++ and ++a ?

Posted 16 May 2015 - 08:05 AM

Oops: b is incremented (1), and this is added to the value of c after it has be incremented.

This post has been edited by CTphpnwb: 16 May 2015 - 08:07 AM

Was This Post Helpful? 1
  • +
  • -

#5 shadow125  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 53
  • Joined: 21-April 15

Re: C language: What's the difference between a++ and ++a ?

Posted 16 May 2015 - 08:13 AM

View PostCTphpnwb, on 16 May 2015 - 08:05 AM, said:

Oops: b is incremented (1), and this is added to the value of c after it has be incremented.

Oh, now everything is clear to me, and thanks for the link.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1