Apply for Zend Framework Certification Training

c



< Data Types in C Last >



Operators in C
Operators in C are symbols that tell the compiler to perform a particular action on one or more values. They are used to build expressions and carry out tasks such as calculations, comparisons, logical decisions, assigning values, manipulating bits, and working with memory.
An operator works with operands, which can be variables, constants, or expressions.
For example:
#include <stdio.h>
int main() {
    int sum = 10 + 20;
    printf("Sum = %d", sum);
    return 0;
}
Output:
Sum = 30
Here, + is the arithmetic operator, while 10 and 20 are its operands.
Classification of Operators in C
C operators can be grouped according to the type of operation they perform:
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Other/Special Operators
Operators can also be classified according to the number of operands:
Unary: Works with one operand, such as ++a
Binary: Works with two operands, such as a + b
Ternary: Works with three expressions, such as a > b ? a : b
1. Arithmetic Operators
Arithmetic operators are used to perform common mathematical operations on numeric data. They can be applied to integer as well as floating-point values.
Operator Operation Example
+ Addition a + b
- Subtraction a - b
* Multiplication a * b
/ Division a / b
% Remainder a % b
++ Increment a++
-- Decrement a--
The unary + and - operators can also be used to indicate a positive or negative value.
Example
#include <stdio.h>
int main() {
    int a = 25, b = 5;
    printf("Addition       = %d\n", a + b);
    printf("Subtraction    = %d\n", a - b);
    printf("Multiplication = %d\n", a * b);
    printf("Division       = %d\n", a / b);
    printf("Remainder      = %d\n", a % b);
    printf("Positive a     = %d\n", +a);
    printf("Negative a     = %d\n", -a);
    printf("Post-increment = %d\n", a++);
    printf("Post-decrement = %d\n", a--);
    return 0;
}
Output:
Addition       = 30
Subtraction    = 20
Multiplication = 125
Division       = 5
Remainder      = 0
Positive a     = 25
Negative a     = -25
Post-increment = 25
Post-decrement = 26
Note: When both operands are integers, / performs integer division. For example, 7 / 2 gives 3, not 3.5.
2. Relational Operators
Relational operators are used when two values need to be compared. They are commonly used with if, while, for, and other decision-making statements.
The result of a comparison in C is:
1 → condition is true
0 → condition is false
Operator Meaning Example
< Less than a < b
> Greater than a > b
<= Less than or equal to a <= b
>= Greater than or equal to a >= b
== Equal to a == b
!= Not equal to a != b
Example
#include <stdio.h>
int main() {
    int a = 25, b = 5;
    printf("a < b  = %d\n", a < b);
    printf("a > b  = %d\n", a > b);
    printf("a <= b = %d\n", a <= b);
    printf("a >= b = %d\n", a >= b);
    printf("a == b = %d\n", a == b);
    printf("a != b = %d\n", a != b);
    return 0;
}
Output:
a < b  = 0
a > b  = 1
a <= b = 0
a >= b = 1
a == b = 0
a != b = 1
Here, 1 represents true and 0 represents false.
3. Logical Operators
Logical operators are useful when a program needs to combine or reverse conditions. They are frequently used in decision-making and loop conditions.
C provides three logical operators:
Operator Name Purpose
&& Logical AND True only when both conditions are true
|| Logical OR True when at least one condition is true
! Logical NOT Reverses the logical result
Example
#include <stdio.h>
int main() {
    int a = 25, b = 5;
    printf("a && b = %d\n", a && b);
    printf("a || b = %d\n", a || b);
    printf("!a     = %d\n", !a);
    return 0;
}
Output:
a && b = 1
a || b = 1
!a     = 0
Since both a and b contain non-zero values, they are considered true. Therefore, a && b and a || b produce 1.
4. Bitwise Operators
Bitwise operators work directly with the individual bits of integer values. They are particularly important in system programming, embedded systems, device programming, flags, masks, and other low-level applications.
Operator Name Example
& Bitwise AND a & b
` ` Bitwise OR a | b
^ Bitwise XOR a ^ b
~ Bitwise Complement ~a
<< Left Shift a << b
>> Right Shift a >> b
Example
#include <stdio.h>
int main() {
    int a = 25, b = 5;
    printf("a & b  = %d\n", a & b);
    printf("a | b  = %d\n", a | b);
    printf("a ^ b  = %d\n", a ^ b);
    printf("~a     = %d\n", ~a);
    printf("a >> b = %d\n", a >> b);
    printf("a << b = %d\n", a << b);
    return 0;
}
Output:
a & b  = 1
a | b  = 29
a ^ b  = 28
~a     = -26
a >> b = 0
a << b = 800
For example, shifting 25 five positions to the left produces 800 on typical systems:
25 << 5 = 25 × 2^5 = 800
5. Assignment Operators
Assignment operators store a value in a variable. The basic assignment operator is =, while compound assignment operators combine an operation with assignment.
For example:
a += b;
is equivalent to:
a = a + b;
Assignment Operator Table
Operator Meaning Equivalent Form
= Assign a = b
+= Add and assign a = a + b
-= Subtract and assign a = a - b
*= Multiply and assign a = a * b
/= Divide and assign a = a / b
%= Modulus and assign a = a % b
&= Bitwise AND and assign a = a & b
` =` Bitwise OR and assign `a = a b`
^= Bitwise XOR and assign a = a ^ b
<<= Left shift and assign a = a << b
>>= Right shift and assign a = a >> b
Example
#include <stdio.h>
int main() {
    int a = 25, b = 5;
    a = b;
    printf("a = b   : %d\n", a);
    a += b;
    printf("a += b  : %d\n", a);
    a -= b;
    printf("a -= b  : %d\n", a);
    a *= b;
    printf("a *= b  : %d\n", a);
    a /= b;
    printf("a /= b  : %d\n", a);
    a %= b;
    printf("a %%= b  : %d\n", a);
    a |= b;
    printf("a |= b   : %d\n", a);
    a ^= b;
    printf("a ^= b   : %d\n", a);
    return 0;
}
Output:
a = b   : 5
a += b  : 10
a -= b  : 5
a *= b  : 25
a /= b  : 5
a %= b  : 0
a |= b   : 5
a ^= b   : 0
Compound assignment operators make expressions shorter and can make repetitive calculations easier to read.
Other Important Operators in C
C also provides several operators for special purposes such as determining memory size, accessing structure members, converting data types, and working with pointers.
6. sizeof Operator
The sizeof operator determines the amount of memory occupied by a data type or object.
Syntax
sizeof(operand)
Example:
int num = 10;
printf("%zu", sizeof(num));
On a common system where int occupies 4 bytes, the output will be:
4
The exact size of a data type can vary depending on the compiler and system.
7. Comma Operator
The comma operator allows multiple expressions to be evaluated from left to right. The value of the entire expression is the value of the last expression.
Syntax
expression1, expression2
Example
int a;
a = (10, 20, 30);
After execution, a contains:
30
8. Conditional Operator
The conditional operator ?: is the only ternary operator in C. It provides a compact way to choose between two values based on a condition.
Syntax
condition ? expression1 : expression2;
If the condition is true, expression1 is selected; otherwise, expression2 is selected.
Example
int a = 10, b = 20;
int largest = (a > b) ? a : b;
Here, largest will contain 20.
9. Member Access Operators
The . and -> operators are used to access members of structures and unions.
Dot Operator
The dot operator is used with a structure or union variable.
structure_variable.member;
Arrow Operator
The arrow operator is used when a pointer refers to a structure or union.
structure_pointer->member;
Example:
struct Student {
    int age;
};
struct Student s;
struct Student *ptr = &s;
s.age = 20;
ptr->age = 21;
10. Type Cast Operator
A cast operator explicitly converts a value from one data type to another.
Syntax
(new_type) value;
Example
float result;
result = (float)10 / 3;
The cast changes 10 into a floating-point value before division.
Another example:
int num = 10;
printf("%.2f", (float)num);
Output:
10.00
11. Address-of and Dereference Operators
Pointers use two important operators:
& → obtains the memory address of a variable
* → accesses the value stored at an address
Example
#include <stdio.h>
int main() {
    int num = 10;
    int *ptr = &num;
    printf("Value of num = %d\n", num);
    printf("Address of num = %p\n", (void *)&num);
    printf("Value using pointer = %d\n", *ptr);
    return 0;
}
Output:
Value of num = 10
Address of num = 0x7ffdb58c037c
Value using pointer = 10
The actual memory address will normally be different each time the program runs.
Complete Example of Special Operators
#include <stdio.h>
int main() {
    int num = 10;
    int *ptr = &num;
    printf("Size of num = %zu bytes\n", sizeof(num));
    printf("Address of num = %p\n", (void *)&num);
    printf("Value through pointer = %d\n", *ptr);
    printf("Conditional result = %d\n", (10 < 5) ? 10 : 20);
    printf("Converted value = %.2f\n", (float)num);
    return 0;
}
Possible Output:
Size of num = 4 bytes
Address of num = 0x7ffdb58c037c
Value through pointer = 10
Conditional result = 20
Converted value = 10.00
The memory address shown in the output is only an example and can change between executions.
Quick Summary
Category                         Main Operators                                  Common Purpose
Arithmetic                       + - * / % ++ --                                 Mathematical calculations
Relational                        < > <= >= == !=                             Comparing values
Logical `&&
!` Combining conditions
Bitwise                            & | ^ ~ << >>                                  Bit-level operations
Assignment                      = += -= *= /= %=                           Storing/updating values
Conditional                      ?:                                                      Selecting one of two values
Size                                sizeof                                                 Finding memory size
Comma                           ,                                                        Evaluating multiple expressions
Member Access                . ->                                                    Accessing structure/union members
Cast (type) Explicit type conversion
Pointer & * Address and memory access
Key Points to Remember
Operators are used to create expressions and perform operations in C.
Arithmetic operators are mainly used for calculations.
Relational operators compare two values and produce 0 or 1.
Logical operators combine conditions.
Bitwise operators manipulate individual bits.
Assignment operators store or update values in variables.
The conditional operator ?: is useful for short if-else decisions.
sizeof helps determine the memory occupied by a type or object.
& obtains an address, while * can access the value stored at that address.
The exact size of data types and memory addresses depends on the system and compiler.

< Data Types in C Last >



Ask a question



  • Question:
    {{questionlistdata.blog_question_description}}
    • Answer:
      {{answer.blog_answer_description  }}
    Replay to Question


Back to Top