Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
9 views101 pages

Unit 2 - Merged

The document provides an overview of various operators in C programming, including arithmetic, logical, bitwise, and assignment operators, along with examples of their usage. It also covers type conversions, formatted input, and character handling functions. Additionally, it includes sample C programs demonstrating these concepts in practice.

Uploaded by

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

Unit 2 - Merged

The document provides an overview of various operators in C programming, including arithmetic, logical, bitwise, and assignment operators, along with examples of their usage. It also covers type conversions, formatted input, and character handling functions. Additionally, it includes sample C programs demonstrating these concepts in practice.

Uploaded by

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

Unit 2

• Here a and b are variables and are known as operands . The modulo
division operator % cannot be used on floating point data.
• When both the operands in a single arithmetic expression such as a+b
are integers, the expression is called an integer expression , and the
operation is called integer arithmetic.

if a and b are integers, then for a = 14 and b = 4 we have the


following results:
a – b = 10
a + b = 18
a * b = 56
a / b = 3 (decimal part truncated)
a % b = 2 (remainder of division)
• During integer division:
• if both the operands are of the same sign, the result is truncated
towards zero.
• If one of them is negative, the direction of truncation is
implementation dependent.
• That is, 6/7 = 0 and –6/–7 = 0
Operand 1 Operand 2 Result Sign Example Final Result
Positive Positive Positive 7/3 2
Negative Positive Negative -7 / 3 -2
Positive Negative Negative 7 / -3 -2
Negative Negative Positive -7 / -3 2
Expression First Operand (Dividend) Result
-14 % 3 Negative -2
-14 % -3 Negative -2
14 % -3 Positive 2
C Program to Convert #include <stdio.h>

int main() {
Days to Months and Days int totalDays, months, days;

// Input: Total number of days


printf("Enter the number of days: ");
scanf("%d", &totalDays);

// Calculate months and remaining days


months = totalDays / 30; // Integer division to calculate full
months
days = totalDays % 30; // Remainder gives the remaining
days

// Output the result


printf("%d days is equivalent to %d months and %d days.\n",
totalDays, months, days);

return 0;
}
Logical operators
#include <stdio.h>
C Program Using
int main() {
Logical Operators int a = 5, b = 10;

// Logical AND
if (a > 0 && b > 0) {
printf("Both a and b are positive.\n");
}

// Logical OR
if (a > 0 || b < 0) {
printf("Either a is positive or b is negative.\n");
}

// Logical NOT
if (!(a == b)) {
printf("a is not equal to b.\n");
}

return 0;
}
#include <stdio.h>
C Program with User Input and int main() {
Logical Operators int age;
int hasID;

// Get user input


printf("Enter your age: ");
scanf("%d", &age);

printf("Do you have a valid ID? (1 for Yes, 0 for No): ");
scanf("%d", &hasID);

// Check using logical AND


if (age >= 18 && hasID == 1) {
printf("Access granted.\n");
} else {
printf("Access denied.\n");
}

return 0;
}
Assignment operator
• Assignment operators are used to assign the result of an expression
to a variable.
• Ex:
int x = 1;
x=x+10;
Shorthand assignment operator
Increment , decrement operators
#include <stdio.h>

int main()
{
int a = 3, b = 4, c;

c = a++ + ++b; // c = 3 + 5 = 8, then a becomes 4


printf("a = %d, b = %d, c = %d\n", a, b, c);

c = --a + b--; // c = 3 + 5 = 8, then b becomes 4


printf("a = %d, b = %d, c = %d\n", a, b, c);

return 0;
}
Increment , decrement
operators
#include <stdio.h>

int main()
{
int x = 5;

printf("x = %d\n", x);


printf("x++ = %d\n", x++); // Post-increment: prints 5, then x becomes 6
printf("After x++: x = %d\n", x);
printf("++x = %d\n", ++x); // Pre-increment: x becomes 7, then prints 7

printf("x-- = %d\n", x--); // Post-decrement: prints 7, then x becomes 6


printf("After x--: x = %d\n", x);
printf("--x = %d\n", --x); // Pre-decrement: x becomes 5, then prints 5

return 0;
}
#include <stdio.h>
Increment , decrement int main()
{
operators int a = 10;

printf("Original value of a: %d\n", a);

// Post-increment
printf("Post-increment (a++): %d\n", a++);
printf("After post-increment, a: %d\n", a);

// Pre-increment
printf("Pre-increment (++a): %d\n", ++a);
printf("After pre-increment, a: %d\n", a);

// Post-decrement
printf("Post-decrement (a--): %d\n", a--);
printf("After post-decrement, a: %d\n", a);

// Pre-decrement
printf("Pre-decrement (--a): %d\n", --a);
printf("After pre-decrement, a: %d\n", a);
return 0;
}
Bitwise operators
• C has a distinction of supporting special operators known as bitwise
operators for manipulation of data at bit level.

