CS Q&A -2
CS Q&A -2
In C programming, tokens are the smallest meaningful units in a program. They are the
building blocks of a C program and are used to construct statements and expressions.
Types of Tokens in C:
1. Keywords
2. Identifiers
3. Constants
4. Strings
5. Operators
6. Special Symbols
1. Keywords – Reserved words that have a special meaning in C (e.g., int, return,
if, while).
2. Identifiers – Names given to variables, functions, arrays, etc. (e.g., sum, main,
num1).
3. Operators – Symbols used for operations (e.g., +, -, *, /).
4. Constants – Fixed values that do not change during program execution (e.g., 3.14,
100, 'A').
In C programming, %d and %f are format specifiers used in functions like printf() and
scanf() to specify the type of data being printed or read.
int main() {
return 0;
Output:
The number is: 25
●
●
int main() {
float pi = 3.14159;
return 0;
Output:
Value of pi: 3.141590
●
●
Summary
Arithmetic Operators in C
Example Program
#include <stdio.h>
int main() {
int a = 10, b = 3;
return 0;
}
Output:
Addition: 13
Subtraction: 7
Multiplication: 30
Division: 3
Modulus: 1
1. Formatted Input/Output
Formatted I/O functions allow the programmer to specify format specifiers to control the
format of input and output.
int main() {
int age;
float height;
printf("You are %d years old and %.2f meters tall.\n", age, height);
return 0;
}
Key Features of Formatted I/O: ✔ Uses format specifiers (%d, %f, etc.)
✔ Controls the way data is displayed or read
✔ More precise but requires careful input formatting
2. Unformatted Input/Output
Unformatted I/O functions handle data without any specific format. They work with
characters and strings without using format specifiers.
int main() {
char name[50];
printf("Hello, ");
puts(name); // Prints the string with a newline
return 0;
}
Comparison Table
Feature Formatted I/O Unformatted I/O
Data Types Works with all data types Works mostly with
(int, float, char, etc.) characters and strings
When to Use?
● Use Formatted I/O (printf(), scanf()) when dealing with numbers, precise
formatting, or structured input.
● Use Unformatted I/O (getchar(), puts()) when handling single characters or
simple string outputs.
Type Conversion in C
Type conversion in C refers to changing one data type into another to ensure proper
operations between different types of variables.
int main() {
int num = 5;
float result = num + 2.5; // int is converted to float automatically
Explanation:
int main() {
double pi = 3.14159;
int truncated_pi = (int) pi; // Type casting double to int
Explanation:
● (int) pi converts the double value 3.14159 into an int, removing the decimal
part.
The break and continue statements are used to control the flow of loops (for, while,
do-while) and switch cases in C.
1. break; Statement
● The break statement terminates the loop or switch statement immediately.
● When break is encountered inside a loop, the program exits the loop and moves to
the next statement after the loop.
#include <stdio.h>
int main() {
if (i == 5) {
return 0;
Output:
1234
Explanation:
● The loop starts from 1 and runs up to 10, but when i == 5, the break statement
stops the loop immediately.
#include <stdio.h>
int main() {
int num = 2;
switch (num) {
case 1:
printf("One\n");
break;
case 2:
printf("Two\n");
case 3:
printf("Three\n");
break;
default:
printf("Invalid\n");
return 0;
Output:
Two
Explanation:
● Without break, the program would continue executing the next cases.
2. continue; Statement
● The continue statement skips the rest of the current iteration and moves to the
next iteration of the loop.
● Unlike break, continue does not terminate the loop—it just skips some code.
#include <stdio.h>
int main() {
if (i == 3) {
return 0;
Output:
1245
Explanation:
● When i == 3, continue skips the printf() statement and moves to the next
iteration.
1. Simple if Statement
● The if statement checks a condition. If the condition is true, it executes the block
inside {}.
● If the condition is false, it skips the block.
Syntax:
if (condition) {
Example:
#include <stdio.h>
int main() {
int num = 10;
if (num > 0) {
printf("Number is positive.\n");
return 0;
Output:
Number is positive.
2. if-else Statement
● Adds an else block to execute when the if condition is false.
Syntax:
if (condition) {
} else {
Example:
#include <stdio.h>
int main() {
if (num > 0) {
printf("Number is positive.\n");
} else {
printf("Number is negative.\n");
return 0;
Output:
Number is negative.
Syntax:
if (condition1) {
} else if (condition2) {
Example:
#include <stdio.h>
int main() {
int num = 0;
if (num > 0) {
printf("Number is positive.\n");
printf("Number is negative.\n");
} else {
printf("Number is zero.\n");
return 0;
Output:
Number is zero.
Explanation: Since num > 0 and num < 0 are false, the else block executes.
4. Nested if Statement
● An if statement inside another if statement.
● Used when one condition depends on another condition.
Syntax:
if (condition1) {
if (condition2) {
Example:
#include <stdio.h>
int main() {
return 0;
Output:
Number is positive and divisible by 5.
Explanation:
Switch Statement in C
case value1:
break;
case value2:
break;
...
default:
● The expression is usually an int or char (floating-point values are not allowed).
● Each case specifies a possible value for expression and executes the
corresponding code.
● The break statement exits the switch after a case is executed (without break,
execution continues to the next case).
● The default case executes if no cases match (optional).
int main() {
int day = 3;
switch (day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
case 4:
printf("Thursday\n");
break;
case 5:
printf("Friday\n");
break;
default:
printf("Invalid day\n");
return 0;
Output:
Wednesday
Explanation:
● day = 3, so case 3 executes.
● The break statement prevents further execution.
#include <stdio.h>
int main() {
int num = 2;
switch (num) {
case 1:
printf("One\n");
case 2:
printf("Two\n");
case 3:
printf("Three\n");
default:
printf("Invalid\n");
return 0;
Output:
Two
Three
Invalid
Explanation:
int main() {
switch (grade) {
case 'A':
printf("Excellent\n");
break;
case 'B':
printf("Good\n");
break;
case 'C':
printf("Average\n");
break;
default:
printf("Fail\n");
}
return 0;
Output:
Good
Explanation:
#include <stdio.h>
int main() {
switch (num) {
case 1:
printf("One\n");
break;
case 2:
printf("Two\n");
break;
default:
printf("Not 1 or 2\n");
return 0;
Output:
Not 1 or 2
Explanation:
● Since num = 10, and no case matches, the default case executes.
Looping Statements in C
Looping statements in C are used to execute a block of code multiple times based on a
condition. This helps in reducing code repetition and improving efficiency.
Types of Loops in C
C provides three types of loops:
1. for Loop
The for loop is used when the number of iterations is known beforehand.
Syntax:
// Code to execute
}
#include <stdio.h>
int main() {
return 0;
Output:
12345
Explanation:
● i = 1 (initialization)
● i <= 5 (condition checked before each iteration)
● i++ (increment i after each loop)
2. while Loop
The while loop is used when the number of iterations is not known in advance. It
executes as long as the condition is true.
Syntax:
while (condition) {
// Code to execute
#include <stdio.h>
int main() {
int i = 1;
while (i <= 5) {
i++;
return 0;
Output:
12345
Explanation:
3. do-while Loop
The do-while loop is similar to while, but it always executes at least once, even if the
condition is false.
Syntax:
do {
// Code to execute
} while (condition);
#include <stdio.h>
int main() {
int i = 1;
do {
i++;
return 0;
Output:
12345
Explanation:
Use Case When iterations are When iterations are When at least one
known unknown execution is
required
Condition Check Before the loop Before each After each iteration
starts iteration
#include <stdio.h>
int main() {
return 0;
}
Press Ctrl + C to stop execution manually.
#include <stdio.h>
int main() {
if (i == 3) {
return 0;
Output:
12
#include <stdio.h>
int main() {
if (i == 3) {
return 0;
Output:
1245
Summary