Arithmetic
In C, basic arithmetic operations such as addition, subtraction, multiplication, and division can be performed using a set of built-in operators. C also supports more advanced arithmetic operations such as modulo (remainder) and increment/decrement.
Here are some examples of basic arithmetic operations in C:
#include <stdio.h>
int main(void) {
int a = 10;
int b = 5;
int sum = a + b; // addition
int diff = a - b; // subtraction
int product = a * b; // multiplication
int quotient = a / b; // division
int remainder = a % b; // modulo (remainder)
printf("Sum: %d\n", sum);
printf("Difference: %d\n", diff);
printf("Product: %d\n", product);
printf("Quotient: %d\n", quotient);
printf("Remainder: %d\n", remainder);
return 0;
}
C also supports the increment (++) and decrement (--) operators, which can be used to increase or decrease the value of a variable by 1. Here is an example of how these operators can be used:
#include <stdio.h>
int main(void) {
int num = 5;
printf("num: %d\n", num); // prints 5
num++; // increases the value of num by 1
printf("num: %d\n", num); // prints 6
num--; // decreases the value of num by 1
printf("num: %d\n", num); // prints 5
return 0;
}
Last modified on: 2023-01-10 00:45:31