C language content

In the C programming language, arithmetic operators are used to perform basic mathematical operations on numerical data types such as integers and floating-point numbers. Here’s a detailed description of each arithmetic operator in C:

1. Addition ( ' + ' )

The ' + 'operator adds two operands.

Example:

				
					int a = 5, b = 3;
int result = a + b;  // result is 8

				
			

2. Subtraction ( ' - ' )

The ' - ' operator subtracts the second operand from the first.

Example:

				
					int a = 5, b = 3;
int result = a - b;  // result is 2

				
			

3. Multiplication ( ' * ' )

The ' * ' operator multiplies two operands.

Example:

				
					int a = 5, b = 3;
int result = a * b;  // result is 15

				
			

4. Division ( ' / ' )

The ' / ' operator divides the first operand by the second. The result is of the same type as the operands. For integers, the result is truncated towards zero.

Example:

				
					int a = 6, b = 3;
int result = a / b;  // result is 2

float x = 5.0, y = 2.0;
float result2 = x / y;  // result is 2.5

				
			

5. Modulus ( ' % ' )

The ' % ' operator gives the remainder of the division of the first operand by the second. It is only applicable to integer operands.

Example:

				
					int a = 5, b = 3;
int result = a % b;  // result is 2

				
			

Unary Arithmetic Operators

These operators operate on a single operand.

6. Unary Plus ( ' + ' )

The + operator is used to indicate a positive value, although it’s rarely used explicitly as numbers are positive by default.

Example:

				
					int a = +5;  // same as int a = 5;

				
			

7. Unary Minus ( ' - ' )

The - operator negates the value of its operand.

Example:

				
					int a = 5;
int result = -a;  // result is -5

				
			

Increment and Decrement Operators

These are special unary operators used to increase or decrease the value of a variable by 1.

8. Increment ( ' ++ ' )

Increases the value of its operand by 1. It can be used in two forms:

  • Prefix increment (++a): Increments the value before it is used in an expression.
  • Postfix increment (a++): Increments the value after it is used in an expression.

Example:

				
					int a = 5;
int b = ++a;  // a is incremented to 6, then b is assigned 6

int c = 5;
int d = c++;  // d is assigned 5, then c is incremented to 6

				
			

9. Decrement ( ' -- ' )

Decreases the value of its operand by 1. It also has prefix and postfix forms:

  • Prefix decrement (--a): Decrements the value before it is used in an expression.
  • Postfix decrement (a--): Decrements the value after it is used in an expression.

Example:

				
					int a = 5;
int b = --a;  // a is decremented to 4, then b is assigned 4

int c = 5;
int d = c--;  // d is assigned 5, then c is decremented to 4

				
			

Detailed Examples and Notes

Mixed-Type Arithmetic

When performing arithmetic operations on mixed types (e.g., an ‘int‘ and a ‘float‘), C promotes the ‘int‘ to a ‘float‘ before performing the operation.

Example:

				
					int a = 5;
float b = 2.0;
float result = a + b;  // result is 7.0

				
			

Integer Division

When both operands of a division are integers, the result is also an integer, and any fractional part is discarded.

Example:

				
					int a = 7, b = 2;
int result = a / b;  // result is 3, not 3.5

				
			

Division by Zero

Attempting to divide by zero using either the / or % operator results in undefined behavior and typically causes a runtime error.

Example:

				
					int a = 5, b = 0;
int result = a / b;  // Undefined behavior

				
			

Understanding these operators and how they interact with different data types and contexts is fundamental to writing correct and efficient C programs.

Scroll to Top