Back to: C Tutorials For Beginners and Professionals
Operators in C Language with Examples
In this article, I will discuss Operators in C Language with Examples. Please read our previous article discussing the Constants in C program. As part of this article, you will learn what operators are in C, their type, and when and how to use different types of operators in the C language with examples.
Operators in C Language
In C programming, operators are special symbols used for performing specific operations on one, two, or three operands and then returning a result. The operators in C can be classified into several categories based on their functionality. Here are some examples of different types of operators in C:
Arithmetic Operators in C: Used for performing mathematical calculations.
- + (Addition): Adds two operands. Example: a + b
- – (Subtraction): Subtracts the second operand from the first. Example: a – b
- * (Multiplication): Multiplies both operands. Example: a * b
- / (Division): Divides the numerator by the denominator. Example: a / b
- % (Modulus): Returns the remainder after division. Example: a % b
Here’s a simple C program that demonstrates the use of all arithmetic operators. This program will take two numbers as input and then show the results of addition, subtraction, multiplication, division, and modulus operations using these numbers.
#include <stdio.h> int main() { int num1, num2; printf("Enter two integers: "); scanf("%d %d", &num1, &num2); printf("Addition: %d + %d = %d\n", num1, num2, num1 + num2); printf("Subtraction: %d - %d = %d\n", num1, num2, num1 - num2); printf("Multiplication: %d * %d = %d\n", num1, num2, num1 * num2); // Checking for division by zero if (num2 != 0) { printf("Division: %d / %d = %d\n", num1, num2, num1 / num2); printf("Modulus: %d %% %d = %d\n", num1, num2, num1 % num2); } else { printf("Division and Modulus by zero is not allowed.\n"); } return 0; }
This program covers the following arithmetic operations:
- Addition (+): Adds two numbers.
- Subtraction (-): Subtracts the second number from the first.
- Multiplication (*): Multiplies two numbers.
- Division (/): Divides the first number by the second. It checks for division by zero, which is an illegal operation.
- Modulus (%): Finds the remainder when the first number is divided by the second. Again, it checks for division by zero.
Relational Operators in C: Used for comparing two values.
- == (Equal to): Checks if the values of two operands are equal. Example: a == b
- != (Not equal to): Checks if the values of two operands are not equal. Example: a != b
- > (Greater than): Checks if the left operand is greater than the right. Example: a > b
- < (Less than): Checks if the left operand is less than the right. Example: a < b
- >= (Greater than or equal to): Checks if the left operand is greater than or equal to the right. Example: a >= b
- <= (Less than or equal to): Checks if the left operand is less than or equal to the right. Example: a <= b
Below is a simple C program demonstrating the use of all relational operators. This program compares two integers using each of the relational operators and displays the results.
#include <stdio.h> int main() { int a, b; // Assigning some values to a and b a = 10; b = 20; // Using relational operators printf("a == b: %d\n", a == b); // Equal to printf("a != b: %d\n", a != b); // Not equal to printf("a > b: %d\n", a > b); // Greater than printf("a < b: %d\n", a < b); // Less than printf("a >= b: %d\n", a >= b); // Greater than or equal to printf("a <= b: %d\n", a <= b); // Less than or equal to return 0; }
In this program:
- a and b are two integer variables initialized with values 10 and 20, respectively.
- Each print statement uses a different relational operator to compare a and b.
- The results of these comparisons are printed on the console. Since these are boolean expressions, the result will be 1 if the condition is true and 0 if it is false.
Logical Operators in C: Used to perform logical operations.
- && (Logical AND): Returns true if both operands are true. Example: a && b
- || (Logical OR): Returns true if either of the operands is true. Example: a || b
- ! (Logical NOT): Reverses the logical state of its operand. Example: !a
Here’s an example of a C program that demonstrates the use of all logical operators (&&, ||, and !). This program will evaluate several logical expressions and print the results.
#include <stdio.h> int main() { int a = 5, b = 3; // Logical AND (&&) if (a > 0 && b > 0) { printf("Both a and b are positive numbers.\n"); } else { printf("Either a or b is not a positive number.\n"); } // Logical OR (||) if (a > 0 || b < 0) { printf("Either a is positive or b is negative (or both).\n"); } else { printf("Neither a is positive nor b is negative.\n"); } // Logical NOT (!) if (!a) { printf("a is zero.\n"); } else { printf("a is not zero.\n"); } return 0; }
In this program:
- The Logical AND && checks if both a and b are greater than 0.
- The Logical OR || checks if either a is greater than 0 or b is less than 0.
- The Logical NOT ! negates the value of a (in this case, since a is not 0, !a becomes false).
Assignment Operators in C: Used to assign values to variables.
- = (Simple Assignment): Assigns the value of the right operand to the left operand. Example: a = b
- += (Add and Assignment): Adds the right operand to the left operand and assigns the result to the left operand. Example: a += b (equivalent to a = a + b)
- -= (Subtract and Assignment): Subtracts the right operand from the left operand and assigns the result to the left operand. Example: a -= b (equivalent to a = a – b)
- *= (Multiply and Assignment): Multiplies the right operand with the left operand and assigns the result to the left operand. Example: a *= b (equivalent to a = a * b)
- /= (Divide and Assignment): Divides the left operand by the right operand and assigns the result to the left operand. Example: a /= b (equivalent to a = a / b)
- %= (Modulus and Assignment): Takes modulus using two operands and assigns the result to the left operand. Example: a %= b (equivalent to a = a % b)
Below is a simple C program demonstrating the use of all assignment operators. This program includes examples of simple assignment (=), as well as compound assignments (+=, -=, *=, /=, %=, <<=, >>=, &=, ^=, |=).
#include <stdio.h> int main() { int a, b; // Simple assignment a = 10; printf("a = %d\n", a); // Add and assignment a += 2; // equivalent to a = a + 2 printf("a += 2: %d\n", a); // Subtract and assignment a -= 2; // equivalent to a = a - 2 printf("a -= 2: %d\n", a); // Multiply and assignment a *= 2; // equivalent to a = a * 2 printf("a *= 2: %d\n", a); // Divide and assignment a /= 2; // equivalent to a = a / 2 printf("a /= 2: %d\n", a); // Modulus and assignment a %= 3; // equivalent to a = a % 3 printf("a %%= 3: %d\n", a); // Left shift and assignment a <<= 1; // equivalent to a = a << 1 printf("a <<= 1: %d\n", a); // Right shift and assignment a >>= 1; // equivalent to a = a >> 1 printf("a >>= 1: %d\n", a); // Bitwise AND and assignment b = 5; a &= b; // equivalent to a = a & b printf("a &= %d: %d\n", b, a); // Bitwise XOR and assignment a ^= b; // equivalent to a = a ^ b printf("a ^= %d: %d\n", b, a); // Bitwise OR and assignment a |= b; // equivalent to a = a | b printf("a |= %d: %d\n", b, a); return 0; }
This program assigns a value to the variable a and then uses various assignment operators to modify its value. The result of each operation is printed to the console. This way, you can see how each assignment operator affects the variable’s value. Remember that the % symbol in printf is a format specifier, so when printing the modulus assignment operator, you need to use %% to represent a single % character in the output.
Increment and Decrement Operators in C: Used to increase or decrease the value of a variable.
- ++ (Increment): Increases the value of the operand by 1. Example: a++ (increments a by 1)
- — (Decrement): Decreases the value of the operand by 1. Example: a– (decrements a by 1)
Below is a C program that demonstrates the use of increment and decrement operators. These operators are used to increase or decrease the value of a variable by one. In C, there are two types of increment and decrement operators: postfix (num++, num–) and prefix (++num, –num).
#include <stdio.h> int main() { int a = 5, b = 5; int result; // Postfix Increment result = a++; // result = 5, a = 6 printf("After Postfix Increment: a = %d, result = %d\n", a, result); // Resetting 'a' to 5 a = 5; // Prefix Increment result = ++a; // a = 6, result = 6 printf("After Prefix Increment: a = %d, result = %d\n", a, result); // Resetting 'a' to 5 a = 5; // Postfix Decrement result = b--; // result = 5, b = 4 printf("After Postfix Decrement: b = %d, result = %d\n", b, result); // Resetting 'b' to 5 b = 5; // Prefix Decrement result = --b; // b = 4, result = 4 printf("After Prefix Decrement: b = %d, result = %d\n", b, result); return 0; }
This program does the following:
- Initializes two integer variables, a and b, with the value 5.
- Demonstrates postfix increment (a++) and prefix increment (++a) and prints the values of a and result after each operation.
- Resets a to 5 before demonstrating the next operation.
- Demonstrates postfix decrement (b–) and prefix decrement (–b) and prints the values of b and result after each operation.
- Resets b to 5 before demonstrating the next operation.
- When you run this program, it will display the values of a, b, and the result after each increment and decrement operation, showcasing the difference between the postfix and prefix forms of these operators.
Bitwise Operators in C: Used for manipulating data at the bit level.
- & (Bitwise AND): Performs bitwise AND on two operands. Example: a & b
- | (Bitwise OR): Performs bitwise OR on two operands. Example: a | b
- ^ (Bitwise XOR): Performs bitwise XOR on two operands. Example: a ^ b
- ~ (Bitwise NOT): Performs bitwise NOT on the operand. Example: ~a
- << (Left Shift): Shifts the bits of the first operand to the left. Example: a << 2
- >> (Right Shift): Shifts the bits of the first operand to the right. Example: a >> 2
Here’s a simple C program demonstrating the use of bitwise operators. Bitwise operators are used for manipulating a data type at the bit level. The program will perform different bitwise operations on two integers in this example.
#include <stdio.h> int main() { unsigned int a = 12; // Binary: 1100 unsigned int b = 25; // Binary: 11001 unsigned int result; // Using bitwise AND operator result = a & b; printf("a & b: %u\n", result); // Using bitwise OR operator result = a | b; printf("a | b: %u\n", result); // Using bitwise XOR operator result = a ^ b; printf("a ^ b: %u\n", result); // Using bitwise NOT operator result = ~a; printf("~a: %u\n", result); // Using bitwise left shift operator result = a << 2; printf("a << 2: %u\n", result); // Using bitwise right shift operator result = a >> 2; printf("a >> 2: %u\n", result); return 0; }
In this program:
- a and b are two unsigned integers with values 12 and 25, respectively.
- The program applies each bitwise operator (&, |, ^, ~, <<, >>) to these numbers and prints the result.
- Remember that the ~ (bitwise NOT) operator inverts all bits of its operand, so the result can appear large because it converts to an unsigned format.
Ternary Operator (Conditional Operator) in C: Used as a shorthand for an if-else condition.
- ?: (Ternary operator): condition ? expression1 : expression2
The ternary operator in C, often written as ?:, is a shorthand for a simple if-else statement. It takes three operands and is used to evaluate a boolean expression and return one of two values depending on whether the expression is true or false. Here is an example program that demonstrates the use of the ternary operator:
#include <stdio.h> int main() { int a = 10, b = 20; int max; // Using the ternary operator to find the maximum of two numbers max = (a > b) ? a : b; printf("The maximum of %d and %d is %d\n", a, b, max); return 0; }
In this program:
- The expression (a > b) is evaluated.
- If it is true (i.e., if a is greater than b), max is assigned the value of a.
- If it is false (i.e., b is greater than or equal to a), max is assigned the value of b.
Special Operators in C Language:
- sizeof() (Returns the size of a variable)
- & (Returns the address of a variable)
- * (Pointer to a variable)
- , (Comma operator)
- -> (Member selection operator for pointers)
Special operators in C language include the comma operator, sizeof operator, and pointer operators (* and &). Here’s a simple C program that demonstrates the usage of these special operators:
#include <stdio.h> int main() { int a, b, c; int *ptr; // Pointer to an integer // Comma operator a = 10, b = 20, c = 30; printf("Using Comma Operator: a = %d, b = %d, c = %d\n", a, b, c); // sizeof operator printf("Size of int: %zu bytes\n", sizeof(int)); printf("Size of a: %zu bytes\n", sizeof(a)); // Pointer operators (* and &) ptr = &a; // & operator gets the address of a printf("Address of a: %p\n", (void *)ptr); printf("Value at address of a: %d\n", *ptr); // * operator dereferences the pointer return 0; }
In this program:
- Comma Operator: Used to separate multiple expressions. In this example, it’s used to assign values to a, b, and c.
- sizeof Operator: Determines the size of a data type or variable. This program prints the size of an int and the size of the variable a.
- Pointer Operators:
- & (Address of Operator): Gets the address of a variable.
- * (Pointer Dereference Operator): Accesses the value at a given memory address.
These operators play a critical role in C programming, allowing for complex expressions and computations, control flow management, and memory manipulation.
In the next article, I am going to discuss Unary Operators in C Programming Language with Examples. Here, in this article, I try to explain Operators in C Programming Language with examples, and I hope you enjoy this Operators in C Language article. Please give your feedback and suggestions about this article.