Learning Sections show
Operators in C
Arithmetic Operators
#include<stdio.h> // Include the standard input-output library
int main() {
// Arithmetic operations
int a = 10, b = 5;
int sum = a + b; // Addition
int difference = a - b; // Subtraction
int product = a * b; // Multiplication
int quotient = a / b; // Division
int remainder = a % b; // Modulus
// Print results
printf("Sum: %d\n", sum);
printf("Difference: %d\n", difference);
printf("Product: %d\n", product);
printf("Quotient: %d\n", quotient);
printf("Remainder: %d\n", remainder);
return 0;
}
Explanation: This program demonstrates the use of arithmetic operators: addition, subtraction, multiplication, division, and modulus. It prints the results using printf
.
Relational Operators
#include<stdio.h> // Include the standard input-output library
int main() {
// Relational operations
int a = 10, b = 5;
// Check relational conditions
printf("a > b: %d\n", a > b);
printf("a < b: %d\n", a <</span> 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);
return 0;
}
Explanation: This program demonstrates the use of relational operators: greater than, less than, greater than or equal to, less than or equal to, equal to, and not equal to. It prints the results using printf
.
Logical Operators
#include<stdio.h> // Include the standard input-output library
int main() {
// Logical operations
int a = 1, b = 0;
// Check logical conditions
printf("a && b: %d\n", a && b);
printf("a || b: %d\n", a || b);
printf("!a: %d\n", !a);
return 0;
}
Explanation: This program demonstrates the use of logical operators: logical AND (&&
), logical OR (||
), and logical NOT (!
). It prints the results using printf
.
Bitwise Operators
#include<stdio.h> // Include the standard input-output library
int main() {
// Bitwise operations
int a = 5, b = 3;
// Perform bitwise operations
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);
return 0;
}
Explanation: This program demonstrates the use of bitwise operators: AND (&
), OR (|
), XOR (^
), NOT (~
), left shift (<<
), and right shift (>>
). It prints the results using printf
.
Assignment Operators
#include<stdio.h> // Include the standard input-output library
int main() {
// Assignment operations
int a = 10;
int b;
// Perform assignment operations
b = a; // Simple assignment
printf("b = a: %d\n", b);
b += 5; // Add and assign
printf("b += 5: %d\n", b);
b -= 2; // Subtract and assign
printf("b -= 2: %d\n", b);
b *= 3; // Multiply and assign
printf("b *= 3: %d\n", b);
b /= 4; // Divide and assign
printf("b /= 4: %d\n", b);
b %= 3; // Modulus and assign
printf("b %= 3: %d\n", b);
return 0;
}
Explanation: This program demonstrates the use of assignment operators: simple assignment (=
), add and assign (+=
), subtract and assign (-=
), multiply and assign (*=
), divide and assign (/=
), and modulus and assign (%=
). It prints the results using printf
.
Increment and Decrement Operators
#include<stdio.h> // Include the standard input-output library
int main() {
// Increment and decrement operations
int a = 10;
// Perform increment operations
printf("a: %d\n", a);
printf("++a: %d\n", ++a); // Pre-increment
printf("a++: %d\n", a++); // Post-increment
printf("a: %d\n", a); // Value after post-increment
// Perform decrement operations
printf("--a: %d\n", --a); // Pre-decrement
printf("a--: %d\n", a--); // Post-decrement
printf("a: %d\n", a); // Value after post-decrement
return 0;
}
Explanation: This program demonstrates the use of increment (++
) and decrement (--
) operators. It prints the results using printf
.