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

Page 1 of 1

## 4 Replies - 3899 Views - Last Post: 16 May 2015 - 08:13 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=376054&amp;s=d0caa64418505cfab13a799a24fb0ba1&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

Reputation: 1
• 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++?

Is This A Good Question/Topic? 0

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

### #2 CTphpnwb

• D.I.C Lover

Reputation: 3746
• Posts: 13,621
• 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

### #3 Salem_c

• void main'ers are DOOMED

Reputation: 2131
• 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.

### #4 CTphpnwb

• D.I.C Lover

Reputation: 3746
• Posts: 13,621
• 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

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

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

Posted 16 May 2015 - 08:13 AM

CTphpnwb, 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.