AIM : Write a C program to demonstrate various operators: Arithmetic, Relational,
Logical, Assignment, Unary, Conditional (Ternary), Bitwise, Comma, and Size of.
REQUIREMENTS:
Any C compiler (e.g., GCC, Clang) and a text editor.
THEORY:
An operator is a symbol that tells the compiler to perform a specific mathematical or
logical manipulation. C language is rich in built-in operators that are used to perform
various operations on variables and data.
• Arithmetic Operators: Used for mathematical calculations like addition +,
subtraction -, multiplication *, division /, and modulus %.
• Relational Operators: Used to compare two operands and determine their
relationship. They include == (equal to), != (not equal to), > (greater than), < (less
than), >= (greater than or equal to), and <= (less than or equal to). The result is
either true (1) or false (0).
• Logical Operators: Used to combine or manipulate boolean expressions. They
are && (Logical AND), || (Logical OR), and ! (Logical NOT).
• Assignment Operators: Used to assign a value to a variable. The simple
assignment operator is =. Compound assignment operators combine an
arithmetic operation with assignment, such as += (add and assign) and -=
(subtract and assign).
• Unary Operators: Operators that work on a single operand. Examples include ++
(increment) and -- (decrement).
• Conditional (Ternary) Operator: A shorthand for an if-else statement. Its format
is expression1 ? expression2 : expression3. If expression1 is true, expression2 is
executed; otherwise, expression3 is executed.
• Bitwise Operators: Used to perform operations at the bit level. They include &
(Bitwise AND), | (Bitwise OR), ^ (Bitwise XOR), ~ (Bitwise NOT), << (Left shift), and
>> (Right shift).
• Comma Operator: Used to link related expressions together. The expression on
the right is evaluated after the expression on the left, and the value of the entire
expression is the value of the rightmost expression.
• sizeof Operator: A unary operator that returns the size, in bytes, of a variable or a
data type.
PROCEDURE:
1. Open your C programming environment (e.g., VS Code).
2. Create a new file and save it with a .c extension (e.g., operators.c).
3. Type the provided C code into the editor.
4. Save the file.
5. Compile and run the program to see the output.
C Code:
#include <stdio.h>
int main() {
// 1. Arithmetic Operators
int a = 5, b = 2;
printf("Arithmetic Operators:\n");
printf("a + b = %d\n", a + b); // Addition
printf("a - b = %d\n", a - b); // Subtraction
printf("a * b = %d\n", a * b); // Multiplication
printf("a / b = %d\n", a / b); // Division (integer division)
printf("a %% b = %d\n", a % b); // Modulus (remainder)
// 2. Relational Operators
printf("\nRelational Operators:\n");
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
// 3. Logical Operators
int x = 1, y = 0;
printf("\nLogical Operators:\n");
printf("x && y: %d\n", x && y); // Logical AND
printf("x || y: %d\n", x || y); // Logical OR
printf("!x: %d\n", !x); // Logical NOT
// 4. Assignment Operators
printf("\nAssignment Operators:\n");
int c = a; // Assignment
printf("c = a: %d\n", c);
c += b; // Add and assign
printf("c += b: %d\n", c);
// 5. Unary Operators
printf("\nUnary Operators:\n");
int d = 10;
printf("++d: %d\n", ++d); // Pre-increment
printf("--d: %d\n", --d); // Pre-decrement
// 6. Conditional (Ternary) Operator
printf("\nConditional (Ternary) Operator:\n");
int max = (a > b) ? a : b;
printf("Max of a and b is: %d\n", max);
// 7. Bitwise Operators
printf("\nBitwise Operators:\n");
printf("a & b = %d\n", a & b); // Bitwise AND
printf("a | b = %d\n", a | b); // Bitwise OR
printf("a ^ b = %d\n", a ^ b); // Bitwise XOR
// 8. Comma Operator
printf("\nComma Operator:\n");
int result;
result = (a = 1, b = 2, a + b);
printf("Result of (a = 1, b = 2, a + b): %d\n", result);
// 9. Size of Operator
printf("\nSize of Operator:\n");
printf("sizeof(a): %zu bytes\n", sizeof(a)); // Size of variable 'a'
return 0;
Output:
Arithmetic Operators:
a+b=7
a-b=3
a * b = 10
a/b=2
a%b=1
Relational Operators:
a == b: 0
a != b: 1
a > b: 1
a < b: 0
Logical Operators:
x && y: 0
x || y: 1
!x: 0
Assignment Operators:
c = a: 5
c += b: 7
Unary Operators:
++d: 11
--d: 10
Conditional (Ternary) Operator:
Max of a and b is: 5
Bitwise Operators:
a&b=0
a|b=7
a^b=7
Comma Operator:
Result of (a = 1, b = 2, a + b): 3
Size of Operator:
sizeof(a): 4 bytes
CONCLUSION:
The program successfully demonstrates the use of different types of operators in the C
programming language, providing a clear output for each operation.