C Language Operators: Fundamentals and Usage
Arithmetic Operators
C language provides several arithmetic operators for performing mathematical operations on variables:
+ - * / %
- All operators except % can work with both integers and floating-point numbers.
- For the / operator, if both operands are integers, integer division is performed. If at least one operand is a floating-point number, floating-point division is performed.
- The % operator requires both operands to be integers. It returns the remainder after division.
Assignment Operators
Assignment operators allow you to assign values to variables. They can be used for simple assignments or combined with other operations for concise code.
int weight = 120; // Initial weight value weight = 89; // Reassigning a new value double salary = 10000.0; salary = 20000.0; // Using assignment operator // Assignment operations can be chained: int a = 10; int x = 0; int y = 20; a = x = y + 1; // Chained assignment // For better readability, consider this alternative: x = y + 1; a = x;
Compound Assignment Operators
These operators combine assignment with another operation for more concise code:
+= -= *= /= %= >>= <<= &= |= ^=
Example:
int value = 10; value = value + 10; // Standard assignment value += 10; // Equivalent compound assignment
Relational Operators
Relational operators compare values and return a boolean result:
> >= < <= != // Tests for "not equal" == // Tests for "equal"
While these operators are straightforward, be cautious about a common pitfall: confusing == (equality comparison) with = (assignment).
Unary Operators
Overview
Unary operators operate on a single operand:
! // Logical negation - // Unary minus + // Unary plus & // Address-of sizeof // Size of operand in bytes ~ // Bitwise complement -- // Pre/post decrement ++ // Pre/post increment * // Dereference operator (type) // Type casting
Example code:
#includeint main() { int number = -10; int *pointer = NULL; printf("%d\n", !2); // Output: 0 (false) printf("%d\n", !0); // Output: 1 (true) number = -number; // Change sign pointer = &number; // Get address printf("%d\n", sizeof(number)); printf("%d\n", sizeof(int)); printf("%d\n", sizeof number); // Valid syntax printf("%d\n", sizeof int); // Error: missing operand return 0; }
sizeof with Arrays
#includevoid displayArraySize(int arr[]) { printf("%zu\n", sizeof(arr)); // Size of pointer, not array } void displayCharArraySize(char ch[]) { printf("%zu\n", sizeof(ch)); // Size of pointer, not array } int main() { int numbers[10] = {0}; char characters[10] = {0}; printf("%zu\n", sizeof(numbers)); // Size of entire array printf("%zu\n", sizeof(characters)); // Size of entire array displayArraySize(numbers); displayCharArraySize(characters); return 0; }
Increment and Decrement Operators
Pre-increment and Pre-decrement
#includeint main() { int counter = 10; int result = ++counter; // Increment first, then use // counter is now 11, result is 11 int value = 10; int output = --value; // Decrement first, then use // value is now 9, output is 9 return 0; }
Post-increment and Post-decrement
#includeint main() { int counter = 10; int result = counter++; // Use first, then increment // counter is now 11, result is 10 int value = 10; int output = value--; // Use first, then decrement // value is now 9, output is 10 return 0; }