Fundamentals 12 min read

Master C Operators: From Arithmetic to sizeof – A Complete Guide

This comprehensive guide explains C language operators—including arithmetic, relational, logical, assignment, comma, and sizeof—detailing their types, precedence, associativity, and usage with clear examples and code snippets to help you write correct and efficient expressions.

AI Cyberspace
AI Cyberspace
AI Cyberspace
Master C Operators: From Arithmetic to sizeof – A Complete Guide

Overview

Operators (also called operation symbols) perform calculations on program code. They are classified by operation type (arithmetic, relational, logical, assignment, comma, sizeof) and by operand count (unary, binary, ternary).

Operands are the data items (constants, variables, functions) on which operators act.

Expressions are formed by connecting operands with operators; they are the basic units for evaluation.

Arithmetic Operators

Arithmetic operators perform mathematical calculations.

Examples of increment/decrement operators:

i++ : returns the current value, then increments i (i = i + 1).

++i : increments i first, then returns the new value.

i-- : returns the current value, then decrements i (i = i - 1).

--i : decrements i first, then returns the new value.

NOTE :

The operand of the remainder operator must be an integer.

Increment and decrement operators can only be applied to variables, not to constants or expressions.

int main(){
    int i = 0;
    printf("i = %d
", i++); // prints 0, then i becomes 1
    printf("After i++, i = %d
", i);
    return 0;
}

Relational Operators

Relational operators determine the relationship between two operands and always yield 0 (false) or 1 (true). In C, there is no built‑in bool type; 0 represents false and any non‑zero value represents true. C++ introduces bool for readability but still maps false to 0 and true to 1.

Operands can be constants, variables, functions, or any expression (arithmetic, relational, logical, assignment, or character).

a < b
a + b != c + d
'a' > 'b'

Example : Using relational operators to implement bubble sort.

#include <stdlib.h>
#define ARRAY_LENGTH 8
int main(){
    int a[ARRAY_LENGTH] = {0};
    // ... (populate array, sort using > operator) ...
    return 0;
}

Logical Operators

Logical operators evaluate the truth of operand values (0 = false, non‑zero = true).

0 < x <= 1 // 0 < x <= 1 x != 0 && y != 0 // both x and y are non‑zero x > 1 || x < -1 // x is greater than 1 or less than -1

NOTE :

Logical NOT (!), AND (&&), and OR (||) operate on the integer representation of truth.

When multiple operator types appear, evaluation order is arithmetic → relational → logical.

int a = 11, b = 0;
printf("a && b = %d
", a && b);
printf("a || b = %d
", a || b);

Assignment Operators

Assignment operators store a value into a variable. They include the basic = and compound forms such as +=, -=, *=, /=, >>=, <<=.

Examples: a += b // equivalent to a = a + b a %= b + 2 // equivalent to a = a % (b + 2) a *= x - y // equivalent to a = a * (x - y)

Assignment expressions evaluate to the value assigned, and the left side must be a variable.

int a = 5;
a += 3; // a becomes 8

Comma Operator

The comma operator evaluates a sequence of expressions from left to right and yields the value of the last expression.

int a, b, c, d;
d = (a = 1, b = a + 1, c = b + 1);
printf("a = %d, b = %d, c = %d, d = %d
", a, b, c, d);

sizeof Operator

The sizeof operator returns the size in bytes of a data type or variable.

printf("sizeof(char) = %d
", sizeof(char));
printf("sizeof(int) = %d
", sizeof(int));
printf("sizeof(long int) = %d
", sizeof(long int));
printf("sizeof(float) = %d
", sizeof(float));
printf("sizeof(double) = %d
", sizeof(double));
printf("sizeof(stringA) = %d
", sizeof(stringA)/sizeof(char));

These examples demonstrate how to query memory usage for primitive types and character arrays.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Code ExamplesC programmingoperatorsExpression EvaluationOperator Precedence
AI Cyberspace
Written by

AI Cyberspace

AI, big data, cloud computing, and networking.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.