• These operators are used for testing the bits, or shifting them right or
left.

• Bitwise operators may not be applied to float or double .


Bitwise operators
Bitwise operators

Operator Name Description


& Bitwise AND Sets each bit to 1 if both bits are 1
` ` Bitwise OR
Sets each bit to 1 if only one of the
^ Bitwise XOR
bits is 1 (exclusive OR)
Inverts all bits (1 becomes 0, 0
~ Bitwise NOT
becomes 1)
Shifts bits to the left (fills with 0 on
<< Left shift
the right)
Shifts bits to the right (fills with 0
>> Right shift
on the left for unsigned types)
1.Bitwise AND (&) - Compares each bit of two numbers.
Returns 1 only if both bits are 1.

Example: 5 & 3 → ?

Binary:
0101 (5)
& 0011 (3)
---------
0001 → Result: 1
| (Bitwise OR) -Compares each bit of two numbers. Returns 1 if
at least one bit is 1.

Example:
5|3→?
Binary:
0101 (5)
| 0011 (3)
------------
0111 → Result: 7
3. ^ (Bitwise XOR) - Compares each bit of two numbers. Returns 1 if the bits
are different.

Example:

5^3→?
Binary:

0101
^ 0011
----------
0110 → Result: 6
4. << (Left Shift) - Shifts bits to the left by the specified number of positions. Adds 0s at the
right. Each shift left multiplies the number by 2.

Example:

5 << 1 → ?
Binary:
0101 << 1 = 1010

→ Result: 10
>> (Right Shift) - Shifts bits to the right by the specified number of
positions. Removes bits from the right. Each shift right divides the number
by 2.

Example: 5 >> 1 → ?

Binary: 0101 >> 1 = 0010 → Result: 2


