โ 6. Operators
An operator is a symbol that tells the C compiler to perform an operation. Operators are everywhere in C: they calculate marks and salaries, compare values, combine conditions, update variables, work with bits, access memory, and select values. Instead of memorizing a list of symbols, learn what each operator asks C to do, what operands it needs, and what result it produces.
6.1 What Is an Operator?
Consider this statement:
int total = 10 + 20;
Here 10 and 20 are the operands, and
+ is the operator. The operator tells C to add the
two operands. The result, 30, is then stored in total.
A calculator button such as + tells the calculator what to do.
In C, operators play the same role inside expressions. For example,
a + b means โtake the values of a and b
and perform addition.โ
| Term | Meaning | Example |
|---|---|---|
| Operator | Symbol or form that performs an operation | +, ==, && |
| Operand | Value or expression operated on | In a + 5, a and 5 |
| Expression | A combination of operands and operators that produces a value or effect | a + b * 2 |
| Result | The value produced by evaluating an expression | 10 + 20 produces 30 |
6.2 Why Are Operators Important?
A C program cannot do useful work with variables alone. Operators allow a program to calculate, compare, decide, repeat, update, and manipulate data. For example:
int marks = 75;
int pass = marks >= 40;
int salary = 30000;
salary += 5000;
int age = 22;
if (age >= 18 && age <= 60)
printf("Eligible");
The first expression compares marks, the second updates salary, and the third combines two conditions. These are all built from operators.
| What the program needs to do | Typical operators |
|---|---|
| Calculate a value | + - * / % |
| Compare two values | < <= > >= == != |
| Combine conditions | && || ! |
| Store or update a value | = += -= *= /= %= |
| Increase or decrease a value | ++ -- |
| Work with individual bits | & | ^ ~ << >> |
| Choose one of two values | ? : |
6.3 Operators Based on Number of Operands
One useful way to understand operators is to count how many operands they work with. This is called their arity.
| Type | Number of operands | Example | Meaning |
|---|---|---|---|
| Unary | One | -x |
Negate one value |
| Binary | Two | a + b |
Operate on two values |
| Ternary / conditional | Three operands | age >= 18 ? 1 : 0 |
Choose between two expressions based on a condition |
The same symbol can have different meanings depending on how it is used.
For example, - can mean subtraction in a - b, but it
can mean unary negation in -a.
Similarly, * can mean multiplication in a * b and is
also used with pointers in expressions such as *ptr.
6.4 Arithmetic Operators
Arithmetic operators perform numerical calculations. The five basic arithmetic operators are:
| Operator | Name | Example | Result |
|---|---|---|---|
+ |
Addition | 10 + 3 |
13 |
- |
Subtraction | 10 - 3 |
7 |
* |
Multiplication | 10 * 3 |
30 |
/ |
Division | 10 / 3 |
3 for integer operands |
% |
Remainder | 10 % 3 |
1 |
Simple Example
#include <stdio.h>
int main(void)
{
int a = 20;
int b = 6;
printf("Addition = %d\n", a + b);
printf("Subtraction = %d\n", a - b);
printf("Multiplication = %d\n", a * b);
printf("Division = %d\n", a / b);
printf("Remainder = %d\n", a % b);
return 0;
}
Output:
Addition = 26
Subtraction = 14
Multiplication = 120
Division = 3
Remainder = 2
How Does / Work With Integers?
This is one of the most important points for beginners. When both operands are integers, C performs integer division. The fractional part is not stored in the integer result.
printf("%d\n", 7 / 2); /* 3 */
printf("%.1f\n", 7.0 / 2); /* 3.5 */
In the first expression both operands are integers, so the result is an integer.
In the second expression, 7.0 is a floating-point value, so floating-
point division is performed.
What Does % Really Mean?
The modulus or remainder operator gives the remainder left after integer division. It is extremely useful for checking even/odd numbers, extracting digits, and solving cyclic problems.
int n = 27;
printf("%d\n", n % 10); /* last digit = 7 */
printf("%d\n", n % 2); /* 1 means odd */
Do not use % with floating-point operands. The C remainder operator
is for integer arithmetic. For floating-point remainder, the standard library
provides functions such as fmod().
6.5 Relational Operators โ Comparing Values
Relational operators ask a question such as โIs a greater than
b?โ The result of a comparison in C is an int value:
1 for true and 0 for false.
| Operator | Question | Example |
|---|---|---|
< |
Is left value less than right value? | a < b |
<= |
Is left value less than or equal to right value? | a <= b |
> |
Is left value greater than right value? | a > b |
>= |
Is left value greater than or equal to right value? | a >= b |
== |
Are the two values equal? | a == b |
!= |
Are the two values different? | a != b |
int age = 20;
printf("%d\n", age >= 18); /* 1 */
printf("%d\n", age < 18); /* 0 */
= is not ==.
= means assignment. It stores a value.
== means equality comparison. It asks whether two values are equal.
int x = 10; /* assignment */
if (x == 10) /* comparison */
{
printf("x is 10");
}
6.6 Logical Operators โ Combining Conditions
Logical operators are mainly used when a program has more than one condition.
C treats 0 as false and any nonzero value as true when a scalar
value is used as a condition.
| Operator | Name | Meaning |
|---|---|---|
&& |
Logical AND | True only when both conditions are true |
|| |
Logical OR | True when at least one condition is true |
! |
Logical NOT | Reverses the truth value |
Logical AND &&
int age = 25;
if (age >= 18 && age <= 60)
{
printf("Age is in the required range");
}
Both conditions must be true:
age >= 18 โ true
age <= 60 โ true
โ
true && true
โ
true
Logical OR ||
int day = 6;
if (day == 6 || day == 7)
{
printf("Weekend");
}
Only one condition needs to be true for the complete OR expression to be true.
Logical NOT !
int loggedIn = 0;
if (!loggedIn)
{
printf("Please log in");
}
Because loggedIn is zero, it represents false. Applying
! changes false to true.
6.7 Truth Table for Logical Operators
| A | B | A && B | A || B |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 |
| 1 | 0 | 0 | 1 |
| 1 | 1 | 1 | 1 |
For NOT:
| A | !A |
|---|---|
| 0 | 1 |
| 1 | 0 |
6.8 Short-Circuit Evaluation
Short-circuit evaluation is one of the most useful behaviors of logical
operators. With &&, C evaluates the right side only when the
left side is true. With ||, C evaluates the right side only when the
left side is false.
if (x != 0 && 100 / x > 5)
{
printf("Condition satisfied");
}
Suppose x is 0. The first condition
x != 0 is false. Therefore C does not evaluate
100 / x. This prevents a division-by-zero operation.
Short-circuiting is part of the language behavior, not merely a compiler speed optimization. It is commonly used to safely check pointers, array boundaries, input values, and other conditions before using them.
6.9 Assignment Operator =
The assignment operator stores a value in a modifiable object.
int marks;
marks = 85;
Think of the statement as:
marks
โ
85 is stored here
The expression on the right is evaluated first. The resulting value is then converted to the type of the object on the left and stored there.
int x;
x = 10 + 5;
printf("%d", x); /* 15 */
Assignment is different from comparison:
x = 5; /* put 5 into x */
x == 5; /* ask whether x is equal to 5 */
6.10 Compound Assignment Operators
When a program needs to update an existing value, compound assignment operators make the code shorter and clearer.
| Operator | Example | Conceptual meaning |
|---|---|---|
+= | x += 5 | x = x + 5 |
-= | x -= 5 | x = x - 5 |
*= | x *= 5 | x = x * 5 |
/= | x /= 5 | x = x / 5 |
%= | x %= 5 | x = x % 5 |
&= | x &= mask | Bitwise AND assignment |
|= | x |= mask | Bitwise OR assignment |
^= | x ^= mask | Bitwise XOR assignment |
<<= | x <<= 1 | Left-shift assignment |
>>= | x >>= 1 | Right-shift assignment |
int score = 50;
score += 10; /* 60 */
score -= 5; /* 55 */
score *= 2; /* 110 */
score /= 10; /* 11 */
printf("%d", score);
It reduces repetition and makes the intention of an update obvious. It is especially useful in counters, totals, balances, accumulators, and loops.
6.11 Increment and Decrement Operators
The increment operator ++ increases a value by one. The decrement
operator -- decreases a value by one.
int count = 5;
count++;
printf("%d\n", count); /* 6 */
count--;
printf("%d\n", count); /* 5 */
There are two forms: prefix and postfix.
| Form | Example | Value of the expression | What happens to the variable |
|---|---|---|---|
| Prefix | ++x |
New value | Increment happens before the value is used |
| Postfix | x++ |
Old value | The old value is used, then x is incremented |
int a = 5;
int p = ++a;
/* a = 6, p = 6 */
a = 5;
int q = a++;
/* q = 5, a = 6 */
If you are only changing a variable and not using the expression's value,
count++ and ++count both clearly express an increment.
The important difference appears when the increment expression itself is part
of a larger expression.
6.12 Avoid Tricky Increment Expressions
Beginners often see expressions such as:
printf("%d %d", i++, ++i);
Do not use such expressions in normal programs. When an object is modified more than once in an expression without the sequencing required by the C language, the behavior can be undefined. Even in cases that have defined behavior, such expressions are difficult for humans to read.
Prefer simple statements:
printf("%d\n", i);
i++;
printf("%d\n", i);
Do not try to impress the compiler with a complicated one-line expression. Write code that another programmer can understand and verify.
6.13 Conditional Operator ? :
The conditional operator is C's only ternary operator. It is useful when a program needs to choose between two expressions based on a condition.
int a = 25;
int b = 40;
int max = (a > b) ? a : b;
printf("Maximum = %d", max);
Read it as:
condition ? value_if_true : value_if_false
For the example:
(a > b) ? a : b
โ
Is a greater than b?
โ
No
โ
Choose b
โ
max = 40
Only the selected second or third operand is evaluated. This makes the
conditional operator useful for compact choices, but a normal
if/else statement is usually clearer when the logic
becomes large.
6.14 Bitwise Operators โ Working With Individual Bits
Bitwise operators work directly on the individual bits of integer values.
Instead of treating 5 only as a decimal number, C can work with its binary form:
5 = 0101
- They are common in systems programming, embedded programming and device registers.
- They are useful for flags, permissions, masks, networking and low-level data manipulation.
- They are different from logical operators such as
&&,||and!.
| Operator | Name | What it does | Easy memory trick |
|---|---|---|---|
& | Bitwise AND | 1 only when both bits are 1 | Keep common 1s |
| | Bitwise OR | 1 when at least one bit is 1 | Combine 1s |
^ | Bitwise XOR | 1 when the two bits are different | Different = 1 |
~ | Bitwise NOT | Flips every bit | 0 โ 1 |
<< | Left shift | Moves bits toward the left | Shift left |
>> | Right shift | Moves bits toward the right | Shift right |
5 = 0101& | ^ ~ << >>To understand the operators, compare two small positive integers:
5 = 0101
3 = 0011
Bitwise AND & โ Keep Only Common 1 Bits
1 & 1 โ 1- Every other combination produces
0.
0101 (5)
& 0011 (3)
------
0001 (1)
Only the positions where both numbers contain 1 remain 1.
Bitwise OR | โ Combine 1 Bits
0 | 0 โ 0- If either bit is
1, the result is1.
0101 (5)
| 0011 (3)
------
0111 (7)
Bitwise XOR ^ โ Detect Differences
- Same bits โ
0 - Different bits โ
1
0101 (5)
^ 0011 (3)
------
0110 (6)
Bitwise NOT ~ โ Flip Every Bit
The ~ operator changes every bit:
0 โ 1
1 โ 0
Do not confuse bitwise NOT ~ with logical NOT !.
| Original | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 1 |
|---|---|---|---|---|---|---|---|---|
After ~ | 1 | 1 | 1 | 1 | 1 | 0 | 1 | 0 |
So, conceptually:
5 = 00000101
~5 = 11111010
Why Does ~5 Become -6?
The ~ operator only flips bits. It does not directly say โmake the number negative.โ
The final numeric result depends on how the resulting bit pattern is interpreted.
- With a signed integer, the pattern can represent a negative value.
- With an unsigned integer, the same pattern represents a non-negative value.
- C also performs integer promotions before applying operators such as
~, so the operand's type matters.
5 = 00000000 00000000 00000000 00000101
~5 = 11111111 11111111 11111111 11111010
Notice that the most significant bit is now 1.
On the usual modern two's-complement signed-integer systems, a most significant bit of 1 indicates a negative value. To find its magnitude, invert the bits and add 1:
11111111 11111111 11111111 11111010 original pattern
00000000 00000000 00000000 00000101 invert bits
00000000 00000000 00000000 00000110 add 1
= 6
Therefore the original pattern represents -6.
~x = -x - 1
~5 = -5 - 1
= -6
Use the formula for quick calculations, but understand the bit-level process for interviews and low-level programming.
Signed vs Unsigned: Same Bits, Different Meaning
- Uses a sign representation; modern systems normally use two's complement.
- A leading
1can indicate a negative value. - For a typical 32-bit signed
int,~5is -6.
~5 = 11111111 ... 11111010
โ
-6
- All value bits represent non-negative quantities.
- There is no negative sign interpretation.
- The same flipped bits therefore produce a large positive value.
8-bit concept:
5 = 00000101
~5 = 11111010
= 250
unsigned char are commonly promoted to int before ~ is applied. Always consider the promoted type.
Actual 32-bit Unsigned Example
unsigned int x = 5;
5 = 00000000 00000000 00000000 00000101
~5 = 11111111 11111111 11111111 11111010
Result = 4294967290
For a typical 32-bit unsigned int, the result is 4294967290.
~x = UINT_MAX - x
For an 8-bit unsigned value, the maximum is conceptually 255:
~5 = 255 - 5
= 250
For real C code, the width of unsigned int is implementation-defined, so UINT_MAX from <limits.h> gives the maximum value of that type.
| Case | Pattern after ~5 | Interpretation | Typical result |
|---|---|---|---|
Signed two's-complement int | 11111111 ... 11111010 | Negative value | -6 |
| 8-bit unsigned representation | 11111010 | Positive value | 250 |
Typical 32-bit unsigned int | 11111111 ... 11111010 | Positive value | 4294967290 |
~always flips bits.- The type determines how those bits are interpreted.
- Signed two's-complement interpretation can produce a negative value.
- Unsigned interpretation produces a non-negative value.
- Integer promotions matter for
charandshort.
Bitwise Shift Operators << and >>
<<
5 = 00000101
5 << 1 = 00001010 = 10
Bits move left and zeros enter from the right. For suitable non-negative values, shifting left by one position corresponds to multiplying by 2.
>>
5 = 00000101
5 >> 1 = 00000010 = 2
Bits move right. For non-negative values, a right shift by one position commonly corresponds to integer division by 2.
6.15 Logical Operators vs Bitwise Operators
This is one of the most common C interview and beginner mistakes because the symbols look similar.
| Logical | Bitwise | What it does |
|---|---|---|
&& |
& |
Logical condition vs bit-by-bit AND |
|| |
| |
Logical condition vs bit-by-bit OR |
! |
~ |
Reverse truth value vs flip bits |
int a = 5;
int b = 3;
printf("%d\n", a && b); /* 1: both values are nonzero */
printf("%d\n", a & b); /* 1: 0101 & 0011 = 0001 */
The first expression asks a logical question. The second expression manipulates the binary representation bit by bit.
6.16 Shift Operators << and >>
Shift operators move the bits of an integer value. They are commonly used with masks and low-level data processing.
Left Shift
unsigned int x = 5;
/*
5 = 00000101
5 << 1 = 00001010 = 10
5 << 2 = 00010100 = 20
*/
For suitable unsigned values and valid shift counts, a left shift moves bits toward more significant positions and fills the low-order positions with zeroes. Do not blindly memorize that left shift always means multiplication by two; the statement is only safe under the appropriate value and shift conditions.
Right Shift
unsigned int x = 20;
/*
20 = 00010100
20 >> 1 = 00001010 = 10
20 >> 2 = 00000101 = 5
*/
For unsigned values, right shift moves bits toward less significant positions and fills the high-order positions with zeroes.
The shift amount must be valid for the width of the promoted left operand. Also, signed negative values have additional rules and are best avoided in beginner bit-manipulation examples. Use unsigned integer types when the goal is deliberate bit manipulation.
6.17 Unary Operators You Must Recognize
Unary operators work with one operand. Some of them become especially important when you later learn pointers and dynamic memory.
| Form | Purpose | Example |
|---|---|---|
-x | Unary minus / negation | -10 |
+x | Unary plus | +10 |
!x | Logical NOT | !loggedIn |
~x | Bitwise complement | ~mask |
++x / x++ | Increment | ++count |
--x / x-- | Decrement | count-- |
&x | Address-of | &number |
*ptr | Dereference a pointer | *ptr |
(type)x | Explicit type conversion | (float)x |
The last three forms will become much clearer when you learn pointers and type conversion in detail. For now, recognize them as important C syntax.
6.18 sizeof, Casts, Member Access and Other Important Operators
C has several operators that do not fit into simple arithmetic or logical categories.
| Operator / form | Purpose | Example |
|---|---|---|
sizeof |
Find the size in bytes of a type or object | sizeof(int) |
(type) |
Explicitly convert a value to another type | (float)5 / 2 |
& |
Address-of operator in a pointer context | &x |
* |
Dereference operator in a pointer context | *ptr |
. |
Access a structure/union member through an object | student.age |
-> |
Access a structure/union member through a pointer | ptr->age |
[] |
Array subscripting | marks[2] |
() |
Function call | printf("Hi") |
, |
Comma operator in contexts where the comma operator is used | (x = 1, y = 2) |
sizeof Is an Operator, Not a Function
int x;
printf("%zu\n", sizeof(x));
printf("%zu\n", sizeof(int));
sizeof tells us the size in bytes of a type or object. The exact
number of bytes occupied by a type depends on the implementation, so do not
assume that every C implementation has the same sizes.
Explicit Type Conversion
int total = 5;
int count = 2;
double average = (double) total / count;
printf("%.2f", average); /* 2.50 */
Without the cast, total / count would be integer division because
both operands are integers. The cast changes the calculation so that floating-
point division is performed.
6.19 Type Conversion and Operators
The result of an operator depends not only on the symbol but also on the types of its operands. This is why the following two expressions behave differently:
int a = 5;
int b = 2;
printf("%d\n", a / b); /* 2 */
printf("%.2f\n", (double)a / b); /* 2.50 */
Before performing many arithmetic operations, C applies its rules for integer promotions and usual arithmetic conversions. You do not need to memorize all of those rules at this stage, but you should remember this practical rule: always check the operand types before predicting the result.
Many output-prediction questions are not testing arithmetic itself. They are testing whether you noticed the data types involved in the expression.
6.20 Common Operator Mistakes
| Common mistake | Correct understanding |
|---|---|
= used when comparison is intended |
Use == for equality comparison |
Expecting 7 / 2 to produce 3.5 |
Both operands are integers, so the result is 3 |
Using % with floating-point values |
Use integer operands for %; use fmod() for floating-point remainder |
Confusing && with & |
Logical AND combines conditions; bitwise AND works on bits |
Confusing || with | |
Logical OR combines conditions; bitwise OR works on bits |
Confusing ! with ~ |
! changes truth value; ~ flips bits |
| Assuming prefix and postfix increment are identical | The variable changes by one in both, but the expression values differ |
| Using complicated increment expressions | Separate updates into simple statements |
| Dividing by zero | Check the divisor before division or remainder |
| Ignoring operand types | Check whether the operands are integer, floating-point, signed, or unsigned |
| Assuming every shift is safe | Use valid shift counts and understand signedness |
| Writing very long expressions | Use parentheses or split the calculation into meaningful statements |
6.21 How to Read an Operator Expression Step by Step
When you see a difficult expression in a program or placement question, do not try to calculate everything mentally in one step. Use a systematic process.
Step 1 โ Identify the variables and their types
โ
Step 2 โ Identify each operator
โ
Step 3 โ Check parentheses
โ
Step 4 โ Determine which operation is evaluated first
โ
Step 5 โ Calculate one operation at a time
โ
Step 6 โ Check the resulting type
โ
Step 7 โ Check whether any variable was modified
โ
Step 8 โ Determine the final result
For example:
int a = 10;
int b = 3;
int result = a + b * 2;
Do not simply read from left to right. First identify that multiplication and addition are different operations. The detailed rules that determine grouping are covered in the dedicated Operator Precedence & Associativity topic. For now, use parentheses whenever they make your intended calculation clearer.
int result = a + (b * 2);
6.22 Operator Safety Checklist
- Check the data types of the operands.
- Know the difference between assignment
=and equality==. - Remember that integer division discards the fractional part.
- Use
%for integer remainder calculations. - Never allow an unexpected zero divisor.
- Use
&&and||when combining conditions. - Remember that logical operators short-circuit.
- Do not confuse logical operators with bitwise operators.
- Understand the difference between prefix and postfix increment/decrement.
- Avoid modifying the same variable multiple times in a complicated expression.
- Use unsigned types when deliberately manipulating bit patterns.
- Use valid shift counts.
- Use parentheses when they improve readability.
- Do not memorize output; trace the expression step by step.
6.23 Quick Revision
โ Arithmetic โ + - * / %
๐ Relational โ < <= > >=
โ๏ธ Equality โ == !=
๐ง Logical โ && || !
๐ฅ Assignment โ =
๐ Compound assignment โ += -= *= /= %=
โฌ๏ธ Increment / decrement โ ++ --
โ Conditional โ ? :
๐ข Bitwise โ & | ^ ~ << >>
๐ Size โ sizeof
๐ Cast โ (type)
๐ Pointer-related forms โ & and *
An operator is not just a symbol to memorize. To understand an expression, ask four questions:
- What are the operands?
- What type are the operands?
- What does the operator do?
- What value or side effect does the expression produce?
Once you can answer those four questions, C expressions become much easier to read, debug, and predict.
6.20 Quick MCQs
Select an answer first, then click Check Answer. A correct choice becomes green. If the choice is wrong, it becomes red and the correct option is shown in green. The correct answer and explanation appear below after checking.
The modulus operator % gives the remainder of integer division. 10 divided by 3 leaves a remainder of 1.
== compares two values for equality. The single = operator performs assignment.
Because both operands are int, integer division is performed. The fractional part is discarded, so the result is 2.
&& is logical AND. & is the bitwise AND operator.
++a increments a by one. Starting from 5, a becomes 6.
^ is the bitwise XOR operator in C.
6.21 ๐ฏ Programming Problems
Try every problem yourself first. Use ๐ป Solve It Yourself to write and test your C code. Open Hint only when needed, and use Show Program after attempting the problem.
Problem 1: Read two integers and print their sum, difference, product, quotient and remainder.
Input: Two integers a and b, where b is not zero.
Output: Print sum, difference, product, integer quotient and remainder.
C Code Editor
Sample Input
Program Output
Run your program to see the output.
Test Cases
#include <stdio.h>
int main()
{
int a, b;
scanf("%d %d", &a, &b);
printf("Sum = %d\n", a + b);
printf("Difference = %d\n", a - b);
printf("Product = %d\n", a * b);
printf("Quotient = %d\n", a / b);
printf("Remainder = %d", a % b);
return 0;
}
Problem 2: Check whether a given integer is even or odd using the modulus operator.
Input: One integer n.
Output: Print "Even" or "Odd".
C Code Editor
Sample Input
Program Output
Run your program to see the output.
Test Cases
#include <stdio.h>
int main()
{
int n;
scanf("%d", &n);
printf("%s", (n % 2 == 0) ? "Even" : "Odd");
return 0;
}
Problem 3: Read three numbers and find the largest using relational and logical operators.
Input: Three integers a, b and c.
Output: Print the largest value.
C Code Editor
Sample Input
Program Output
Run your program to see the output.
Test Cases
#include <stdio.h>
int main()
{
int a, b, c;
int largest;
scanf("%d %d %d", &a, &b, &c);
if (a >= b && a >= c)
largest = a;
else if (b >= a && b >= c)
largest = b;
else
largest = c;
printf("Largest = %d", largest);
return 0;
}
Problem 4: Demonstrate the difference between pre-increment and post-increment.
Input: One integer a.
Output: Using separate copies of the input, print the result of pre-increment and post-increment.
C Code Editor
Sample Input
Program Output
Run your program to see the output.
Test Cases
#include <stdio.h>
int main()
{
int a;
int x, y;
int pre, post;
scanf("%d", &a);
x = a;
y = a;
pre = ++x;
post = y++;
printf("Pre: value = %d, variable = %d\n", pre, x);
printf("Post: value = %d, variable = %d", post, y);
return 0;
}
Problem 5: Check whether a number lies between 10 and 100 using logical operators.
Input: One integer n.
Output: Print "Inside Range" when 10 <= n <= 100, otherwise print "Outside Range".
C Code Editor
Sample Input
Program Output
Run your program to see the output.
Test Cases
#include <stdio.h>
int main()
{
int n;
scanf("%d", &n);
printf("%s",
(n >= 10 && n <= 100)
? "Inside Range"
: "Outside Range");
return 0;
}
Problem 6: Find the larger of two numbers using the conditional operator.
Input: Two integers a and b.
Output: Print the larger value.
C Code Editor
Sample Input
Program Output
Run your program to see the output.
Test Cases
#include <stdio.h>
int main()
{
int a, b;
int larger;
scanf("%d %d", &a, &b);
larger = (a > b) ? a : b;
printf("Larger = %d", larger);
return 0;
}
Problem 7: Perform AND, OR and XOR operations on two integers.
Input: Two non-negative integers a and b.
Output: Print the results of a & b, a | b and a ^ b.
C Code Editor
Sample Input
Program Output
Run your program to see the output.
Test Cases
#include <stdio.h>
int main()
{
int a, b;
scanf("%d %d", &a, &b);
printf("AND = %d\n", a & b);
printf("OR = %d\n", a | b);
printf("XOR = %d", a ^ b);
return 0;
}
Problem 8: Demonstrate left-shift and right-shift operations using a positive integer.
Input: A positive integer n and a non-negative shift count k.
Output: Print n << k and n >> k.
C Code Editor
Sample Input
Program Output
Run your program to see the output.
Test Cases
#include <stdio.h>
int main()
{
unsigned int n, k;
scanf("%u %u", &n, &k);
printf("Left Shift = %u\n", n << k);
printf("Right Shift = %u", n >> k);
return 0;
}
6.22 Key Takeaway
Before solving a coding problem, identify:
What calculation is required?
What comparison is required?
Are multiple conditions involved?
Is integer or floating-point arithmetic required?
Once you understand operators well, decision-making and loops become much easier.
๐ฌ Operators โ Expression Evaluation
Follow how C evaluates
result = a + b * 2
when a = 10 and b = 3.
๐ฌ Operator Evaluation Visualizer
See which operation is evaluated first and how the final value is produced.
a + b * 2
3 * 2 = 6
10 + 6 = 16
result = 16
๐ Program Tracing โ Operators
Trace the important difference between pre-increment and post-increment.
โ
๐ค Operators โ Interview Questions
๐ก Operators โ Extra Tips
- Be very clear about the difference between = and ==. It is one of the most common beginner mistakes.
- Memorize the behavior of integer division and modulus with simple examples.
- Know that && and || use short-circuit evaluation. This is frequently tested in output-prediction questions.
- For ++a and a++, first ask whether the value of the expression itself is being used.
- Do not mix several modifications of the same object into one complicated expression in interview code. Prefer simple, separately sequenced statements.
- For bitwise questions, convert small positive integers to binary and trace each bit position.
โ๏ธ Operators โ Extra Practice Questions
-
Predict the values of
aandbafterint a = 5; int b = ++a;. -
Predict the values of
aandbafterint a = 5; int b = a++;. -
Explain why
x != 0 && 10 / x > 2can safely avoid division by zero. -
Evaluate
10 + 3 * 2and explain which operator is applied first. -
For
a = 12andb = 10, calculatea & b,a | b, anda ^ b. -
Rewrite
if (a > b) max = a; else max = b;using the conditional operator.