Programming in C
Module 2
IMP QNs Solved
Shiva Priya EduWorld
MODULE–2 : PROGRAMMING IN C
1. Explain Console Input and Output in C. Describe character input and output functions
with examples.
2. Explain reading and writing strings in C. Describe string input and output functions.
3. Explain Formatted Console Input and Output. Describe printf() and scanf() with syntax
and examples.
4. What are Statements in C? Explain the classification of statements.
5. Explain Selection Statements in C. Describe if, if–else, and else–if ladder with examples.
6. Explain the switch statement in C. Write syntax, working, and examples.
7. Explain Iteration Statements in C. Describe while, do–while, and for loops with
examples.
8. Compare while loop and do–while loop with examples.
9. Explain Jump Statements in C. Describe break, continue, and goto with examples.
10. Explain Expression Statements and Block Statements in C with examples.
11. Explain True and False conditions in C. How are logical expressions evaluated?
12. Write short notes on common input/output format specifiers and escape sequences
used in C.
Shiva Priya EduWorld
1. Explain Console Input and Output in C. Describe character input and output functions with
examples.
• Console Input means taking data from the user through the keyboard.
Console Output means displaying data to the user on the screen.
C provides standard input and output functions using the console for interaction between user and
program.
• Character Input Functions:
These functions are used to read a single character from the keyboard.
• getchar():
It reads one character from standard input.
It waits for the user to press a key and Enter.
Example: If user enters A, getchar() reads A.
• getch():
It reads a single character without displaying it on the screen.
It does not wait for Enter key.
Mostly used for password-like input.
• getche():
It reads a single character and displays it on the screen.
It does not wait for Enter key.
• Character Output Functions:
These functions are used to display a single character on the screen.
Shiva Priya EduWorld
• putchar():
It prints one character to the console.
Example: putchar('A') displays A on the screen.
• putch():
It prints a single character on the screen.
Used with getch() and getche() functions.
• Summary:
Console input/output helps user interaction.
Character I/O functions deal with single characters.
They are simple, fast, and commonly used in basic C programs.
Shiva Priya EduWorld
2. Explain reading and writing strings in C. Describe string input and output functions.
• A string in C is a collection of characters ending with a null character ‘\0’.
Reading a string means taking a group of characters as input.
Writing a string means displaying the string on the screen.
• String Input Functions:
• scanf():
Used to read a string using format specifier %s.
It reads input until a space, tab, or newline is found.
Suitable for single-word strings.
• gets():
Used to read an entire line including spaces.
Input ends when Enter key is pressed.
It is not safe because it does not check memory size.
• fgets():
Used to read a line of text safely.
It reads a limited number of characters including spaces.
Recommended for safe string input.
• Summary:
Strings store multiple characters.
Different functions are used based on need and safety.
fgets() and puts() are commonly preferred for safe string handling.
Shiva Priya EduWorld
3. Explain Formatted Console Input and Output. Describe printf() and scanf() with syntax and
examples.
• Formatted input and output allow data to be read and displayed in a specific format.
Format specifiers are used to define the type of data such as integer, float, or character.
This makes input and output more readable and meaningful.
• printf() function:
printf() is used to display formatted output on the screen.
It prints text along with variable values using format specifiers.
• Syntax:
printf("format string", variable list);
• Example:
printf("Age = %d", age);
• Common format specifiers:
%d for integer
%f for float
%c for character
%s for string
• scanf() function:
scanf() is used to read formatted input from the keyboard.
It assigns the entered values to variables using format specifiers.
Address operator (&) is used to store input values.
• Summary:
Formatted I/O improves clarity of input and output.
printf() is used for output and scanf() is used for input.
Format specifiers connect variables with input and output.
Shiva Priya EduWorld
4. What are Statements in C? Explain the classification of statements.
• A statement in C is an instruction given to the computer to perform a specific task.
Each statement in C usually ends with a semicolon (;).
Statements control the flow of execution of a C program.
• Classification of Statements in C:
1. Expression Statements:
These statements perform operations like calculations or assignments.
Examples include arithmetic operations and assignments.
2. Selection Statements:
These statements are used to make decisions based on conditions.
They choose one block of code from multiple options.
3. Iteration Statements:
These statements are used to repeat a block of code multiple times.
They are also called looping statements.
4. Jump Statements:
These statements are used to change the normal flow of execution.
They transfer control from one part of the program to another.
5. Block Statements:
A block statement is a group of statements enclosed within braces { }.
They are treated as a single unit.
• Summary:
Statements form the backbone of a C program.
They control decision making, looping, and flow of execution.
Proper use of statements makes programs clear and structured.
Shiva Priya EduWorld
5. Explain Selection Statements in C. Describe if, if–else, and else–if ladder with examples.
7. Explain Iteration Statements in C. Describe while, do–while, and for loops with examples
Shiva Priya EduWorld
6. Explain the switch statement in C. Write syntax, working, and examples.
• The switch statement is a selection control statement.
It is used to execute one block of code from multiple options.
The choice is made based on the value of an expression.
• Syntax of switch statement:
switch(expression)
{
case constant1:
statements;
break;
case constant2:
statements;
break;
default:
statements;
}
• Working of switch statement:
First, the expression inside switch is evaluated.
The value of the expression is compared with each case constant.
When a matching case is found, the corresponding statements are executed.
The break statement stops execution and exits the switch block.
If no case matches, the default block is executed.
If break is not used, control flows to the next case (fall-through).
Shiva Priya EduWorld
8. Compare while loop and do–while loop with examples.
while loop is condition-first.
do–while loop is execution-first.
Shiva Priya EduWorld
9. Explain Jump Statements in C. Describe break, continue, and goto with examples.
• Jump statements are used to transfer control from one part of the program to another.
They change the normal sequence of execution.
They are mainly used inside loops and switch statements.
• break statement:
The break statement is used to immediately terminate a loop or switch statement.
Control moves to the statement following the loop or switch.
• Example:
If break is used inside a loop when i becomes 5, the loop stops completely.
• continue statement:
The continue statement skips the remaining statements of the current iteration.
Control moves to the next iteration of the loop.
• Example:
If continue is used when i is 3, the statements after continue are skipped for i = 3.
• goto statement:
The goto statement transfers control to a labeled statement in the program.
It is generally avoided because it makes programs difficult to understand.
• Example:
If goto is used to jump to a label named start, execution continues from start.
• Summary:
break exits the loop or switch.
continue skips current iteration.
goto jumps to a labeled statement.
Shiva Priya EduWorld
10. Explain Expression Statements and Block Statements in C with examples.
• An expression statement is a statement that performs a computation or operation.
It usually consists of expressions like assignments, arithmetic operations, or function calls.
Expression statements end with a semicolon.
• Examples:
Assigning a value to a variable.
Adding two numbers and storing the result.
Calling a function.
• Block Statements in C:
A block statement is a group of statements enclosed within curly braces { }.
All statements inside a block are executed together.
A block is treated as a single statement.
• Examples:
Statements inside if, else, loops, and functions form a block.
Multiple statements executed when a condition is true.
• Summary:
Expression statements perform actions.
Block statements group multiple statements.
Blocks help in organizing code clearly.
Shiva Priya EduWorld
11. Explain True and False conditions in C. How are logical expressions evaluated?
• In C, any non-zero value is considered TRUE.
Zero (0) is considered FALSE.
Conditions are used in decision making and looping statements.
• Relational operators:
Relational operators compare two values.
Examples include greater than, less than, equal to, and not equal to.
They return either TRUE or FALSE.
• Logical operators:
Logical operators are used to combine conditions.
AND (&&) returns true if all conditions are true.
OR (||) returns true if at least one condition is true.
NOT (!) reverses the condition result.
• Evaluation of logical expressions:
Logical expressions are evaluated from left to right.
Operator precedence is followed during evaluation.
Short-circuit evaluation is used:
In AND (&&), if the first condition is false, the second is not checked.
In OR (||), if the first condition is true, the second is not checked.
• Examples:
If 5 > 3, the condition is true.
If 0, the condition is false.
If (a > 0 && b > 0), both must be true.
• Summary:
True means non-zero value.
False means zero.
Logical expressions control program flow. Shiva Priya EduWorld
12. Write short notes on common input/output format specifiers and escape sequences used in C.
• Input/Output Format Specifiers in C:
Format specifiers are used in printf() and scanf() to define the type of data being read or displayed.
They tell the compiler how to interpret the data.
• Common format specifiers:
%d – used for integer values
%f – used for floating point values
%c – used for single characters
%s – used for strings
%lf – used for double values
%u – used for unsigned integers
• Escape Sequences in C:
Escape sequences are special characters used inside output statements.
They begin with a backslash ().
They control cursor movement or formatting of output.
• Common escape sequences:
\n – moves cursor to new line
\t – gives horizontal tab space
\b – backspace
\r – moves cursor to beginning of line
\" – prints double quote
Summary: Format specifiers control data type display and input. Escape sequences improve output formatting.
Both are widely used in formatted console I/O.
Shiva Priya EduWorld