Special Operators
• comma operator (,)
• sizeof operator
• pointer operators ( & and * ) and
• member selection operators (. and – > ).
The Comma Operator
• The comma operator can be used to link the related expressions together.
• A comma-linked list of expressions are evaluated left to right and the value
of right-most expression is the value of the combined expression.
• For example, the statement
value = (x = 10, y = 5, x+y);
Explanation: first assigns the value 10 to x , then assigns 5 to y , and finally
assigns 15 (i.e. 10 + 5) to value .
Since comma operator has the lowest precedence of all operators, the
parentheses are necessary
Comma operator:
Comma operator
Comma operator
Swapping of 2 variables using comma operator
#include <stdio.h>
int main()
{
int x = 5, y = 10, t;
int result;
// Swapping using comma operator
result = (t = x, x = y, y = t);
// Print the values after swapping
printf("After swapping:\n");
printf("x = %d\n", x); // Should print 10
printf("y = %d\n", y); // Should print 5
printf("result = %d\n", result); // Should print 5 (value of t)
return 0;
The sizeof Operator
• The sizeof is a compile time operator and, when used with an
operand, it returns the number of bytes the operand occupies. The
operand may be a variable, a constant or a data type qualifier.
Arithmetic Expressions
Evaluation of Expressions
• Expressions are evaluated using an assignment statement of the form:

Examples: Precedence of arithmetic operators


Evaluate the following expression:
x=a-b/3+c*2-1
Hierarchical representation of the following expression :
x=a-b/3+c*2-1
x= 9-12/3+3*2-1
Evaluate the following expression:
9 – 12/(3+3)*(2 – 1)
Rules for Evaluation of Expression
Type Conversions
• Implicit Type Conversion
• Explicit Conversion
Implicit Type Conversion

• C automatically converts any intermediate values to the proper type


so that the expression can be evaluated without loosing any
significance. This automatic conversion is known as implicit type
conversion.
• If the operands are of different types, the ‘lower’ type is
automatically converted to the ‘higher’ type before the operation
proceeds. The result is of the higher type.
1. short and char are automatically converted to int

char a = 10;
short b = 20;
int result = a + b; // Both 'a' and 'b' are promoted to int
2. If one operand is long double, the other is converted to long double;
result is long double

Ex:
float a = 5.5;
long double b = 2.2L;
long double result = a + b; // 'a' is promoted to long double
3. if one operand is double, the other is converted to double; result is double

float a = 5.5f;
double b = 2.2;
double result = a + b; // 'a' is promoted to double
4. if one operand is float, the other is converted to float; result is float

Ex:
int a = 5;
float b = 2.5f;
float result = a + b; // 'a' is promoted to float
5. Else if one operand is unsigned long int, the other is converted to
unsigned long int; result is unsigned long int

Ex:

unsigned int a = 100;


unsigned long int b = 100000UL;
unsigned long int result = a + b; // 'a' is promoted to unsigned long int
6. One operand is long int and other is unsigned int

(a) If unsigned int can be converted to long int, result is long int:

unsigned int a = 100;


long int b = 1000L;
long int result = a + b; // 'a' is converted to long int

(b) Else, both converted to unsigned long int;


result is unsigned long int:

Ex:
unsigned int a = 4294967295U; // max value for 32-bit unsigned i
7. if one operand is long int, the other is converted to long int;
result is long int

int a = 50;
long int b = 1000L;
long int result = a + b; // 'a' is promoted to long int

8. if one operand isunsigned int, the other is converted to unsigned int;


result is unsigned int

int a = 20;
unsigned int b = 30U;
unsigned int result = a + b; // 'a' is promoted to unsigned int
The following changes are introduced during the final
assignment.
• float to int causes truncation of the fractional part.
• double to float causes rounding of digits.
• long int to int causes dropping of the excess higher order
bits.
Explicit Type Conversion
Reading a character

> The simplest of all input/output operations is reading a character


from the ‘standard input’ unit (usually the keyboard) and writing it to
the ‘standard output’ unit (usually the screen).

> Reading a single character can be done by using the getchar.


getchar()
• Syntax:
Variable_name = getchar();

Ex: char name; name = getchar();


1.Write a C program that: #include <stdio.h>
•Asks the user a YES/NO question using getchar(). int main()
•If the user enters Y or y, print: {
"My name is BUSY BEE"
char response;
•Otherwise, print:
"You are good for nothing" printf("Do you want to know my name? (Y/N): ");
response = getchar(); // Read a single character input

if (response == 'Y' || response == 'y')


{
printf("My name is BUSY BEE\n");
}
else
{
printf("You are good for nothing\n");
}
return 0;
}
#include <stdio.h>

2.C program that reads a character #include <ctype.h> // Needed for isalpha() and isdigit()

from the keyboard and checks int main()

whether it's a letter or digit {


char ch;
printf("Enter a character: ");
ch = getchar(); // Read a single character
if (isalpha(ch))
{
printf("It is a letter.\n");
}
else if (isdigit(ch))
{
printf("It is a digit.\n");
} else
{
printf("It is neither a letter nor a digit.\n");
}
return 0; }
Character Test Function
Writing a character

• Syntax :
putchar(variable_name);
#include <stdio.h>
#include <ctype.h> // Required for islower(), toupper(), tolower(), isalpha()

Write a C program that reads a


int main()
character and converts it to uppercase if
{
it is lowercase; otherwise, converts it to
char ch;
lowercase.
printf("Enter a character: ");
ch = getchar(); // Read a single character
if (islower(ch))
{
printf("Converted to uppercase: %c\n", toupper(ch));
}
else
{
printf("Converted to lowercase: %c\n", tolower(ch));
}
return 0;
}
Formatted Input
• Formatted input refers to an input data that has been arranged in a
particular format.
• For example, consider the following data:

15.75 123 John


scanf: scanf means scan formatted.

 The control string specifies the field format in which the data is to be
entered and the arguments arg 1, arg2, ...., argn specify the address of
locations where the data is stored.

 Control string and arguments are separated by commas.


