FPL Assignment 2 Q & A: 1. Arithmetic Operators
FPL Assignment 2 Q & A: 1. Arithmetic Operators
1. Arithmetic Operators
+ : Addition
- : Subtraction
* : Multiplication
/ : Division
% : Modulus (remainder of a division)
These operators are used to compare two values. They return a boolean value (1 for true, 0
for false).
== : Equal to
!= : Not equal to
< : Less than
> : Greater than
<= : Less than or equal to
>= : Greater than or equal to
3. Logical Operators
These operators are used to perform logical operations, typically used with conditional
statements.
4. Bitwise Operators
5. Assignment Operators
These operators are used to assign values to variables.
= : Simple assignment
+= : Add and assign
-= : Subtract and assign
*= : Multiply and assign
/= : Divide and assign
%= : Modulus and assign
? : : Conditional operator
8. Sizeof Operator
This operator is used to determine the size (in bytes) of a data type or variable.
9.Comma Operator
The comma operator allows multiple expressions to be evaluated in a single statement, but
only the last expression's value is returned.
, : Comma operator
10.Pointer Operators
Example:
int a = 5, b = 10;
if (a < b && b > 0) {
printf("Both conditions are true.\n");
}
2. || (Logical OR)
Example:
int a = 5, b = 10;
if (a == 5 || b == 0) {
printf("At least one condition is true.\n");
}
3. ! (Logical NOT)
Example:
int a = 0;
if (!a) {
printf("a is zero (false), so !a is true.\n");
}
✅ Output: a is zero (false), so !a is true.
Operator Name Description
& Bitwise AND Sets each bit to 1 only if both bits are 1
` | Bitwise OR
^ Bitwise XOR Sets each bit to 1 if only one of the bits is 1
~ Bitwise NOT Inverts each bit (1 becomes 0, 0 becomes 1)
<< Left Shift Shifts bits to the left by given positions (adds zeros)
Right Shifts bits to the right by given positions
>>
Shift (drops bits)
int a = 5; // 0101
int b = 3; // 0011
int result = a & b; // 0001 = 1
2. | Bitwise OR
int a = 5; // 0101
int b = 3; // 0011
int result = a | b; // 0111 = 7
3. ^ Bitwise XOR
int a = 5; // 0101
int b = 3; // 0011
int result = a ^ b; // 0110 = 6
4. ~ Bitwise NOT
int a = 5; // 00000101
int result = ~a; // 11111010 (in 2's
complement = -6)
5. << Left Shift
int a = 5; // 00000101
int result = a << 1; // 00001010 = 10
6. >> Right Shift
int a = 5; // 00000101
int result = a >> 1; // 00000010 = 2
In C, relational operators are used to compare two values or expressions. They return either
true (1) or false (0) depending on whether the condition is satisfied.
Example in code:
#include <stdio.h>
int main() {
int a = 10, b = 20;
printf("a == b: %d\n", a == b); // Output: 0
printf("a != b: %d\n", a != b); // Output: 1
printf("a > b: %d\n", a > b); // Output: 0
printf("a < b: %d\n", a < b); // Output: 1
printf("a >= b: %d\n", a >= b); // Output: 0
printf("a <= b: %d\n", a <= b); // Output: 1
return 0;
}
In C, expressions are combinations of variables, constants, operators, and function calls that
the compiler can evaluate to produce a value
Types of Expressions in C
1. Constant Expression
10 + 5 // evaluates to 15
2. Arithmetic Expression
a+b*c
3. Relational Expression
Compares values using relational operators (<, >, <=, >=, ==, !=).
a>b
4. Logical Expression
a&b
6. Assignment Expression
x=y+2
7. Unary Expression
++a
8. Conditional (Ternary) Expression
sqrt(25)
Q.5 Explain conditional /Ternary operator in c with example.
✅ Syntax
📘 Example:
#include <stdio.h>
int main() {
int a = 10, b = 20;
int max;
return 0;
}
✅ Output:
The sizeof operator in C is used to determine the size (in bytes) of a data type or a variable
in memory
🔸 Syntax:
🔸 Examples:
#include <stdio.h>
int main() {
int a = 10;
double b = 5.5;
printf("Size of int: %zu bytes\n", sizeof(int)); // usually 4
printf("Size of double: %zu bytes\n", sizeof(b)); // usually 8
printf("Size of variable a: %zu bytes\n", sizeof a); // also valid without parentheses
return 0;
}
Q.7What is the use of increment and decrement operators in C?
The increment (++) and decrement (--) operators in C are used to increase or decrease a
variable’s value by 1, respectively.
✅ Types:
int a = 5;
printf("%d\n", a++); // prints 5, then a becomes 6
printf("%d\n", ++a); // a becomes 7, then prints 7
Q.8Short note on Precedence of Arithmetic Operators.
Here’s the order of precedence for arithmetic operators, from highest to lowest:
Multiplication (*), Division (/), and Modulus (%) have higher precedence than
Addition (+) and Subtraction (-).
Operations with the same precedence are evaluated from left to right (left
associativity).
📘 Example:
int result = 5 + 3 * 2;
In this expression:
✅ Result: 11
If you want to change the natural order, you can use parentheses to enforce your desired
evaluation order
✅ Result: 16