Unit 2 - Merged
Unit 2 - Merged
• 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.
int main() {
Days to Months and Days int 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;
printf("Do you have a valid ID? (1 for Yes, 0 for No): ");
scanf("%d", &hasID);
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;
return 0;
}
Increment , decrement
operators
#include <stdio.h>
int main()
{
int x = 5;
return 0;
}
#include <stdio.h>
Increment , decrement int main()
{
operators int a = 10;
// 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.
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 → ?
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:
(a) If unsigned int can be converted to long int, result is 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
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
2.C program that reads a character #include <ctype.h> // Needed for isalpha() and isdigit()
• Syntax :
putchar(variable_name);
#include <stdio.h>
#include <ctype.h> // Required for islower(), toupper(), tolower(), isalpha()
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.
Use:
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);
6. Skipping Fields
Use * to skip a field:
#include<stdio.h>
int main()
{
int a,b,c,x,y,z;
int p,q,r;
// 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);
return 0;
}
Field width for real number
#include <stdio.h>
int main()
{
int a = 10, b = 20;
return 0;
}
Logical operators:
#include <stdio.h>
int main()
{
int x = 5, y = 10;
🔸 Q2: Write a C program that checks if a number is between 10 and 100 (inclusive).
🔸 Q3: Write a C program to check if two floating-point numbers are not equal.
Failed the exam (marks < 40) or was absent (use a variable int absent = 1 or 0)
Use logical ||.
🔸 Q8: Write a program that takes age and marks as input and:
#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).
int main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
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;
}
#include <stdio.h>
int main() {
int age;
printf("Enter age: ");
scanf("%d", &age);
return 0;
}
#include <stdio.h>
int main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
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:
#include <stdio.h>
int main()
{
int a, b, c;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
return 0;
}
🔸 Q8: Write a program that takes age and marks as input and:
#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;
}
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
return 0;
}
#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;
}
#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
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
A. To perform addition
B. To assign values
C. To group expressions and control evaluation order
D. To define a function
A. !=
B. &&
C. <=
D. ==
Answer: C. <=
A. true
B. false
C. 5
D. 2
Answer: B. false
(Because 5 > 2 is true, and !true is false)
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)
A. ==
B. !=
C. <=
D. !
Answer: B. !=
A. 0
B. 1
C. true
D. Compilation error
Answer: B. 1
(!0 becomes 1, so 1 == 1 is true → 1)
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
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)
A. 0
B. 1
C. true
D. Compilation error
Answer: B. 1
(a + b = 9; 9 > 8 → 1; then 1 == 1 → 1)
A. ==
B. &&
C. !
D. <=
Answer: C. !
A. 0
B. 1
C. true
D. false
Answer: A. 0
(3 > 2 → true, 5 < 10 → true, so true && true → true, then !true → 0)
A. 1
B. 0
C. true
D. Error
Answer: B. 0
(5 > 2 → 1; 1 == 0 → 0)
A. ==, !, &&
B. !, ==, &&
C. &&, ==, !
D. ==, &&, !
A. 0
B. 1
C. true
D. Compilation error
Answer: B. 1
(1 && 0 → 0; !0 → 1; 1 || 1 → 1)
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
A. 6
B. 5
C. Undefined
D. Compilation error
Answer: B. 5
(Post-increment: b gets the current value, then a becomes 6)
A. 5
B. 6
C. Undefined
D. Compilation error
Answer: B. 6
(Pre-increment: a becomes 6 first, then assigned to b)
A. 10
B. 9
C. 11
D. 0
Answer: A. 10
(Post-decrement: prints before decrement)
int i = 3;
printf("%d", --i);
A. 3
B. 2
C. 4
D. Undefined
Answer: B. 2
(Pre-decrement: decrements before printing)
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)
int a = 5;
printf("%d %d", a++, ++a);
A. 5 7
B. 5 6
C. 6 6
D. Undefined behavior
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;
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;
return 0;
}
This program uses a nested ternary operator to check whether a number is positive, negative,
or zero.
#include <stdio.h>
int main()
{
int num;
return 0;
}