scanf:
• Control string (also known as format string ) contains field
specifications, which direct the interpretation of input data.
• It may include: Field (or format) specifications, consisting of the
conversion character %, a data type character (or type specifier), and
an optional number, specifying the field width. Blanks, tabs, or
newlines.
scanf()

1.Basic Integer Input


 scanf("%d", &num); reads an integer and stores it in num.

2. Field Width in scanf()


 The field width restricts the number of characters to be read for that input.
 Syntax:

scanf("%2d", &num); //reads only 2 digits even if more are available.


Ex:
3. Avoiding Field Width Errors

Avoid field width unless absolutely necessary.

Use:

scanf("%d %d", &num1, &num2);

This reads integers correctly regardless of the number of digits.


4. Input Rules
Input Rules

Ex:
•If you enter:

25,50

Note: it will not work correctly with scanf("%d %d", &a, &b);
because scanf stops reading at the comma.
Input rules
> scanf skips whitespace

•When reading values, scanf automatically ignores any number of whitespace characters
(space, tab, newline) until it finds valid data.

Ex:

int x;
scanf("%d", &x);

Will successfully read input even if the user types:


<space>42

(scanf skips the spaces and reads 42)


5. Floating Point Instead of Integer
•Inputting 3.14 using %d will read only 3.

•Fractional part is discarded.

6. Skipping Fields
Use * to skip a field:
#include<stdio.h>
int main()
{
int a,b,c,x,y,z;
int p,q,r;

printf("Enter three integer numbers\n");


scanf("%d %*d %d", &a, &b, &c);
printf("%d %d %d \n\n", a, b, c);

printf("Enter two 4-digit numbers\n");


scanf("%2d %4d", &x, &y);
printf("%d %d\n\n", x, y);

printf("Enter two integers\n");


scanf("%d %d", &a, &x);
printf("%d %d \n\n", a, x);

printf("Enter a nine digit number\n");


scanf("%3d %4d %3d", &p, &q, &r);
printf("%d %d %d \n\n", p, q, r);

printf("Enter two three digit numbers\n");


scanf("%d %d", &x, &y);
printf("%d %d", x, y);
#include <stdio.h>

Ex: int main() {


int num1, num2;

// Reading with field width


printf("Enter two numbers (Field width 2 and 5): ");
scanf("%2d %5d", &num1, &num2);
printf("With field width: num1 = %d, num2 = %d\n", num1, num2);

// Reading without field width


printf("Enter two numbers again (no field width): ");
scanf("%d %d", &num1, &num2);
printf("Without field width: num1 = %d, num2 = %d\n", num1, num2);

// Skipping a field
int a, b;
printf("Enter three numbers (middle one will be skipped): ");
scanf("%d %*d %d", &a, &b);
printf("After skipping: a = %d, b = %d\n", a, b);

// Short and long input


short s;
long l;
printf("Enter a short and a long integer: ");
scanf("%hd %ld", &s, &l);
printf("Short = %hd, Long = %ld\n", s, l);

return 0;
}
Field width for real number

scanf() and Float Input Explanation:

scanf("%f %f %f", &x, &y, &z);

This tells the program to:


•Read three float numbers from input.
•Store them in x, y, and z respectively.
Example:
Reading Character Strings in C

• C provides two main ways to input character strings:


Relational operators: Comparisons can be done with the help of relational
operators.

Sample program to show relational operators usage:

#include <stdio.h>
int main()
{
int a = 10, b = 20;

printf("a = %d, b = %d\n", a, b);


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
printf("a >= b: %d\n", a >= b); // Greater than or equal to
printf("a <= b: %d\n", a <= b); // Less than or equal to

return 0;
}

Logical operators:

#include <stdio.h>
int main()
{
int x = 5, y = 10;

printf("x = %d, y = %d\n", x, y);


printf("x > 0 && y > 0: %d\n", x > 0 && y > 0); // Logical AND
printf("x > 0 || y < 0: %d\n", x > 0 || y < 0); // Logical OR
printf("!(x == y): %d\n", !(x == y)); // Logical NOT
return 0;
}

Relational Operators Questions

🔸 Q1: Write a C program to compare two integers entered by the user.

 Use relational operators to display:


o Whether the first number is equal to, greater than, or less than the second number.

🔸 Q2: Write a C program that checks if a number is between 10 and 100 (inclusive).

 Use >= and <= operators.

🔸 Q3: Write a C program to check if two floating-point numbers are not equal.

✅ Logical Operators Questions

🔸 Q4: Write a C program to check if a person is eligible to vote.

 Age must be greater than or equal to 18.


 Use if and logical conditions.

🔸 Q5: Write a program to check if a number is:

 Even and greater than 10


 Use both % and &&.

🔸 Q6: Write a program to check if a student:

 Failed the exam (marks < 40) or was absent (use a variable int absent = 1 or 0)
 Use logical ||.

🔸 Q7: Given three numbers, check if:

 All are positive using &&


 At least one is negative using ||
✅ Combined Relational & Logical Operators Questions

🔸 Q8: Write a program that takes age and marks as input and:

 Prints “Scholarship Granted” if age ≥ 18 and marks > 75


 Prints “Considered” if age < 18 or marks > 90

🔸 Q9: Write a program that:

 Checks if a number is not between 50 and 100.


 Use ! with relational operators.

🔸 Q10: Take two numbers and:

 Print "Both are even" if both are divisible by 2.


 Print "One or both are odd" otherwise.

Relational Operators Questions

🔸 Q1: Write a C program to compare two integers entered by the user.

 Use relational operators to display:


o Whether the first number is equal to, greater than, or less than the second number.

#include <stdio.h>

int main() {
int a, b;
printf("Enter two integers: ");
scanf("%d %d", &a, &b);

if (a == b)
printf("Both numbers are equal.\n");
else if (a > b)
printf("First number is greater.\n");
else
printf("Second number is greater.\n");

return 0;
}
🔸 Q2: Write a C program that checks if a number is between 10 and 100 (inclusive).

 Use >= and <= operators.


#include <stdio.h>

int main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);

