Aim:
To write a C program to demonstrate the usage of various types of operators in C.
Algorithm:
Step 1: Declare and initialize necessary integer variables (e.g., a, b, x, y, result, etc.).
Step 2: Perform and display results of Arithmetic Operators: +, -, *, /, %.
Step 3: Evaluate and display Relational Operators: >, <, >=, <=.
Step 4: Evaluate and display Equality Operators: ==, !=.
Step 5: Evaluate and display Logical Operators: &&, ||, !.
Step 6: Apply and display Unary Operators: pre-increment (++a), pre-decrement (--b).
Step 7: Use Ternary (Conditional) Operator to find and display the maximum of two values.
Step 8: Perform and display results of Bitwise Operators: &, |, ^, ~, <<, >>.
Step 9: Demonstrate Assignment Operators: =, +=, -=, *=, /=.
Step 10: Use the Comma Operator to evaluate multiple expressions and return the last value.
Step 11: Use and display the result of the sizeof Operator on various data types and variables.
Code:
#include <stdio.h>
int main() {
int a = 10, b = 5, result;
int x = 0, y = 1;
int num = 7;
int temp;
printf("=== Arithmetic Operators ===\n");
printf("a + b = %d\n", a + b);
printf("a - b = %d\n", a - b);
printf("a * b = %d\n", a * b);
printf("a / b = %d\n", a / b);
printf("a %% b = %d\n", a % b);
printf("\n=== Relational Operators ===\n");
printf("a > b: %d\n", a > b);
printf("a < b: %d\n", a < b);
printf("a >= b: %d\n", a >= b);
printf("a <= b: %d\n", a <= b);
printf("\n=== Equality Operators ===\n");
printf("a == b: %d\n", a == b);
printf("a != b: %d\n", a != b);
printf("\n=== Logical Operators ===\n");
printf("x && y = %d\n", x && y); // false && true = false
printf("x || y = %d\n", x || y); // false || true = true
printf("!x = %d\n", !x); // !false = true
printf("\n=== Unary Operators ===\n");
printf("++a = %d\n", ++a);
printf("--b = %d\n", --b);
printf("\n=== Conditional (Ternary) Operator ===\n");
result = (a > b) ? a : b;
printf("Max of a and b is: %d\n", result);
printf("\n=== Bitwise Operators ===\n");
printf("a & b = %d\n", a & b);
printf("a | b = %d\n", a | b);
printf("a ^ b = %d\n", a ^ b);
printf("~a = %d\n", ~a);
printf("a << 1 = %d\n", a << 1);
printf("a >> 1 = %d\n", a >> 1);
printf("\n=== Assignment Operators ===\n");
result = a;
printf("result = a: %d\n", result);
result += b;
printf("result += b: %d\n", result);
result -= b;
printf("result -= b: %d\n", result);
result *= b;
printf("result *= b: %d\n", result);
result /= b;
printf("result /= b: %d\n", result);
printf("\n=== Comma Operator ===\n");
temp = (a = 3, b = 4, a + b); // a and b are updated, last value is the result
printf("Comma Operator Result: %d\n", temp);
printf("\n=== sizeof Operator ===\n");
printf("Size of int: %zu bytes\n", sizeof(int));
printf("Size of float: %zu bytes\n", sizeof(float));
printf("Size of char: %zu bytes\n", sizeof(char));
printf("Size of variable a: %zu bytes\n", sizeof(a));
return 0;