Course Content
C Basics
C Basics
Sum, Difference, Increment, Decrement
Sum operator (+)
The addition operator +
sums the values on both sides of the +
sign.
For example:
Instead of any values, you can use variables:
Main.c
Note
In some programming languages, it is possible to sum strings, for example, "Hell" + "o" = "Hello", but not in C.
Difference operator (-)
The -
difference operator forms the difference between the values on both sides of the -
sign.
For example:
Main.c
You can use the -
sign to change positive values to negative values.
Main.c
Increment operator (++)
The increment operator was created to make the code easier to read.
This operator means +1
and is typically used in loops. This is the simplest form of using this operator.
Decrement operator (--)
The decrement operator, on the other hand, means -1
:
Post and Prefix Increment
Increment and decrement have two forms:
- prefix (++a);
- postfix (a++).
They differ in that they return different values.
Operator | Expression example | Description |
++ | a++ | increments a by 1 and return the old value |
++ | ++a | increments a by 1 and return the new value |
-- | --b | decrements b by 1 and return the new value |
-- | b-- | decrements b by 1 and returns the old value |
Main.c
If b
equals 7, what b++
equals to?
Select the correct answer
Everything was clear?