if (num >= 10 && num <= 100)


printf("Number is between 10 and 100.\n");
else
printf("Number is outside the range.\n");

return 0;
}

🔸 Q3: Write a C program to check if two floating-point numbers are not equal.
#include <stdio.h>

int main() {
float x, y;
printf("Enter two float numbers: ");
scanf("%f %f", &x, &y);

if (x != y)
printf("The numbers are not equal.\n");
else
printf("The numbers are equal.\n");

return 0;
}

✅ Logical Operators Questions

🔸 Q4: Write a C program to check if a person is eligible to vote.

 Age must be greater than or equal to 18.


 Use if and logical conditions.

#include <stdio.h>

int main() {
int age;
printf("Enter age: ");
scanf("%d", &age);

if (age >= 18)


printf("Eligible to vote.\n");
else
printf("Not eligible to vote.\n");

return 0;
}

🔸 Q5: Write a program to check if a number is:

 Even and greater than 10


 Use both % and &&.

#include <stdio.h>

int main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);

if (num % 2 == 0 && num > 10)


printf("Even and greater than 10.\n");
else
printf("Condition not met.\n");

return 0;
}
🔸 Q6: Write a program to check if a student:

 Failed the exam (marks < 40) or was absent (use a variable int absent = 1 or 0)
 Use logical ||.

#include <stdio.h>

int main()
{
int marks, absent;
printf("Enter marks and absence status (1=absent, 0=present): ");
scanf("%d %d", &marks, &absent);
if (marks < 40 || absent == 1)
printf("Student failed or was absent.\n");
else
printf("Student passed and was present.\n");

return 0;
}
🔸 Q7: Given three numbers, check if:

 All are positive using &&


 At least one is negative using ||

#include <stdio.h>

int main()
{
int a, b, c;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);

if (a > 0 && b > 0 && c > 0)


printf("All numbers are positive.\n");

if (a < 0 || b < 0 || c < 0)


printf("At least one number is negative.\n");

return 0;
}

✅ Combined Relational & Logical Operators Questions

🔸 Q8: Write a program that takes age and marks as input and:

 Prints “Scholarship Granted” if age ≥ 18 and marks > 75


 Prints “Considered” if age < 18 or marks > 90

#include <stdio.h>

int main()
{
int age, marks;
printf("Enter age and marks: ");
scanf("%d %d", &age, &marks);
if (age >= 18 && marks > 75)
printf("Scholarship Granted.\n");
else if (age < 18 || marks > 90)
printf("Considered.\n");
else
printf("Not eligible.\n");

return 0;
}

