Skip to main content

Command Palette

Search for a command to run...

Arithmetic Operators

C Programming Series - Article 2

Updated
β€’5 min read
Arithmetic Operators
S

πŸ‘‹ Hey, I'm Shreya, a Senior Software Engineer in an Leading MNC. Join me on this blog as I share insights, tips, and experiences. Let's explore these fascinating worlds together! πŸš€πŸ’» #CodeAndSecure

In the C language, mathematical operations follow specific rules that differ slightly from standard algebra, especially regarding data types and operation order. Below is a detailed compilation of notes covering Arithmetic Operators, derived directly from class examples.

1. Basic Arithmetic Operators

C provides the standard set of operators for calculation:

  • + : Addition (Sum)

  • - : Subtraction (Difference)

  • * : Multiplication (Product)

  • / : Division (Quotient)

  • % : Modulus (Remainder)

While addition, subtraction, and multiplication are straightforward, Division and Modulus have special behaviour in C.


2. The Division Operator (/)

The behaviour of the division operator depends on the data types of the operands (Int vs. Float).

Rule 1: Integer vs. Float Division

  • Int / Int = Int: The result is the quotient only. The decimal part is truncated.

  • Int / Float = Float: The result includes the decimal part.

Examples from Notes:

  • a = 5 / 2; β†’ Result is 2 (The 0.5 is discarded).

  • a = 5.0 / 2; β†’ Result is 2.5 (Float division).

  • a = 5 / 2.0; β†’ Result is 2.5.

Rule 2: Sign of the Quotient

For division, the sign of the result follows standard algebraic rules:

  • + / + = +

  • + / - = -

  • - / + = -

  • - / - = +

Examples:

  • a = -5 / 2; β†’ Result: -2

  • a = 5 / -2; β†’ Result: -2

  • a = -5 / -2; β†’ Result: 2

  • a = 2 / -5; β†’ Result: 0 (Since 2 < 5, quotient is 0).


3. The Modulus Operator (%)

The modulus operator returns the remainder of the division. The notes highlight four critical rules for using %.

Rule A: The Sign Rule

The Rule: In C, the result of a modulus operation always takes the sign of the Numerator (the left operand). The sign of the denominator is ignored.

Derivation:

  • Formula: Dividend = Divisor * Quotient + Remainder

Examples:

  1. a = 5 % 2; β†’ 1 (Standard positive)

  2. a = 5 % -2; β†’ 1 (Sign of 5 is +)

  3. a = -5 % 2; β†’ -1 (Sign of -5 is -)

  4. a = -5 % -2; β†’ -1 (Sign of -5 is -)

Rule B: The "Small Numerator" Rule

If the numerator is smaller than the denominator, the result is equal to the numerator itself.

Examples:

  • 18 % 25 β†’ 18

  • 15 % 30 β†’ 15

  • 2 % 5 β†’ 2

Rule C: The Float Restriction

The modulus operator is only valid for integers.

  • Invalid: a = 5.0 % 2; β†’ Compilation Error (Illegal use of floating point).

  • Invalid: a = 5.0 % 2.0; β†’ Compilation Error.

Rule D: Extracting the Unit Digit

Using modulo 10 is a standard trick to get the last digit of a number.

Examples:

  • 13 % 10 β†’ 3

  • 78 % 10 β†’ 8

  • 129 % 10 β†’ 9

Note: x % y = 0 only when x is a multiple of y.


4. Operator Precedence and Associativity

When multiple operators appear in one expression, C uses a Priority Table to decide the order.

Priority Levels

  1. High Priority: * , / , %

  2. Low Priority: + , -

Associativity (The Tie-Breaker)

If operators have the same priority (e.g., * and /), they are evaluated from Left to Right (L β†’ R).

Complex Evaluation Examples

1. Left-to-Right Evaluation (/ and *)

  • Expression: a = 13 / 2 * 5;

    • Step 1: 13 / 2 = 6 (Integer division).

    • Step 2: 6 * 5 = 30.

    • Result: 30.

  • Expression: a = 13 * 2 / 5;

    • Step 1: 13 * 2 = 26.

    • Step 2: 26 / 5 = 5.2 β†’ 5 (Truncated).

    • Result: 5.

2. Multiple Divisions

  • Expression: a = 21 / 5 / 2;

    • Step 1: 21 / 5 = 4.

    • Step 2: 4 / 2 = 2.

    • Result: 2.

3. Mixed Priorities

  • Expression: a = 2 + 3 * 5;

    • * has higher priority.

    • Step 1: 3 * 5 = 15.

    • Step 2: 2 + 15 = 17.

    • Result: 17.

  • Expression: a = 3 * 5 * 2 % 7;

    • All are high priority. Evaluate L β†’ R.

    • 3 * 5 = 15.

    • 15 * 2 = 30.

    • 30 % 7 = 2.

    • Result: 2.

  • Expression: a = 6 - 4 + 2 - 3;

    • All are low priority. Evaluate L β†’ R.

    • 6 - 4 = 2.

    • 2 + 2 = 4.

    • 4 - 3 = 1.

    • Result: 1.


5. Real-World Application: ATM Logic

The modulus operator is essential for checking divisibility.

Scenario: An ATM needs to check if the withdrawal amount is valid for 100-rupee notes.

  • Total in Account: 8930

  • Withdrawal Request: 310

Logic:

if (310 % 100 == 0) {
    // This block runs if remainder is 0
    give_money();
}
else {
    // This block runs because 310 % 100 = 10 (Not 0)
    printf("Error: Please enter cash in multiples of 100");
}

πŸ“š Navigate the Series

C Programming

Part 2 of 4

"Master C from the ground up! πŸš€ This series breaks down syntax, pointers, and memory management into clear, byte-sized guides. Perfect for beginners building a strong coding foundation."

Up next

Syntax and Constants

Introduction to C Programming- C Programming Series - Article 1