TExcelTAD
Home › C › C Change Values

C Change Variable Values

A variable is called a variable because its value can vary. After a variable is created you can assign it a new value as many times as you like. The old value is simply overwritten.

c · try it yourself
#include <stdio.h>

int main(void) {
    int level = 1;
    printf("Level: %d\n", level);

    level = 2;          // overwrite the old value
    printf("Level: %d\n", level);

    level = 10;
    printf("Level: %d\n", level);
    return 0;
}
Level: 1
Level: 2
Level: 10

Notice that the type (int) is written only once, when the variable is declared. Writing int level = 2; a second time in the same block would try to create a new variable with the same name, which is a compile error:

c
int level = 1;
int level = 2;   // error: redefinition of 'level'

Copying one variable into another

Assigning one variable to another copies the value at that moment. The two variables remain independent afterwards:

c · try it yourself
#include <stdio.h>

int main(void) {
    int original = 50;
    int copy = original;   // copy gets 50

    original = 99;         // changing original does not affect copy

    printf("original = %d\n", original);
    printf("copy     = %d\n", copy);
    return 0;
}
original = 99
copy     = 50

Using the old value to compute the new one

The right-hand side of = is evaluated first, then stored on the left. So a variable can appear on both sides — the old value is read, then replaced:

c · try it yourself
#include <stdio.h>

int main(void) {
    int coins = 10;
    coins = coins + 5;     // 10 + 5
    printf("After finding 5: %d\n", coins);

    coins = coins * 2;     // 15 * 2
    printf("After doubling: %d\n", coins);

    coins += 3;            // shorthand for coins = coins + 3
    printf("After +3: %d\n", coins);

    coins++;               // shorthand for coins = coins + 1
    printf("After ++: %d\n", coins);
    return 0;
}
After finding 5: 15
After doubling: 30
After +3: 33
After ++: 34

The shorthand operators are summarised here (more in C Operators):

Shorthand Same as
x += 3 x = x + 3
x -= 3 x = x - 3
x *= 3 x = x * 3
x /= 3 x = x / 3
x++ x = x + 1
x-- x = x - 1

Swapping two values

A classic exercise: exchange the contents of two variables. If you just write a = b; b = a;, the original value of a is lost after the first line. The fix is a temporary third variable:

c · try it yourself
#include <stdio.h>

int main(void) {
    int a = 3, b = 8;
    printf("Before: a=%d b=%d\n", a, b);

    int temp = a;   // remember a
    a = b;
    b = temp;

    printf("After:  a=%d b=%d\n", a, b);
    return 0;
}
Before: a=3 b=8
After:  a=8 b=3

Values must fit the type

When you assign a value, it is converted to the variable's type. Assigning 7.9 to an int keeps only the whole part 7 — the fraction is dropped, not rounded. You will see more about this in C Type Conversion.

Tip: If a value should never change after it is set, mark it const (see C Constants). The compiler will then reject any accidental reassignment.

Test yourself

After int x = 4; x = x * 3; x = x - 2; what is x?

  1. 4
  2. 10
  3. 12
  4. 2

After int a = 1; int b = a; a = 5; what is b?

  1. 5
  2. 1
  3. 0
  4. 6

Which statement adds 10 to total?

  1. total =+ 10;
  2. total += 10;
  3. int total += 10;
  4. total + 10;