🔸 Q9: Write a program that:

 Checks if a number is not between 50 and 100.


 Use ! with relational operators.

#include <stdio.h>

int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);

if (!(num >= 50 && num <= 100))


printf("Number is NOT between 50 and 100.\n");
else
printf("Number is in the range.\n");

return 0;
}

NOTE: if (!(number >= 50) || !(number <= 100))

🔸 Q10: Take two numbers and:

 Print "Both are even" if both are divisible by 2.


 Print "One or both are odd" otherwise.

#include <stdio.h>

int main()
{
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);

if (a % 2 == 0 && b % 2 == 0)
printf("Both are even.\n");
else
printf("One or both are odd.\n");

return 0;
}

Understanding Operator Precedence – Practice Questions


Complete order of precedence :

 Parentheses () – grouping (not an operator)


 Logical NOT !
 Relational operators – <, <=, >, >=
 Equality operators – ==, !=
 Logical AND &&
 Logical OR ||

1.What will be the output of the following code?

#include <stdio.h>

int main() {
int a = 5, b = 10;
if (a < b && b > 3 || a == 5)
printf("Condition is true\n");
else
printf("Condition is false\n");
return 0;
}
Solution:
2. What will the following code output?

#include <stdio.h>

int main()
{
int x = 5;
if (!x > 2)
printf("True\n");
else
printf("False\n");
return 0;
}

Solution:

1. !x → !5 → 0 (false)
2. 0 > 2 → false

Predict the Output

int x = 0, y = 5;

if (x != y && y || x)

Evaluate:

 x != y → 0 != 5 → true
 x != y && y → true && 5 → true
 true || x → true || 0 → true

1. What is the primary purpose of parentheses () in a C expression?

A. To perform addition
B. To assign values
C. To group expressions and control evaluation order
D. To define a function

Answer: C. To group expressions and control evaluation order


2. What does the logical NOT operator ! do in C?

A. Multiplies the operand by -1


B. Returns true if the operand is non-zero
C. Reverses the logical state of its operand
D. Compares two values for equality

Answer: C. Reverses the logical state of its operand

3. Which of the following is a relational operator in C?

A. !=
B. &&
C. <=
D. ==

Answer: C. <=

4. What is the result of the expression !(5 > 2) in C?

A. true
B. false
C. 5
D. 2

Answer: B. false
(Because 5 > 2 is true, and !true is false)

5. Which expression correctly checks if a number x is equal to 10 in C?

A. x = 10
B. x == 10
C. x := 10
D. x != 10

Answer: B. x == 10

6. What will the expression (a > 5 && b < 10) evaluate to if a = 6 and b = 12?

A. true
B. false
C. 6
D. 12

Answer: B. false
(Because a > 5 is true, but b < 10 is false, so the whole && condition is false)

7. Which of the following operators is used to test inequality in C?

A. ==
B. !=
C. <=
D. !

Answer: B. !=

1. What is the output of the expression: !0 == 1?

A. 0
B. 1
C. true
D. Compilation error

Answer: B. 1
(!0 becomes 1, so 1 == 1 is true → 1)

2. Which expression evaluates to true if x = 5, y = 10?

A. x > 3 && y < 9


B. x < 6 && y >= 10
C. x == 5 || y < 10
D. !(x <= 5 && y == 10)

Answer: C. x == 5 || y < 10
(First two options are partly false; fourth is negating a true condition)

3. In the expression x < 10 || x > 20 && x != 15, which part is evaluated first?

A. x < 10
B. x > 20
C. x != 15
D. x > 20 && x != 15

Answer: D. x > 20 && x != 15


(&& has higher precedence than ||)

4. What does the expression !(5 < 10 == 0) evaluate to?

A. 0
B. 1
C. true
D. Error

Answer: B. 1
(5 < 10 is true (1), then 1 == 0 is false (0), then !0 is 1)

5. If a = 4, b = 5, what is the result of: a + b > 8 == 1?

A. 0
B. 1
C. true
D. Compilation error
Answer: B. 1
(a + b = 9; 9 > 8 → 1; then 1 == 1 → 1)

6. Which operator has the highest precedence among the following?

A. ==
B. &&
C. !
D. <=

Answer: C. !

