C - Arithmetic operators

In the C programming language, arithmetic operators are used to perform mathematical calculations on numeric data types such as integers and floating-point numbers. Here are the common arithmetic operators in C:

1. Addition (+): Adds two numbers together.

int result = 3 + 5; // result will be 8
2. Subtraction (-): Subtracts the second number from the first.

int result = 7 - 2; // result will be 5
3. Multiplication (*): Multiplies two numbers together.

int result = 4 * 6; // result will be 24
4. Division (/): Divides the first number by the second.

int result = 10 / 2; // result will be 5
Note: Integer division truncates the decimal part, so 10 / 3 would result in 3, not 3.333.
5. Modulus (%): Returns the remainder when the first number is divided by the second.

int result = 11 % 3; // result will be 2
6. Increment (++) and Decrement (--): These operators are used to increase or decrease the value of a variable by 1, respectively.

int x = 5;
x++; // x is now 6
x--; // x is back to 5
7. Exponentiation (^): C does not have a built-in exponentiation operator like some other languages. You would need to use a library function or write your own code to perform exponentiation.

These arithmetic operators can be used with various data types, including integers (int, long, short), floating-point numbers (float, double), and more. The result of an arithmetic operation typically has the same data type as the operands involved in the operation.

Here's an example that demonstrates the use of arithmetic operators in C:


#include <stdio.h>

int main() {
    int a = 10;
    int b = 3;

    int sum = a + b;
    int difference = a - b;
    int product = a * b;
    int quotient = a / b;
    int remainder = a % b;

    printf("Sum: %d\n", sum);
    printf("Difference: %d\n", difference);
    printf("Product: %d\n", product);
    printf("Quotient: %d\n", quotient);
    printf("Remainder: %d\n", remainder);

    return 0;
}

With this code, the program will compile and run without errors, and the output will be as follows:


Sum: 13
Difference: 7
Product: 30
Quotient: 3
Remainder: 1

The program calculates and prints the results of addition, subtraction, multiplication, division, and the modulus operation on the values of a and b.

Arithmetic expressions and Evaluation of expressions

In the C programming language, arithmetic expressions are combinations of variables, constants, and operators that can be evaluated to produce a single numeric result. These expressions can be used in various parts of C code, such as assignments, conditional statements, and function arguments. Here's an overview of how arithmetic expressions are evaluated in C:

1. Operators:

C provides a variety of arithmetic operators for performing calculations. These operators include addition (+), subtraction (-), multiplication (*), division (/), modulus (%), increment (++), and decrement (--), among others.

2. Operands:

Operands are the values or variables on which the operators act. For example, in the expression a + b, a and b are operands.

3. Operator Precedence:

C follows operator precedence rules, which determine the order in which operators are evaluated. Multiplication and division operators have higher precedence than addition and subtraction. Operators with higher precedence are evaluated before those with lower precedence. Parentheses can be used to override the default precedence. For example, in the expression a + b * c, b * c is evaluated first due to the higher precedence of *.

4. Associativity:

In cases where multiple operators of the same precedence appear in an expression, the associativity of the operators comes into play. Most binary operators in C are left-associative, meaning they are evaluated from left to right. For example, in the expression a - b - c, the subtraction is performed from left to right: (a - b) - c.

5. Type Coercion:

When an expression involves operands of different data types, C may perform type coercion (promotion or demotion) to ensure that the operands have compatible types. For example, if you mix integers and floating-point numbers in an expression, the integer may be implicitly converted to a floating-point number.

Here's an example of evaluating an arithmetic expression in C:


#include <stdio.h>

int main() {
    int a = 10;
    int b = 3;
    double c = 2.5;

    int result1 = a + b;         // Addition
    int result2 = a - b;         // Subtraction
    int result3 = a * b;         // Multiplication
    double result4 = a / c;      // Division with type coercion
    int result5 = a % b;         // Modulus

    printf("Result1: %d\n", result1);
    printf("Result2: %d\n", result2);
    printf("Result3: %d\n", result3);
    printf("Result4: %lf\n", result4);
    printf("Result5: %d\n", result5);

    return 0;
}

In this program, various arithmetic expressions are evaluated, and their results are printed to the console. The output will be:


Result1: 13
Result2: 7
Result3: 30
Result4: 4.000000
Result5: 1

This demonstrates how C evaluates arithmetic expressions, including type coercion when necessary.