Unary Operators
Unary operators operate on single operands and include:
- ! (logical NOT)
- ++ (increment)
- -- (decrement)
- & (address-of)
-
- (dereference)
-
- (unary plus)
-
- (unary minus)
- ~ (bitwise NOT)
- sizeof
- (type) (type cast)
Increment and Decrement Operators
Prefix Increment
int counter = 10;
int result = ++counter;
printf("counter=%d result=%d\n", counter, result);
Behavior: First increment, then use
- Original value: counter = 10
- First increment: counter becomes 11
- Then assign to result: result = 11
- Final values: counter = 11, result = 11
Equivalent code:
int counter = 10;
counter = counter + 1;
result = counter;
printf("counter=%d result=%d\n", counter, result);
Postfix Increment
int counter = 10;
int result = counter++;
printf("counter=%d result=%d\n", counter, result);
Behavior: First use, then increment
- Original value: counter = 10
- First assign to result: result = 10
- Then increment: counter becomes 11
- Final values: counter = 11, result = 10
Equivalent code:
int counter = 10;
int result = counter;
counter = counter + 1;
printf("counter=%d result=%d\n", counter, result);
Prefix Decrement
int value = 10;
int output = --value;
printf("value=%d output=%d\n", value, output);
Behavior: First decrement, then use
- Result: value = 9, output = 9
Postfix Decrement
int value = 10;
int output = value--;
printf("value=%d output=%d\n", value, output);
Behavior: First use, then decrement
- Result: value = 9, output = 10
Type Casting
int integer_value = 3.14; // Warning: type mismatch
To eliminate warnings and explicitly convert types:
int integer_value = (int)3.14; // Extracts integer part only
sizeof Operator
The sizeof operator determines the size of types or variables in bytes:
int data[] = {1, 2, 3, 4, 5};
int element_count = sizeof(data) / sizeof(data[0]);
sizeof(data): Total memory occupied by arraysizeof(data[0]): Memory occupied by single element
Comma Expressions
Comma expressions consist of multiple expressions separated by commas:
expr1, expr2, expr3, ... exprN
Evaluation proceeds left to right, with the final expression's result becoming the overall result:
int x = 1;
int y = 2;
int z = (x>y, x=y+10, x, y=x+1);
Subscript and Function Call Operators
Subscript Operator [ ]
Operands: array name + index value
int numbers[10]; // Array declaration
numbers[9] = 100; // Using subscript operator
// Operands: numbers and 9
Function Call Operator ( )
int find_maximum(int a, int b);
find_maximum(30, 40);
Structure Member Access Operators
Structure Declaration
struct StructureName {
member_list
} variable_list;
Example: Student structure
struct Student {
char name[20];
char gender[5];
int age;
char student_id[20];
};
Structure Variable Definition and Initialization
// Variable definition
struct Coordinate {
int x;
int y;
} point1;
struct Coordinate point2;
// Initialization
struct Coordinate point3 = {15, 25};
struct Person {
char name[15];
int age;
};
struct Person person1 = {"John Doe", 25};
struct Person person2 = {.age=30, .name="Jane Smith"};
// Nested structures
struct TreeNode {
int value;
struct Coordinate coord;
struct TreeNode* next;
} node1 = {15, {5, 8}, NULL};
struct TreeNode node2 = {25, {8, 12}, NULL};
Direct Member Access
Direct member access uses the dot operator (.):
#include <stdio.h>
struct Coordinate {
int x;
int y;
} position = {3, 7};
int main() {
printf("x: %d y: %d\n", position.x, position.y);
return 0;
}
Syntax: structure_variable.member_name
Operator Properties
Precedence
Operator precedence determines evaluation order when multiple operators appear in an expression.
Associativity
When operators have equal precedence, associativity determines evaluation order:
- Left-associative: Evaluate left to right (most operators)
- Right-associative: Evaluate right to left (assignment operators)
Expression Evaluation
Integer Promotion
C language integer arithmetic operations typically use atleast default integer type precision. Character and short integer operands are converted to regular integers before use.
Promotion rules:
- Signed integers: Extend using sign bit
- Unsigned integers: Extend with zero padding
Example:
int main() {
unsigned char a = 5;
unsigned char b = 125;
unsigned char c = a + b;
printf("%d\n", c);
return 0;
}
Note: Computers calculate using two's complement but display original values.
Arithmetic Conversion
When operands have different types, conversion follows standard arithmetic conversion hierarchy to ensure compatible operations.