Loading...
Loading...
Phase 1: Technical Theory | Date: 23 July 2026 | Subject: C Programming Basics | Expected Questions: 6–7
Today you will study:
| Hour | Topic |
|---|---|
| 1 | C basics, data types, variables, constants, and sizeof |
| 2 | Operators |
| 3 | Precedence, associativity, and output tracing |
| 4 | Decision statements |
| 5 | Loops, break, continue, and output tracing |
C is a general-purpose programming language.
It was developed by Dennis Ritchie at Bell Laboratories in the early 1970s.
C is widely used for:
C provides:
C is often called a middle-level language because it supports:
This is a textbook description, not an official language category.
A C program is written as source code.
A C compiler translates the source code into a form the computer can execute.
C Source Program
│
v
Preprocessor
│
v
Compiler
│
v
Assembler
│
v
Object Code
│
v
Linker
│
v
Executable Program
A C source file commonly uses the extension:
.c
Example:
program.c
A simple C program is:
#include <stdio.h>
int main(void)
{
printf("Hello");
return 0;
}
#include <stdio.h>
This includes declarations from the standard input/output header.
int main(void)
Execution of a hosted C program begins with the main function.
{
}
Braces create a block of statements.
printf("Hello");
printf displays formatted output.
;
A semicolon normally ends a C statement.
return 0;
Returning 0 from main indicates successful program termination.
C treats uppercase and lowercase letters as different.
These identifiers are different:
age
Age
AGE
These keywords are not the same:
int
INT
Only lowercase int is a C keyword.
A token is the smallest meaningful unit of a C program.
Main token categories are:
int total = 25;
Tokens are:
int
total
=
25
;
A keyword is a reserved word with a predefined meaning in C.
Examples:
| Keyword | Purpose |
|---|---|
| int | Integer type |
| char | Character type |
| float | Floating-point type |
| double | Higher-precision floating-point type |
| if | Decision statement |
| else | Alternative decision |
| switch | Multi-way selection |
| case | Case label |
| for | Loop |
| while | Loop |
| do | Loop |
| break | Terminates loop or switch |
| continue | Skips to next loop iteration |
| return | Returns from a function |
| void | Indicates no value or no parameters in specific contexts |
| const | Makes an object non-modifiable through that name |
| sizeof | Gives size in bytes |
A keyword cannot normally be used as a variable name.
Invalid:
int for = 10;
An identifier is a name given to a program element.
Examples:
An identifier:
| Identifier | Valid? | Reason |
|---|---|---|
| total | Yes | Contains letters |
| total_marks | Yes | Underscore is allowed |
| marks2 | Yes | Digit is not first |
| _count | Yes | May begin with underscore, though some underscore names are reserved |
| 2marks | No | Begins with a digit |
| total marks | No | Contains a space |
| total-marks | No | Contains a hyphen |
| float | No | It is a keyword |
Use meaningful names:
totalMarks
student_count
average
Avoid unclear names:
x1
aaa
qqq
Short names such as i and j are commonly used as loop counters.
Comments are notes for programmers. They are ignored during normal program execution.
// This is a single-line comment
Single-line comments are supported in C99 and later.
/* This is
a multi-line comment */
Traditional C comments cannot be nested safely.
Invalid idea:
/* Outer comment /* Inner comment */ */
A data type tells the compiler:
| Data Type | Main Use | Example |
|---|---|---|
| char | Character or small integer | 'A' |
| int | Integer | 25 |
| float | Single-precision floating-point value | 3.14f |
| double | Double-precision floating-point value | 3.14 |
| void | Absence of a value | Function returning nothing |
The char type stores a character code or a small integer.
Example:
char grade = 'A';
A character constant uses single quotation marks:
'A'
'7'
'@'
Characters are stored internally as numeric codes.
In ASCII:
'A' = 65
'a' = 97
'0' = 48
Example:
char ch = 'A';
printf("%d", ch);
Output on an ASCII-based system:
65
In C:
sizeof(char) = 1
This is always one C byte.
A C byte contains at least 8 bits. On most modern systems, one byte is 8 bits.
A plain char may behave as signed or unsigned depending on the implementation.
The int type stores whole numbers.
Examples:
int age = 20;
int temperature = -5;
int total = 1000;
It does not store a fractional part.
Examples:
short int a;
unsigned int b;
long int c;
long long int d;
These shorter forms are also valid:
short a;
unsigned b;
long c;
long long d;
A signed integer can represent:
An unsigned integer represents:
For an unsigned n-bit integer:
Range = 0 to 2ⁿ − 1
Example for 8 bits:
0 to 2⁸ − 1
= 0 to 255
For a typical n-bit two’s-complement signed integer:
Range = −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1
Example for 8 bits:
−128 to 127
The float type stores real numbers with fractional parts.
Example:
float price = 12.5f;
The suffix f makes 12.5f a float constant.
Without f, a decimal floating constant such as 12.5 has type double.
A float commonly provides about 6–7 significant decimal digits.
Example:
float x = 3.1415926f;
The double type stores floating-point values with greater precision than float.
Example:
double distance = 12345.6789;
A double commonly provides about 15–16 significant decimal digits.
long double provides at least as much precision as double.
Example:
long double value = 3.14L;
The suffix L indicates a long double constant.
void means the absence of a value in specific contexts.
void showMessage(void)
{
printf("Hello");
}
int main(void)
Here, void means the function accepts no parameters.
Standard C does not define sizeof(void). Some compilers accept it as an extension, but it should not be used in standard C.
The C language does not fix one exact byte size for every data type.
The exact size can depend on:
The only exact basic rule here is:
sizeof(char) = 1
The following relationships are guaranteed:
sizeof(char) ≤ sizeof(short)
sizeof(short) ≤ sizeof(int)
sizeof(int) ≤ sizeof(long)
sizeof(long) ≤ sizeof(long long)
| Type | Common Size | Typical Purpose |
|---|---|---|
| char | 1 byte | Character or small integer |
| short | 2 bytes | Small integer |
| int | 4 bytes | General integer |
| long | 4 or 8 bytes | Larger integer |
| long long | 8 bytes | Large integer |
| float | 4 bytes | Single-precision real number |
| double | 8 bytes | Double-precision real number |
| long double | 8, 12, or 16 bytes | Extended precision |
Many basic examinations expect:
| Data Type | Commonly Expected Size |
|---|---|
| char | 1 byte |
| short int | 2 bytes |
| int | 4 bytes |
| long int | 4 or 8 bytes |
| long long int | 8 bytes |
| float | 4 bytes |
| double | 8 bytes |
| long double | Implementation-dependent |
If a question names a compiler or architecture, use the sizes for that environment.
| Type | Minimum Required Range |
|---|---|
| signed char | −127 to 127 |
| unsigned char | 0 to 255 |
| short | −32767 to 32767 |
| unsigned short | 0 to 65535 |
| int | At least −32767 to 32767 |
| unsigned int | At least 0 to 65535 |
| long | At least −2147483647 to 2147483647 |
| unsigned long | At least 0 to 4294967295 |
| long long | At least −9223372036854775807 to 9223372036854775807 |
Modern two’s-complement systems commonly include the additional most-negative value, such as −128 for 8-bit signed char.
The sizeof operator returns the size of a type or object in bytes.
sizeof(type)
or:
sizeof expression
sizeof(char)
sizeof(int)
sizeof(double)
int x;
sizeof(x)
#include <stdio.h>
int main(void)
{
printf("%zu", sizeof(int));
return 0;
}
%zu is the correct printf format specifier for the result of sizeof.
Example:
int x = 5;
printf("%zu %d", sizeof(x++), x);
For an ordinary non-variable-length object, x++ is not executed inside sizeof.
Typical output when int is 4 bytes:
4 5
int a[10];
sizeof(a)
If int is 4 bytes:
sizeof(a) = 10 × 4 = 40 bytes
Within the same scope where a is an actual array:
Number of elements = sizeof(a) / sizeof(a[0])
A variable is a named memory location whose stored value can change.
int age;
This tells the compiler that age is an integer variable.
int age = 20;
Initialization gives the variable its first value when it is created.
age = 25;
Assignment changes its value.
int a, b, c;
int a = 10, b = 20, c = 30;
A local automatic variable that is not initialized has an indeterminate value.
Example:
int main(void)
{
int x;
printf("%d", x);
}
This program has undefined behaviour because x is read without initialization.
Always initialize variables before reading them.
Correct:
int x = 0;
A constant is a fixed value that does not change during program execution.
Examples:
10
-25
1000
| Form | Example | Meaning |
|---|---|---|
| Decimal | 25 | Decimal 25 |
| Octal | 031 | Decimal 25 |
| Hexadecimal | 0x19 | Decimal 25 |
An integer literal beginning with 0 is traditionally interpreted as octal.
010 = decimal 8
019 is invalid as an octal integer because 9 is not an octal digit.
Examples:
3.14
-0.25
6.02e23
1.5f
2.0L
Scientific notation:
6.02e23 = 6.02 × 10²³
Suffixes:
A character constant uses single quotes:
'A'
'7'
'\n'
A character constant represents an integer character code.
A string literal uses double quotes:
"Hello"
"A"
"123"
| Character Constant | String Literal |
|---|---|
| 'A' | "A" |
| Single quotes | Double quotes |
| Represents one character code | Represents a sequence ending with a null character |
An escape sequence represents a special character.
| Escape Sequence | Meaning |
|---|---|
| \n | New line |
| \t | Horizontal tab |
| \b | Backspace |
| \r | Carriage return |
| \ | Backslash |
| ' | Single quotation mark |
| " | Double quotation mark |
| \0 | Null character |
Example:
printf("A\nB");
Output:
A
B
const int MAX = 100;
MAX should not later be assigned another value through that name.
Invalid:
MAX = 200;
#define PI 3.14159
The preprocessor replaces PI with 3.14159 before compilation.
| const | #define |
|---|---|
| Declares a typed object | Creates a preprocessor macro |
| Processed by compiler | Processed before compilation |
| Obeys scope rules | Macro scope follows preprocessing rules |
| Has a data type | Simple replacement has no C data type by itself |
printf displays formatted output.
printf("Hello");
| Data Type | printf Format |
|---|---|
| int | %d or %i |
| unsigned int | %u |
| long int | %ld |
| long long int | %lld |
| char | %c |
| float expression | %f |
| double | %f |
| long double | %Lf |
| String | %s |
| Hexadecimal integer | %x or %X |
| Octal integer | %o |
| sizeof result | %zu |
int age = 20;
printf("%d", age);
Output:
20
scanf reads formatted input.
int age;
scanf("%d", &age);
The & operator gives the address of age.
| Data Type | scanf Format |
|---|---|
| int | %d |
| char | %c |
| float | %f |
| double | %lf |
| long double | %Lf |
Important:
Type conversion changes a value from one data type to another.
The compiler performs it automatically.
int x = 5;
double y = x;
x is converted to double.
The programmer requests conversion using a cast.
double result = (double)5 / 2;
Without the cast:
5 / 2 = 2
With the cast:
(double)5 / 2 = 2.5
(target_type) expression
Example:
(float)a
(int)x
An operator is a symbol that performs an operation on one or more operands.
Example:
a + b
| Operator | Operation | Example |
|---|---|---|
| + | Addition | a + b |
| - | Subtraction | a - b |
| * | Multiplication | a * b |
| / | Division | a / b |
| % | Remainder | a % b |
When both operands are integers, the result is an integer.
The fractional part is discarded toward zero.
7 / 2 = 3
9 / 4 = 2
-7 / 2 = -3
To get a floating-point result:
7.0 / 2 = 3.5
or:
(double)7 / 2 = 3.5
The % operator returns the remainder after integer division.
7 % 2 = 1
10 % 3 = 1
15 % 5 = 0
The operands of % must have integer types.
Invalid:
5.5 % 2.0
if (number % 2 == 0)
printf("Even");
else
printf("Odd");
Division or remainder by integer zero is undefined.
Invalid:
10 / 0
10 % 0
Unary operators work on one operand.
int a = 5;
int b = -a;
Now b is −5.
Examples:
+a
-a
Relational operators compare values.
| Operator | Meaning |
|---|---|
| < | Less than |
| > | Greater than |
| <= | Less than or equal to |
| >= | Greater than or equal to |
| == | Equal to |
| != | Not equal to |
In C:
Examples:
5 > 3 gives 1
5 < 3 gives 0
5 == 5 gives 1
5 != 5 gives 0
= assignment
== equality comparison
Example:
x = 5;
stores 5 in x.
x == 5
checks whether x equals 5.
Logical operators combine or reverse conditions.
| Operator | Meaning |
|---|---|
| && | Logical AND |
| || | Logical OR |
| ! | Logical NOT |
In C:
A && B is true only when both operands are true.
| A | B | A && B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
Example:
(5 > 2) && (8 > 3)
= 1 && 1
= 1
A || B is true when at least one operand is true.
| A | B | A || B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
! reverses the logical value.
!0 = 1
!1 = 0
!25 = 0
For:
A && B
If A is false, B is not evaluated.
Example:
int x = 0;
int y = 0 && ++x;
x remains 0.
For:
A || B
If A is true, B is not evaluated.
Example:
int x = 0;
int y = 1 || ++x;
x remains 0.
Bitwise operators work on individual bits of integer operands.
| Operator | Meaning |
|---|---|
| & | Bitwise AND |
| | | Bitwise OR |
| ^ | Bitwise XOR |
| ~ | Bitwise complement |
| << | Left shift |
| >> | Right shift |
Do not confuse:
| Logical | Bitwise |
|---|---|
| && | & |
| || | | |
| Works with truth values | Works on individual bits |
A result bit is 1 only when both input bits are 1.
| A | B | A & B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
Example:
12 = 1100
10 = 1010
1100
& 1010
------
1000 = 8
Therefore:
12 & 10 = 8
A result bit is 1 when at least one input bit is 1.
12 = 1100
10 = 1010
1100
| 1010
------
1110 = 14
Therefore:
12 | 10 = 14
XOR produces 1 when the bits are different.
| A | B | A ^ B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
Example:
12 = 1100
10 = 1010
1100
^ 1010
------
0110 = 6
Therefore:
12 ^ 10 = 6
x ^ 0 = x
x ^ x = 0
~ changes every bit:
On a common two’s-complement system:
~x = −x − 1
Example:
~5 = −6
The exact visible bit pattern depends on the integer width.
x << n
Shifts bits n positions to the left.
For a non-negative value when the result is representable:
x << n is equivalent to x × 2ⁿ
Example:
5 << 1
5 = 0101
1010 = 10
Therefore:
5 << 1 = 10
x >> n
Shifts bits n positions to the right.
For non-negative integers:
x >> n is equivalent to integer division by 2ⁿ
Example:
20 >> 2
20 / 4 = 5
Therefore:
20 >> 2 = 5
Avoid relying on right-shift details for negative signed integers in basic output questions because the result is implementation-defined.
x = 10;
The right-side value is stored in the left-side object.
Assignment associates from right to left.
a = b = 5;
This works as:
a = (b = 5);
Both a and b become 5.
| Operator | Meaning |
|---|---|
| += | Add and assign |
| -= | Subtract and assign |
| *= | Multiply and assign |
| /= | Divide and assign |
| %= | Remainder and assign |
| &= | Bitwise AND and assign |
| |= | Bitwise OR and assign |
| ^= | Bitwise XOR and assign |
| <<= | Left shift and assign |
| >>= | Right shift and assign |
Examples:
x += 3;
means approximately:
x = x + 3;
If x is 5, it becomes 8.
The ++ operator increases a variable by 1.
x++;
or:
++x;
The -- operator decreases a variable by 1.
x--;
or:
--x;
++x
The value is increased before the expression uses it.
Example:
int x = 5;
int y = ++x;
Result:
x = 6
y = 6
x++
The old value is used first, and then x is increased.
Example:
int x = 5;
int y = x++;
Result:
y = 5
x = 6
int x = 5;
int y = --x;
Result:
x = 4
y = 4
int x = 5;
int y = x--;
Result:
y = 5
x = 4
Do not try to predict expressions that modify the same variable multiple times without proper sequencing.
Examples to avoid:
i = i++ + ++i;
printf("%d %d", i++, i);
a[i] = i++;
Such expressions may have undefined behaviour. They do not have one reliable output.
Use separate statements:
int old = i;
i++;
printf("%d %d", old, i);
The conditional operator uses three operands.
condition ? expression_if_true : expression_if_false
int max = (a > b) ? a : b;
If a is greater than b, max receives a. Otherwise, max receives b.
int x = 10;
printf("%s", (x % 2 == 0) ? "Even" : "Odd");
Output:
Even
It is called the ternary operator because it uses three operands.
The comma operator evaluates expressions from left to right and produces the value of the last expression.
Example:
int x;
x = (2, 4, 6);
x becomes:
6
Do not confuse the comma operator with commas used to separate function arguments or declarations.
Precedence decides which operator binds more strongly when an expression contains different operators.
Example:
2 + 3 * 4
Multiplication has higher precedence:
2 + (3 * 4)
= 2 + 12
= 14
Not:
(2 + 3) * 4 = 20
Associativity decides the grouping direction when operators have the same precedence.
Multiplication and division have the same precedence and associate left to right:
24 / 6 * 2
= (24 / 6) * 2
= 4 * 2
= 8
Assignment associates right to left:
a = b = 10
Grouping:
a = (b = 10)
The table goes from highest precedence to lowest.
| Level | Operators | Meaning | Associativity |
|---|---|---|---|
| 1 | () [] . -> postfix ++ postfix -- | Postfix operations | Left to right |
| 2 | prefix ++ prefix -- + - ! ~ (type) * & sizeof | Unary operations | Right to left |
| 3 | * / % | Multiplication, division, remainder | Left to right |
| 4 | + - | Addition, subtraction | Left to right |
| 5 | << >> | Bit shifts | Left to right |
| 6 | < <= > >= | Relational | Left to right |
| 7 | == != | Equality | Left to right |
| 8 | & | Bitwise AND | Left to right |
| 9 | ^ | Bitwise XOR | Left to right |
| 10 | | | Bitwise OR | Left to right |
| 11 | && | Logical AND | Left to right |
| 12 | || | Logical OR | Left to right |
| 13 | ?: | Conditional | Right to left |
| 14 | = += -= *= /= %= &= ^= |= <<= >>= | Assignment | Right to left |
| 15 | , | Comma | Left to right |
Memorize this practical order:
5 + 2 * 3
= 5 + 6
= 11
(5 + 2) * 3
= 7 * 3
= 21
20 / 5 * 2
Same precedence, left to right:
= (20 / 5) * 2
= 4 * 2
= 8
10 - 4 - 2
Left to right:
= (10 - 4) - 2
= 6 - 2
= 4
5 > 3 && 2 < 4
Relational operators are evaluated before logical AND:
= 1 && 1
= 1
5 == 5 || 3 > 8
= 1 || 0
= 1
4 + 2 << 1
Addition has higher precedence than shift:
= (4 + 2) << 1
= 6 << 1
= 12
10 & 6 == 2
Equality has higher precedence than bitwise AND:
= 10 & (6 == 2)
= 10 & 0
= 0
Use parentheses when the intended grouping could be unclear:
(10 & 6) == 2
10 & 6 = 2
2 == 2 = 1
Do not depend on memory alone for complicated expressions.
Use parentheses:
result = (a + b) * c;
Parentheses improve:
A control statement changes the normal order in which statements execute.
Decision statements include:
if (condition)
{
statement;
}
If the condition is non-zero, the block runs.
If the condition is zero, the block is skipped.
int age = 20;
if (age >= 18)
{
printf("Adult");
}
Output:
Adult
C treats:
Example:
if (5)
printf("Yes");
Output:
Yes
Example:
if (0)
printf("Yes");
No output is produced.
if (condition)
{
statements_if_true;
}
else
{
statements_if_false;
}
int number = 7;
if (number % 2 == 0)
printf("Even");
else
printf("Odd");
Output:
Odd
If an if or else controls only one statement, braces are optional.
if (x > 0)
printf("Positive");
For multiple statements, use braces:
if (x > 0)
{
printf("Positive");
x++;
}
Recommended practice: use braces even for one statement.
if (x > 0)
printf("Positive");
printf("Checked");
Only the first printf belongs to the if statement.
The second printf runs regardless of the condition.
Correct:
if (x > 0)
{
printf("Positive");
printf("Checked");
}
A nested if is an if statement inside another if statement.
int x = 15;
if (x > 0)
{
if (x % 2 == 0)
printf("Positive even");
else
printf("Positive odd");
}
else
{
printf("Not positive");
}
Output:
Positive odd
An else is matched with the nearest unmatched if.
Example:
if (a > 0)
if (b > 0)
printf("Both positive");
else
printf("b is not positive");
The else belongs to:
if (b > 0)
Use braces to make the intended structure clear.
Use an else-if ladder when testing several conditions in order.
if (condition1)
{
statements1;
}
else if (condition2)
{
statements2;
}
else if (condition3)
{
statements3;
}
else
{
default_statements;
}
int marks = 75;
if (marks >= 90)
printf("A");
else if (marks >= 75)
printf("B");
else if (marks >= 60)
printf("C");
else
printf("D");
Output:
B
Only the first true branch runs.
Order matters.
Incorrect order:
if (marks >= 40)
printf("Pass");
else if (marks >= 75)
printf("Distinction");
A mark of 80 satisfies marks >= 40 first, so the second test is never reached.
A switch selects one path using the value of an integer, character, or enumeration expression.
switch (expression)
{
case constant1:
statements;
break;
case constant2:
statements;
break;
default:
statements;
}
int day = 2;
switch (day)
{
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
case 3:
printf("Wednesday");
break;
default:
printf("Invalid");
}
Output:
Tuesday
Example:
switch (grade)
{
case 'A':
printf("Excellent");
break;
case 'B':
printf("Good");
break;
}
Without break, execution continues into later cases.
Example:
int x = 2;
switch (x)
{
case 1:
printf("A");
case 2:
printf("B");
case 3:
printf("C");
break;
default:
printf("D");
}
Output:
BC
Execution begins at case 2 and continues through case 3 until break.
Several cases can share one block.
switch (ch)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
printf("Vowel");
break;
default:
printf("Not a lowercase vowel");
}
| if-else | switch |
|---|---|
| Supports ranges and complex conditions | Compares one expression with constant case values |
| Can use relational and logical expressions | Uses equality-like matching |
| Suitable for few or complex choices | Suitable for many fixed choices |
| Can test floating-point conditions | switch expression cannot have floating type |
| No automatic fall-through | Fall-through occurs without break |
A loop repeats a block of statements.
C provides:
Most loops contain:
Example:
int i = 1;
while (i <= 5)
{
printf("%d ", i);
i++;
}
The while loop tests the condition before executing the body.
initialization;
while (condition)
{
statements;
update;
}
int i = 1;
while (i <= 5)
{
printf("%d ", i);
i++;
}
Output:
1 2 3 4 5
while is an entry-controlled loop because it checks the condition before entering the body.
It may execute zero times.
Example:
int i = 10;
while (i < 5)
{
printf("%d", i);
}
The body does not run.
The do-while loop executes the body first and tests the condition afterward.
initialization;
do
{
statements;
update;
}
while (condition);
A semicolon is required after the while condition.
int i = 1;
do
{
printf("%d ", i);
i++;
}
while (i <= 5);
Output:
1 2 3 4 5
do-while is an exit-controlled loop.
Its body executes at least once.
Example:
int i = 10;
do
{
printf("%d", i);
}
while (i < 5);
Output:
10
A for loop places initialization, condition, and update in one line.
for (initialization; condition; update)
{
statements;
}
int i;
for (i = 1; i <= 5; i++)
{
printf("%d ", i);
}
Output:
1 2 3 4 5
Initialization
│
v
Condition ─── false ───> Exit
│
true
│
v
Body
│
v
Update
│
└────────────> Condition
Initialization runs once.
Condition is checked before every iteration.
Update runs after each completed iteration.
for (i = 5; i >= 1; i--)
printf("%d ", i);
Output:
5 4 3 2 1
for (i = 0; i <= 10; i += 2)
printf("%d ", i);
Output:
0 2 4 6 8 10
for (i = 0, j = 5; i < j; i++, j--)
{
printf("%d %d\n", i, j);
}
Each of the three sections can be omitted.
for (;;)
{
statements;
}
This creates an infinite loop unless something such as break terminates it.
| Feature | for | while | do-while |
|---|---|---|---|
| Condition checked | Before body | Before body | After body |
| Minimum executions | 0 | 0 | 1 |
| Type | Entry-controlled | Entry-controlled | Exit-controlled |
| Best use | Known or structured repetition | Condition-based repetition | Body must run at least once |
| Semicolon after condition | No | No | Yes |
A loop inside another loop is a nested loop.
int i, j;
for (i = 1; i <= 2; i++)
{
for (j = 1; j <= 3; j++)
{
printf("*");
}
printf("\n");
}
Output:
***
***
If the outer loop runs m times and the inner loop runs n times for each outer iteration:
Total inner executions = m × n
Example:
Outer loop = 3 times
Inner loop = 4 times
Total = 3 × 4 = 12
An infinite loop does not terminate naturally.
Examples:
while (1)
{
}
for (;;)
{
}
Accidental infinite loop:
int i = 1;
while (i <= 5)
{
printf("%d", i);
}
i never changes, so the condition remains true.
break immediately terminates the nearest enclosing:
int i;
for (i = 1; i <= 10; i++)
{
if (i == 5)
break;
printf("%d ", i);
}
Output:
1 2 3 4
When i becomes 5, break ends the loop.
break exits only the nearest enclosing loop.
for (i = 1; i <= 2; i++)
{
for (j = 1; j <= 5; j++)
{
if (j == 3)
break;
printf("%d ", j);
}
}
The outer loop continues.
continue skips the rest of the current iteration and moves to the next iteration.
int i;
for (i = 1; i <= 5; i++)
{
if (i == 3)
continue;
printf("%d ", i);
}
Output:
1 2 4 5
In a while loop, update the loop variable before continue if required.
Correct:
int i = 0;
while (i < 5)
{
i++;
if (i == 3)
continue;
printf("%d ", i);
}
Output:
1 2 4 5
| break | continue |
|---|---|
| Terminates the loop completely | Skips only the current iteration |
| Control moves after the loop | Control moves to the next iteration |
| Also used in switch | Not used to exit switch |
| Nearest enclosing loop or switch | Nearest enclosing loop |
Mnemonic:
int i;
int sum = 0;
for (i = 1; i <= n; i++)
{
sum += i;
}
For n = 5:
sum = 1 + 2 + 3 + 4 + 5 = 15
The factorial of n is:
n! = n × (n − 1) × ... × 2 × 1
Example:
5! = 5 × 4 × 3 × 2 × 1 = 120
Program pattern:
int i;
int fact = 1;
for (i = 1; i <= n; i++)
{
fact *= i;
}
int count = 0;
while (n != 0)
{
n /= 10;
count++;
}
For n = 538:
538 → 53 → 5 → 0
Count:
3
int reverse = 0;
while (n != 0)
{
int digit = n % 10;
reverse = reverse * 10 + digit;
n /= 10;
}
For n = 123:
reverse = 321
Use this process for every output question:
For:
int i;
int sum = 0;
for (i = 1; i <= 3; i++)
sum += i;
| Iteration | i | sum before | sum after |
|---|---|---|---|
| 1 | 1 | 0 | 1 |
| 2 | 2 | 1 | 3 |
| 3 | 3 | 3 | 6 |
Final sum:
6
#include <stdio.h>
int main(void)
{
statements;
return 0;
}
| Type | Common Size | Use |
|---|---|---|
| char | 1 byte | Character/small integer |
| short | 2 bytes | Small integer |
| int | 4 bytes | Integer |
| long | 4 or 8 bytes | Larger integer |
| long long | 8 bytes | Large integer |
| float | 4 bytes | Single-precision real |
| double | 8 bytes | Double-precision real |
Only this is universally exact:
sizeof(char) = 1
Other exact sizes depend on the implementation.
| Type | printf | scanf |
|---|---|---|
| int | %d | %d |
| unsigned int | %u | %u |
| char | %c | %c |
| float | %f | %f |
| double | %f | %lf |
| long int | %ld | %ld |
| long long int | %lld | %lld |
| long double | %Lf | %Lf |
| sizeof result | %zu | — |
+ - * / %
< <= > >= == !=
&& || !
& | ^ ~ << >>
= += -= *= /= %=
++x x++ --x x--
condition ? true_value : false_value
| Statement | Use |
|---|---|
| if | One conditional path |
| if-else | Two paths |
| else-if ladder | Multiple conditions |
| nested if | Decision inside a decision |
| switch | Fixed constant choices |
| Loop | Condition | Minimum Execution |
|---|---|---|
| for | Before body | 0 |
| while | Before body | 0 |
| do-while | After body | 1 |
Who developed the C programming language?
A. James Gosling
B. Dennis Ritchie
C. Bjarne Stroustrup
D. Guido van Rossum
Which is a valid C identifier?
A. 2marks
B. total-marks
C. total_marks
D. float
Which statement is guaranteed by standard C?
A. sizeof(int) is always 2
B. sizeof(int) is always 4
C. sizeof(char) is always 1
D. sizeof(double) is always 8
Which format specifier is used with printf for a value returned by sizeof?
A. %d
B. %f
C. %zu
D. %c
Which operator tests equality?
A. =
B. ==
C. !=
D. <=
Which operator performs bitwise XOR?
A. &&
B. ||
C. ^
D. ~
Which operator has higher precedence?
A. Addition over multiplication
B. Logical OR over logical AND
C. Multiplication over addition
D. Assignment over equality
Which loop always executes its body at least once?
A. for
B. while
C. do-while
D. None of these
Which statement immediately terminates the nearest enclosing loop?
A. continue
B. break
C. case
D. sizeof
Which statement about switch is correct?
A. Its expression must always be a float.
B. Duplicate case values are permitted.
C. break prevents fall-through to later cases.
D. default is compulsory.
Assume stdio.h is included and each fragment is placed correctly inside main.
What is the output?
printf("%d", 2 + 3 * 4);
A. 14
B. 20
C. 24
D. 11
What is the output?
printf("%d", (2 + 3) * 4);
A. 14
B. 18
C. 20
D. 24
What is the output?
int x = 7 / 2;
printf("%d", x);
A. 3
B. 3.5
C. 4
D. 2
What is the output?
printf("%.1f", 7.0 / 2);
A. 3.0
B. 3.5
C. 4.0
D. 7.0
What is the output?
printf("%d", 17 % 5);
A. 2
B. 3
C. 4
D. 5
What is the output?
int x = 5;
int y = x++;
printf("%d %d", x, y);
A. 5 5
B. 5 6
C. 6 5
D. 6 6
What is the output?
int x = 5;
int y = ++x;
printf("%d %d", x, y);
A. 5 5
B. 5 6
C. 6 5
D. 6 6
What is the output?
printf("%d", (5 > 3) && (2 < 1));
A. 0
B. 1
C. 2
D. 5
What is the output?
printf("%d %d %d", 12 & 10, 12 | 10, 12 ^ 10);
A. 8 14 6
B. 14 8 6
C. 8 6 14
D. 6 14 8
What is the output?
int x = 0;
int y = 0 && ++x;
printf("%d %d", x, y);
A. 0 0
B. 1 0
C. 1 1
D. 0 1
What is the output?
int x = 7;
if (x % 2 == 0)
printf("Even");
else
printf("Odd");
A. Even
B. Odd
C. 7
D. No output
What is the output?
int marks = 80;
if (marks >= 90)
printf("A");
else if (marks >= 75)
printf("B");
else if (marks >= 60)
printf("C");
else
printf("D");
A. A
B. B
C. C
D. D
What is the output?
int x = 2;
switch (x)
{
case 1:
printf("A");
case 2:
printf("B");
case 3:
printf("C");
break;
default:
printf("D");
}
A. A
B. B
C. BC
D. BCD
What is the output?
int i;
for (i = 1; i <= 5; i++)
printf("%d ", i);
A. 0 1 2 3 4
B. 1 2 3 4 5
C. 1 2 3 4
D. 5 4 3 2 1
What is the output?
int i = 5;
while (i >= 1)
{
printf("%d ", i);
i -= 2;
}
A. 5 4 3 2 1
B. 5 3 1
C. 5 2
D. 1 3 5
What is the output?
int i = 10;
do
{
printf("%d", i);
}
while (i < 5);
A. No output
B. 5
C. 10
D. Infinite loop
What is the output?
int i;
for (i = 1; i <= 5; i++)
{
if (i == 3)
continue;
printf("%d ", i);
}
A. 1 2 3 4 5
B. 1 2
C. 1 2 4 5
D. 3 4 5
What is the output?
int i;
for (i = 1; i <= 5; i++)
{
if (i == 3)
break;
printf("%d ", i);
}
A. 1 2
B. 1 2 3
C. 1 2 4 5
D. 3 4 5
What is the output?
int i;
int sum = 0;
for (i = 1; i <= 4; i++)
sum += i;
printf("%d", sum);
A. 6
B. 8
C. 10
D. 15
What is the output?
int i, j;
int count = 0;
for (i = 1; i <= 3; i++)
{
for (j = 1; j <= 2; j++)
{
count++;
}
}
printf("%d", count);
A. 3
B. 5
C. 6
D. 9
| Q. | Answer | Q. | Answer | Q. | Answer |
|---|---|---|---|---|---|
| 1 | B | 11 | A | 21 | B |
| 2 | C | 12 | C | 22 | B |
| 3 | C | 13 | A | 23 | C |
| 4 | C | 14 | B | 24 | B |
| 5 | B | 15 | A | 25 | B |
| 6 | C | 16 | C | 26 | C |
| 7 | C | 17 | D | 27 | C |
| 8 | C | 18 | A | 28 | A |
| 9 | B | 19 | A | 29 | C |
| 10 | C | 20 | A | 30 | C |
Dennis Ritchie
total_marks
sizeof(char) = 1
%zu
Equality operator = ==
Bitwise XOR = ^
* before +
do-while: minimum one execution
break
break stops fall-through
2 + 3 * 4
= 2 + 12
= 14
(2 + 3) * 4
= 5 * 4
= 20
7 / 2
= 3
7.0 / 2
= 3.5
17 % 5
= 2
x = 5
y = x++ → y = 5
x = 6
Output: 6 5
x = 5
y = ++x
x = 6
y = 6
Output: 6 6
5 > 3 = 1
2 < 1 = 0
1 && 0 = 0
12 = 1100
10 = 1010
1100 & 1010 = 1000 = 8
1100 | 1010 = 1110 = 14
1100 ^ 1010 = 0110 = 6
Output: 8 14 6
0 && ++x
First operand = 0
++x is not executed
x = 0
y = 0
Output: 0 0
7 % 2 = 1
Output: Odd
80 >= 90 → 0
80 >= 75 → 1
Output: B
x = 2
Start at case 2
case 2 → B
No break
case 3 → C
break
Output: BC
| Iteration | i | Output |
|---|---|---|
| 1 | 1 | 1 |
| 2 | 2 | 2 |
| 3 | 3 | 3 |
| 4 | 4 | 4 |
| 5 | 5 | 5 |
Output: 1 2 3 4 5
| Iteration | i | Output | New i |
|---|---|---|---|
| 1 | 5 | 5 | 3 |
| 2 | 3 | 3 | 1 |
| 3 | 1 | 1 | −1 |
Output: 5 3 1
Body executes before condition.
Output: 10
| i | Action |
|---|---|
| 1 | Print 1 |
| 2 | Print 2 |
| 3 | continue |
| 4 | Print 4 |
| 5 | Print 5 |
Output: 1 2 4 5
| i | Action |
|---|---|
| 1 | Print 1 |
| 2 | Print 2 |
| 3 | break |
Output: 1 2
| i | sum before | sum after |
|---|---|---|
| 1 | 0 | 1 |
| 2 | 1 | 3 |
| 3 | 3 | 6 |
| 4 | 6 | 10 |
Output: 10
Outer iterations = 3
Inner iterations per outer loop = 2
count = 3 × 2
= 6