Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer Sum, Difference, Increment, Decrement | Operators
C Basics

bookSum, Difference, Increment, Decrement

Addition Operator (+)

The + operator adds the values on either side of it.

int result = 50 + 2;

You can also use variables in place of direct values.

Main.c

Main.c

copy
12345678910111213
#include <stdio.h> int main() { int carsSold_2022 = 12; int carsSold_2023 = 45; int carsSold = carsSold_2022 + carsSold_2023; printf("Cars sold in two years: %d", carsSold); return 0; }

Note

While some programming languages allow you to concatenate strings using the + operator, like "Hell" + "o" = "Hello", C does not.

Subtraction Operator (-)

The subtraction operator, -, calculates the difference between the values on either side of it.

Main.c

Main.c

copy
12345678910111213
#include <stdio.h> int main() { int carsSold_2022_2023 = 200; int carsSold_2023 = 57; int carsSold_2022 = carsSold_2022_2023 - carsSold_2023; printf("Cars sold in 2022: %d", carsSold_2022); return 0; }

You can also use the - sign to convert positive values to their negative counterparts.

Main.c

Main.c

copy
12345678910
#include <stdio.h> int main() { int value = 100; printf("Negative value: %d", -value); return 0; }

Increment Operator (++)

The increment operator, ++, was introduced to simplify code readability.

int value = 100;
value++; // Equivalent to: value += 1

This operator adds 1 to a value, and it's commonly used in loops. The example above illustrates the most basic use of the operator.

Decrement Operator (--)

Conversely, the decrement operator, --, subtracts 1 from a value:

int value = 100;
value--;  // Equivalent to: value -= 1

Pre vs. Post Increment

Increment and decrement operators come in two forms:

  • prefix (++a);
  • postfix (a++).

The distinction lies in the values they return:

OperatorUsageDescription
++a++Increments a by 1 but returns its original value
++++aIncrements a by 1 and returns its incremented value
----aDecreases a by 1 and returns its decremented value
--a--Decreases a by 1 but returns its original value
Main.c

Main.c

copy
123456789101112131415161718
#include <stdio.h> int main() { int a = 5, b = 5; // a is increased to 6 // Then, it is displayed. printf("Prefix form a: %d\n", ++a); printf("a = %d\n", a); // 5 is displayed // Then, b is increased to 6. printf("Postfix form b: %d\n", b++); printf("b = %d\n", b); return 0; }
question mark

If b equals 7, what b++ equals to?

Select the correct answer

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 3. Hoofdstuk 2

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

Awesome!

Completion rate improved to 2.63

bookSum, Difference, Increment, Decrement

Veeg om het menu te tonen

Addition Operator (+)

The + operator adds the values on either side of it.

int result = 50 + 2;

You can also use variables in place of direct values.

Main.c

Main.c

copy
12345678910111213
#include <stdio.h> int main() { int carsSold_2022 = 12; int carsSold_2023 = 45; int carsSold = carsSold_2022 + carsSold_2023; printf("Cars sold in two years: %d", carsSold); return 0; }

Note

While some programming languages allow you to concatenate strings using the + operator, like "Hell" + "o" = "Hello", C does not.

Subtraction Operator (-)

The subtraction operator, -, calculates the difference between the values on either side of it.

Main.c

Main.c

copy
12345678910111213
#include <stdio.h> int main() { int carsSold_2022_2023 = 200; int carsSold_2023 = 57; int carsSold_2022 = carsSold_2022_2023 - carsSold_2023; printf("Cars sold in 2022: %d", carsSold_2022); return 0; }

You can also use the - sign to convert positive values to their negative counterparts.

Main.c

Main.c

copy
12345678910
#include <stdio.h> int main() { int value = 100; printf("Negative value: %d", -value); return 0; }

Increment Operator (++)

The increment operator, ++, was introduced to simplify code readability.

int value = 100;
value++; // Equivalent to: value += 1

This operator adds 1 to a value, and it's commonly used in loops. The example above illustrates the most basic use of the operator.

Decrement Operator (--)

Conversely, the decrement operator, --, subtracts 1 from a value:

int value = 100;
value--;  // Equivalent to: value -= 1

Pre vs. Post Increment

Increment and decrement operators come in two forms:

  • prefix (++a);
  • postfix (a++).

The distinction lies in the values they return:

OperatorUsageDescription
++a++Increments a by 1 but returns its original value
++++aIncrements a by 1 and returns its incremented value
----aDecreases a by 1 and returns its decremented value
--a--Decreases a by 1 but returns its original value
Main.c

Main.c

copy
123456789101112131415161718
#include <stdio.h> int main() { int a = 5, b = 5; // a is increased to 6 // Then, it is displayed. printf("Prefix form a: %d\n", ++a); printf("a = %d\n", a); // 5 is displayed // Then, b is increased to 6. printf("Postfix form b: %d\n", b++); printf("b = %d\n", b); return 0; }
question mark

If b equals 7, what b++ equals to?

Select the correct answer

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 3. Hoofdstuk 2
some-alt