1. Write a simple program that prints the results of all the operators available in C (including pre/ post increment , bitwise and/or/not , etc.). Read required operand values from standard input.
C Program Code
#include <stdio.h> // Required for standard input/output functions like printf and scanf
int main() {
// Declare variables to store integer operands
int a, b;
// --- Input from User ---
printf("Enter two integer values for operands (e.g., 10 3): ");
// Read two integers from standard input and store them in 'a' and 'b'
if (scanf("%d %d", &a, &b) != 2) {
printf("Invalid input. Please enter two integers.\n");
return 1; // Indicate an error
}
printf("\n--- Arithmetic Operators ---\n");
printf("a = %d, b = %d\n", a, b);
printf("Addition (a + b): %d\n", a + b); // Sum of a and b
printf("Subtraction (a - b): %d\n", a - b); // Difference of a and b
printf("Multiplication (a * b): %d\n", a * b); // Product of a and b
// Division: Check for division by zero to prevent runtime errors
if (b != 0) {
printf("Division (a / b): %d\n", a / b); // Integer division of a by b
printf("Modulus (a %% b): %d\n", a % b); // Remainder of a divided by b (use %% to print %)
} else {
printf("Division and Modulus by zero are undefined.\n");
}
printf("\n--- Relational Operators (Result is 0 for false, 1 for true) ---\n");
printf("a == b: %d\n", a == b); // True if a is equal to b
printf("a != b: %d\n", a != b); // True if a is not equal to b
printf("a > b: %d\n", a > b); // True if a is greater than b
printf("a < b: %d\n", a < b); // True if a is less than b
printf("a >= b: %d\n", a >= b); // True if a is greater than or equal to b
printf("a <= b: %d\n", a <= b); // True if a is less than or equal to b
printf("\n--- Logical Operators (Result is 0 for false, 1 for true) ---\n");
// Assuming non-zero values are true, zero is false for logical operations
printf("(a > 0 && b > 0): %d\n", (a > 0 && b > 0)); // Logical AND: True if both conditions are true
printf("(a > 0 || b < 0): %d\n", (a > 0 || b < 0)); // Logical OR: True if at least one condition is true
printf("!(a == b): %d\n", !(a == b)); // Logical NOT: Inverts the boolean value
printf("\n--- Bitwise Operators (Applied to integer bits) ---\n");
printf("a = %d (Binary: ", a);
for (int i = 7; i >= 0; i--) { // Print first 8 bits for demonstration
printf("%d", (a >> i) & 1);
}
printf(")\n");
printf("b = %d (Binary: ", b);
for (int i = 7; i >= 0; i--) { // Print first 8 bits for demonstration
printf("%d", (b >> i) & 1);
}
printf(")\n");
printf("Bitwise AND (a & b): %d\n", a & b); // Sets each bit to 1 if both bits are 1
printf("Bitwise OR (a | b): %d\n", a | b); // Sets each bit to 1 if either of the bits is 1
printf("Bitwise XOR (a ^ b): %d\n", a ^ b); // Sets each bit to 1 if only one of the bits is 1
printf("Bitwise NOT (~a): %d\n", ~a); // Inverts all bits (one's complement)
printf("Left Shift (a << 2): %d\n", a << 2); // Shifts bits of a to the left by 2 (multiplies by 2^2)
printf("Right Shift (b >> 1): %d\n", b >> 1); // Shifts bits of b to the right by 1 (divides by 2^1)
printf("\n--- Assignment Operators ---\n");
int c = a; // Simple assignment
printf("c = a: %d\n", c);
c += b; // Equivalent to c = c + b
printf("c += b (c becomes %d): %d\n", c, c);
c = a; // Reset c for next operation
c -= b; // Equivalent to c = c - b
printf("c -= b (c becomes %d): %d\n", c, c);
c = a; // Reset c
c *= b; // Equivalent to c = c * b
printf("c *= b (c becomes %d): %d\n", c, c);
c = a; // Reset c
if (b != 0) {
c /= b; // Equivalent to c = c / b
printf("c /= b (c becomes %d): %d\n", c, c);
c = a; // Reset c
c %= b; // Equivalent to c = c %% b
printf("c %%= b (c becomes %d): %d\n", c, c);
} else {
printf("Compound assignment with division/modulus by zero skipped.\n");
}
printf("\n--- Increment/Decrement Operators ---\n");
int x = a; // Use a copy to show effects clearly
printf("Original x = %d\n", x);
printf("Pre-increment (++x): %d (x is now %d)\n", ++x, x); // Increments x, then uses the new value
x = a; // Reset x
printf("Post-increment (x++): %d (x is now %d)\n", x++, x); // Uses x's current value, then increments x
x = a; // Reset x
printf("Pre-decrement (--x): %d (x is now %d)\n", --x, x); // Decrements x, then uses the new value
x = a; // Reset x
printf("Post-decrement (x--): %d (x is now %d)\n", x--, x); // Uses x's current value, then decrements x
printf("\n--- Ternary (Conditional) Operator ---\n");
printf("Max of a and b: %d\n", (a > b) ? a : b); // If a > b is true, result is a, else b
return 0; // Indicate successful execution
}
Compilation and Execution (Command Prompt)
# Navigate to your Week1 folder
cd ProgrammingProjects/C_Programming/Week1
# Compile the program
gcc operators.c -o operators
# Run the program
./operators
Sample Output (for input `10 3`)
Enter two integer values for operands (e.g., 10 3): 10 3
--- Arithmetic Operators ---
a = 10, b = 3
Addition (a + b): 13
Subtraction (a - b): 7
Multiplication (a * b): 30
Division (a / b): 3
Modulus (a % b): 1
--- Relational Operators (Result is 0 for false, 1 for true) ---
a == b: 0
a != b: 1
a > b: 1
a < b: 0
a >= b: 1
a <= b: 0
--- Logical Operators (Result is 0 for false, 1 for true) ---
(a > 0 && b > 0): 1
(a > 0 || b < 0): 1
!(a == b): 1
--- Bitwise Operators (Applied to integer bits) ---
a = 10 (Binary: 00001010)
b = 3 (Binary: 00000011)
Bitwise AND (a & b): 2
Bitwise OR (a | b): 11
Bitwise XOR (a ^ b): 9
Bitwise NOT (~a): -11
Left Shift (a << 2): 40
Right Shift (b >> 1): 1
--- Assignment Operators ---
c = a: 10
c += b (c becomes 13): 13
c -= b (c becomes 7): 7
c *= b (c becomes 30): 30
c /= b (c becomes 3): 3
c %= b (c becomes 1): 1
--- Increment/Decrement Operators ---
Original x = 10
Pre-increment (++x): 11 (x is now 11)
Post-increment (x++): 10 (x is now 11)
Pre-decrement (--x): 9 (x is now 9)
Post-decrement (x--): 10 (x is now 9)
--- Ternary (Conditional) Operator ---
Max of a and b: 10
Explanation of Code Snippets
`#include <stdio.h>`
#include <stdio.h>
This is a preprocessor directive that includes the standard input/output library. It grants access to functions like printf()
(for printing output to the console) and scanf()
(for reading input from the console). Without this, the compiler wouldn't recognize these functions.
`int main() { ... return 0; }`
int main() {
// ... program logic ...
return 0;
}
This is the main function, the entry point of every C program. The main
function is where the program execution begins. int
indicates that it returns an integer value. return 0;
signifies that the program executed successfully. Any non-zero return value typically indicates an error.
`int a, b;`
int a, b;
Declares two integer variables. int
is a data type that stores whole numbers. a
and b
are the names of the variables. This line reserves memory locations for a
and b
to hold integer values.
Input with `printf` and `scanf`
printf("Enter two integer values... ");
if (scanf("%d %d", &a, &b) != 2) {
printf("Invalid input. Please enter two integers.\n");
return 1;
}
printf()
displays a message to the user. scanf()
reads formatted input from the standard input. "%d %d"
tells scanf
to expect two decimal integers. &a
and &b
are the "address-of" operators, providing scanf
with memory addresses to store the values. The if
condition checks if exactly two integers were successfully read, providing basic error handling.
Arithmetic Operators (`+`, `-`, `*`, `/`, `%`)
printf("Addition (a + b): %d\n", a + b);
// ... other arithmetic operations ...
if (b != 0) { /* ... division/modulus ... */ }
These perform basic mathematical operations: addition, subtraction, multiplication, division (integer division for integers), and modulus (remainder). The if (b != 0)
check for division/modulus is crucial to prevent "division by zero" errors, which would crash the program.
Relational Operators (`==`, `!=`, `>`, `<`, `>=`, `<=`)
printf("a == b: %d\n", a == b);
// ... other relational operations ...
These compare two operands and return a boolean result (0
for false, 1
for true in C). They check for equality, inequality, greater than, less than, greater than or equal to, and less than or equal to.
Logical Operators (`&&`, `||`, `!`)
printf("(a > 0 && b > 0): %d\n", (a > 0 && b > 0));
// ... other logical operations ...
These combine or modify boolean expressions. &&
(Logical AND) is true if both operands are true. ||
(Logical OR) is true if at least one operand is true. !
(Logical NOT) inverts the boolean value of an operand.
Bitwise Operators (`&`, `|`, `^`, `~`, `<<`, `>>`)
printf("Bitwise AND (a & b): %d\n", a & b);
// ... other bitwise operations ...
for (int i = 7; i >= 0; i--) { /* ... print bits ... */ }
These operators work on the individual bits of integer operands. &
(AND), |
(OR), ^
(XOR), ~
(NOT), <<
(Left Shift), and >>
(Right Shift) perform bit-level manipulations. The for
loops are included to visualize the binary representation of the numbers.
Assignment Operators (`=`, `+=`, `-=`, `*=`, `/=`, `%=`)
int c = a;
c += b; // Equivalent to c = c + b
// ... other compound assignments ...
These assign a value to a variable. Compound assignment operators (like +=
) perform an operation (e.g., addition) and then assign the result back to the variable.
Increment/Decrement Operators (`++`, `--`)
printf("Pre-increment (++x): %d (x is now %d)\n", ++x, x);
printf("Post-increment (x++): %d (x is now %d)\n", x++, x);
++x
(Pre-increment) increments x
*before* its value is used in the expression. x++
(Post-increment) uses x
's current value in the expression, then increments x
. Decrement operators (`--x`, `x--`) work similarly but decrease the value.
Ternary (Conditional) Operator (`? :`)
printf("Max of a and b: %d\n", (a > b) ? a : b);
A shorthand for a simple if-else
statement. It evaluates a condition (a > b
). If true, it returns the value before the colon (a
); otherwise, it returns the value after the colon (b
).