0% found this document useful (0 votes)
6 views2 pages

C Operators Explained: Types & Examples

The document is a C program that demonstrates various types of operators including arithmetic, relational, logical, assignment, unary, conditional (ternary), bitwise, comma, and sizeof operators. Each section includes example code and output to illustrate how these operators function. The program concludes with a return statement indicating successful execution.

Uploaded by

adrashmaurya23
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

C Operators Explained: Types & Examples

The document is a C program that demonstrates various types of operators including arithmetic, relational, logical, assignment, unary, conditional (ternary), bitwise, comma, and sizeof operators. Each section includes example code and output to illustrate how these operators function. The program concludes with a return statement indicating successful execution.

Uploaded by

adrashmaurya23
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#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. sizeof Operator
printf("\nsizeof Operator:\n");
printf("sizeof(a): %zu bytes\n", sizeof(a)); // Size of variable 'a'
return 0;
}

You might also like