[go: up one dir, main page]

0% found this document useful (0 votes)
8 views8 pages

FPL Assignment 2 Q & A: 1. Arithmetic Operators

The document outlines various types of operators in C programming, including arithmetic, relational, logical, bitwise, assignment, increment/decrement, conditional, sizeof, comma, and pointer operators. It explains the functionality and usage of logical and bitwise operators, relational operators, and expressions in C, along with examples. Additionally, it covers the conditional (ternary) operator, sizeof operator, increment/decrement operators, and the precedence of arithmetic operators.

Uploaded by

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

FPL Assignment 2 Q & A: 1. Arithmetic Operators

The document outlines various types of operators in C programming, including arithmetic, relational, logical, bitwise, assignment, increment/decrement, conditional, sizeof, comma, and pointer operators. It explains the functionality and usage of logical and bitwise operators, relational operators, and expressions in C, along with examples. Additionally, it covers the conditional (ternary) operator, sizeof operator, increment/decrement operators, and the precedence of arithmetic operators.

Uploaded by

surajandhale1600
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

FPL Assignment 2 Q & A

Q.1What are the different types of operators in C


In C programming, operators are symbols that perform operations on variables and values.
There are several types of operators

1. Arithmetic Operators

These operators are used to perform basic arithmetic operations.

 + : Addition
 - : Subtraction
 * : Multiplication
 / : Division
 % : Modulus (remainder of a division)

2. Relational (Comparison) Operators

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.

 && : Logical AND


 || : Logical OR
 ! : Logical NOT

4. Bitwise Operators

These operators are used to manipulate individual bits of integer values.

 & : Bitwise AND


 | : Bitwise OR
 ^ : Bitwise XOR (exclusive OR)
 ~ : Bitwise NOT
 << : Left shift
 >> : Right shift

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

6. Increment and Decrement Operators

These operators are used to increase or decrease a variable's value by 1.

 ++ : Increment (can be prefix or postfix)


 -- : Decrement (can be prefix or postfix)

7. Conditional (Ternary) Operator

The ternary operator is a shorthand for an if-else statement.

 ? : : Conditional operator

8. Sizeof Operator

This operator is used to determine the size (in bytes) of a data type or variable.

 sizeof : Returns the size 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

These operators are used for manipulating pointers.

 & : Address-of operator (returns the memory address of a variable)


 * : Dereference operator (accesses the value at a memory address)

Q.2 Explain Logical and Bitwise operator in C.

&& (Logical AND)

 Returns true (1) if both operands are true.


 If either is false, the whole expression becomes false.

Example:
int a = 5, b = 10;
if (a < b && b > 0) {
printf("Both conditions are true.\n");
}

✅ Output: Both conditions are true.

2. || (Logical OR)

 Returns true (1) if at least one operand is true.


 Returns false only if both are false.

Example:

int a = 5, b = 10;
if (a == 5 || b == 0) {
printf("At least one condition is true.\n");
}

✅ Output: At least one condition is true.

3. ! (Logical NOT)

 Reverses the logical state.


 If the condition is true, ! makes it false, and vice versa.

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)

& Bitwise AND

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

Q.3Explain Relational operators in C.

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.

Here are the six relational operators in C:

Operator Meaning Example Result


== Equal to a == b True if a equals b
!= Not equal to a != b True if a is not equal to b
> Greater than a>b True if a is greater than b
< Less than a<b True if a is less than b
>= Greater than or equal to a >= b True if a is greater than or equal to b
<= Less than or equal to a <= b True if a is less than or equal to b

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;
}

Q.4.. What are the expressions in C?

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

 Contains only constants.

10 + 5 // evaluates to 15
2. Arithmetic Expression

 Uses arithmetic operators (+, -, *, /, %).

a+b*c
3. Relational Expression

 Compares values using relational operators (<, >, <=, >=, ==, !=).

a>b
4. Logical Expression

 Combines conditions using logical operators (&&, ||, !).

(a > b) && (b > 0)


5. Bitwise Expression

 Manipulates data at bit level.

a&b
6. Assignment Expression

 Assigns a value to a variable.

x=y+2
7. Unary Expression

 Uses a single operand (like -, ++, --, !, etc.).

++a
8. Conditional (Ternary) Expression

 Uses ? : to evaluate conditionally.

int min = (a < b) ? a : b;


9. Function Call Expression

 A function call that returns a value.

sqrt(25)
Q.5 Explain conditional /Ternary operator in c with example.

The ternary operator is a shorthand way of writing an if-else statement.


It’s called ternary because it takes three operands.

✅ Syntax

condition ? expression_if_true : expression_if_false;

 If the condition is true, it returns expression_if_true.


 If the condition is false, it returns expression_if_false.

📘 Example:

#include <stdio.h>

int main() {
int a = 10, b = 20;
int max;

// Using ternary operator


max = (a > b) ? a : b;
printf("The maximum is: %d\n", max);

return 0;
}

✅ Output:

The maximum is: 20


Q.6 Describe sizeof operator in C.

The sizeof operator in C is used to determine the size (in bytes) of a data type or a variable
in memory

🔸 Syntax:

sizeof(expression) // e.g., sizeof(x)


sizeof(type) // e.g., sizeof(int)

🔸 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:

There are two forms of each:

Operator Name Description


++a Pre-increment Increments first, then uses the value
a++ Post-increment Uses the value first, then increments
--a Pre-decrement Decrements first, then uses the value
a-- Post-decrement Uses the value first, then decrements

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.

Precedence of Arithmetic Operators in C

Operator precedence determines the order in which operators are evaluated in an


expression. In C, arithmetic operators have a specific order of precedence, which affects the
outcome of expressions involving multiple operators.
Precedence of Arithmetic Operators:

Here’s the order of precedence for arithmetic operators, from highest to lowest:

1. Multiplication, Division, and Modulus (*, /, %)


2. Addition and Subtraction (+, -)

Operator Precedence Order:

Operator Description Precedence


* Multiplication 1
/ Division 1
% Modulus (Remainder) 1
+ Addition 2
- Subtraction 2

 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:

1. Multiplication (3 * 2) is performed first, because * has higher precedence.


2. Then Addition (5 + 6) is performed.

✅ Result: 11

🎯 Parentheses Override Precedence:

If you want to change the natural order, you can use parentheses to enforce your desired
evaluation order

int result = (5 + 3) * 2; // Adds 5 + 3 first, then multiplies by 2

✅ Result: 16

You might also like