7. Evaluate: !(3 > 2 && 5 < 10)

A. 0
B. 1
C. true
D. false

Answer: A. 0
(3 > 2 → true, 5 < 10 → true, so true && true → true, then !true → 0)

8. What is the output of 5 > 2 == 0?

A. 1
B. 0
C. true
D. Error

Answer: B. 0
(5 > 2 → 1; 1 == 0 → 0)

9. Choose the correct precedence order from highest to lowest:

A. ==, !, &&
B. !, ==, &&
C. &&, ==, !
D. ==, &&, !

Answer: B. !, ==, &&


10. What is the result of !(1 && 0) || 1?

A. 0
B. 1
C. true
D. Compilation error

Answer: B. 1
(1 && 0 → 0; !0 → 1; 1 || 1 → 1)

1. What is the difference between i++ and ++i in C?

A. No difference
B. i++ increments after use, ++i increments before use
C. ++i increments after use, i++ increments before use
D. i++ and ++i are both invalid

Answer: B. i++ increments after use, ++i increments before use

2. If int a = 5;, what is the value of b after b = a++?

A. 6
B. 5
C. Undefined
D. Compilation error

Answer: B. 5
(Post-increment: b gets the current value, then a becomes 6)

3. If int a = 5;, what is the value of b after b = ++a?

A. 5
B. 6
C. Undefined
D. Compilation error

Answer: B. 6
(Pre-increment: a becomes 6 first, then assigned to b)

4. Given: int x = 10; printf("%d", x--); — What will be printed?

A. 10
B. 9
C. 11
D. 0

Answer: A. 10
(Post-decrement: prints before decrement)

5. What will be the output of the following?

int i = 3;
printf("%d", --i);

A. 3
B. 2
C. 4
D. Undefined

Answer: B. 2
(Pre-decrement: decrements before printing)

6. Choose the correct statement about post-increment i++:

A. Increments the value immediately


B. Uses the current value first, then increments
C. Decrements the value
D. Not supported in C

Answer: B. Uses the current value first, then increments

7. What will be the result of the following code?

int a = 4;
int b = a++ + ++a;

A. b = 9
B. b = 10
C. b = 11
D. Undefined behavior

Answer: B. b = 10
(Step-by-step: a++ is 4 (a becomes 5), ++a is 6 → 4 + 6 = 10)
8. What is the final value of x after this code?

int x = 5;
x = x++ + 1;

A. 6
B. 7
C. 5
D. Undefined behavior

Answer: B. 6
(Post-increment returns 5, so x = 5 + 1 = 6; then x++ is lost due to sequence point confusion)

9. Which of the following is true about --x and x--?

A. Both increment the variable


B. Both evaluate to the same value always
C. --x decrements first, x-- decrements after use
D. They are not valid operators

Answer: C. --x decrements first, x-- decrements after use

10. What does this code print?

int a = 5;
printf("%d %d", a++, ++a);

A. 5 7
B. 5 6
C. 6 6
D. Undefined behavior

Answer: D. Undefined behavior


(Multiple modifications of a without a sequence point → undefined in C)
Conditional Operator :
Syntax:
condition ? expression_if_true : expression_if_false;

Sample programs:

1) This program checks whether a number is even or odd using the ternary operator or
conditional operator.

#include <stdio.h>

int main()
{
int num;

printf("Enter a number: ");


scanf("%d", &num);

// Ternary operator to check if the number is even or odd


(num % 2 == 0) ? printf("Even\n") : printf("Odd\n");

return 0;
}
2) Largest of Two Numbers

This program finds the largest of two numbers using the ternary operator.

#include <stdio.h>

int main()
{
int a, b;

printf("Enter two numbers: ");


scanf("%d %d", &a, &b);

// Ternary operator to find the larger number


int largest = (a > b) ? a : b;

printf("The largest number is: %d\n", largest);

return 0;
}

3. Nested Ternary Operator Example

This program uses a nested ternary operator to check whether a number is positive, negative,
or zero.

#include <stdio.h>

int main()
{
int num;

printf("Enter a number: ");


scanf("%d", &num);

// Nested ternary operator


(num > 0) ? printf("Positive\n") : (num < 0) ? printf("Negative\n") : printf("Zero\n");

return 0;
}

You might also like