- C Programming Tutorial
- C - Home
- Basics of C
- C - Introduction
- C - Features
- C - Basics
- C - History
- C - Structure of C Program
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - String Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
C Programming - C Operators
![]() Share with a Friend |
C Programming - C Operators
C Operators
Operators in C are special symbols or keywords used to perform specific operations on variables or values. C supports a rich set of operators for various operations such as arithmetic, logical, relational, bitwise, and more.
Types of Operators
- Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations.
| Operator | Description | Example |
|---|---|---|
|
+ |
Addition |
a + b |
|
- |
Subtraction |
a - b |
|
* |
Multiplication |
a * b |
|
/ |
Division |
a / b |
|
% |
Modulus (remainder) |
a % b |
- Relational (Comparison) Operators
Relational operators are used to compare two values.
| Operator | Description | Example |
|---|---|---|
|
== |
Equal to |
a == b |
|
!= |
Not equal to |
a != b |
|
< |
Less than |
a < b |
|
> |
Greater than |
a > b |
|
<= |
Less than or equal to |
a <= b |
|
>= |
Greater than or equal to |
a >= b |
- Logical Operators
Logical operators are used to perform logical operations.
| Operator | Description | Example |
|---|---|---|
|
&& |
Logical AND |
(a > b) && (c > d) |
|
` |
` |
|
|
! |
Logical NOT |
!(a > b) |
- Bitwise Operators
Bitwise operators work on binary representations of integers.
| Operator | Description | Example |
|---|---|---|
|
Operator |
Description |
Example |
|
& |
Bitwise AND |
a & b |
|
` |
` |
Bitwise OR |
|
^ |
Bitwise XOR |
a ^ b |
|
~ |
Bitwise NOT |
~a |
|
<< |
Left shift |
a << 2 |
|
>> |
Right shift |
a >> 2 |
- Assignment Operators
Assignment operators are used to assign values to variables.
| Operator | Description | Example |
|---|---|---|
|
= |
Assign value |
a = 5 |
|
+= |
Add and assign |
a += 5 |
|
-= |
Subtract and assign |
a -= 5 |
|
*= |
Multiply and assign |
a *= 5 |
|
/= |
Divide and assign |
a /= 5 |
|
%= |
Modulus and assign |
a %= 5 |
- Increment and Decrement Operators
These operators increase or decrease the value of a variable by 1.
| Operator | Description | Example |
|---|---|---|
|
++ |
Increment |
++a, a++ |
|
-- |
Decrement |
--a, a-- |
- Conditional (Ternary) Operator
The ternary operator is a shorthand for if-else conditions.
| Operator | Description | Example |
|---|---|---|
|
? : |
Ternary conditional |
x = (a > b) ? a : b; |
- Special Operators
- Sizeof Operator: Used to find the size of a data type or variable.
C
int a = 5;
printf("%lu", sizeof(a)); // Outputs size of 'a' in bytes
- Comma Operator: Used to separate expressions.
C
int a, b = (1, 2); // b gets the value 2
- Pointer Operators:
| Operator | Description | Example |
|---|---|---|
|
* |
Dereference operator |
*ptr |
|
& |
Address-of operator |
&var |
Operator Precedence and Associativity
Precedence: Determines the order of evaluation of operators.
Associativity: Determines the direction of evaluation (left-to-right or right-to-left).
| Precedence | Operators | Associativity |
|---|---|---|
|
Highest |
(), [], ->, . |
Left-to-right |
|
++, -- (Postfix) |
Left-to-right |
|
|
+, -, *, /, % |
Left-to-right |
|
|
Lowest |
=, +=, -= (Assignment) |
Right-to-left |
Examples
Arithmetic Example
C
int a = 10, b = 20;
printf("Sum: %d", a + b); // Outputs: Sum: 30
Relational Example
C
if (a > b) {
printf("a is greater");
} else {
printf("b is greater");
}
Logical Example
C
if ((a > b) && (b > 0)) {
printf("Condition satisfied");
}
Bitwise Example
C
int a = 5; // Binary: 0101
int b = 3; // Binary: 0011
printf("Bitwise AND: %d", a & b); // Outputs: 1 (0001)
Key Points
- Use parentheses to ensure clarity and desired evaluation order.
- Be cautious with operator precedence to avoid unintended results.
- Understand the data type compatibility for bitwise and arithmetic operations.
- Use logical operators for condition checks and bitwise operators for low-level manipulations.
