SET – I DCA1107 and C Programming
Q1. Explain the role of format specifiers in the printf function in C. Provide examples of
different format specifiers and their corresponding data types.
The printf function in C is used to display output on the screen, and format specifiers play a crucial role in
controlling how data is printed. A format specifier is a special character sequence that begins with a percent
symbol (%) and indicates the type and format of the data to be displayed. It acts as a placeholder that tells the
compiler how to interpret and print the value of a variable.
Format specifiers are necessary because C is a statically typed language, and different data types occupy
different memory sizes and representations. When printf encounters a format specifier, it replaces it with the
corresponding value provided as an argument. If an incorrect format specifier is used, it may result in incorrect
output or undefined behavior.
One of the most commonly used format specifiers is %d, which is used to print integer values. For example, if
an integer variable a has a value of 10, using printf("%d", a); will display 10 on the screen. Similarly, %f is
used for floating-point numbers. When printing decimal values such as 3.14, %f ensures that the value is
displayed with a default precision of six decimal places.
Characters are printed using the %c format specifier. If a character variable stores a value like ‘A’, %c prints the
corresponding character. Strings, which are arrays of characters terminated by a null character, are printed using
%s. This specifier displays all characters of the string until the null terminator is encountered.
Other important format specifiers include %ld for long integers, %lf for double data types, and %u for unsigned
integers. The %x specifier is used to display numbers in hexadecimal format, while %o prints numbers in octal
format. For displaying memory addresses, %p is used.
In addition to data type representation, format specifiers also support width, precision, and alignment control.
For example, %.2f limits a floating-point number to two decimal places. Thus, format specifiers enhance output
readability and ensure correct representation of different data types in C programs.
Q2(a). Compare and contrast while and do-while loops in terms of execution flow and
behaviour.
The while loop and do-while loop are both entry and exit controlled loops used in C programming to execute
a block of code repeatedly based on a condition. However, they differ significantly in execution flow and
behavior.
The while loop is an entry-controlled loop, meaning the condition is checked before the execution of the loop
body. If the condition evaluates to false initially, the loop body will not execute even once. This makes the
while loop suitable when the number of iterations is unknown and the loop should only run if a condition is
satisfied from the beginning.
In contrast, the do-while loop is an exit-controlled loop. In this loop, the condition is checked after the
execution of the loop body. As a result, the loop body is executed at least once regardless of whether the
condition is true or false. This behavior is useful in situations where the loop must execute at least one time,
such as displaying a menu-driven program.
Another difference lies in syntax. The while loop starts with the condition, whereas the do-while loop begins
with the do keyword and ends with a while condition followed by a semicolon. Because of this, the do-while
loop is slightly less commonly used but remains important for specific programming scenarios.
Overall, the main distinction is that the while loop may execute zero times, whereas the do-while loop
executes at least once, making their usage dependent on program requirements.
Q2(b). Differentiate between the break statement and the continue statement.
The break and continue statements are control statements used within loops in C to alter the normal flow of
execution. Although both affect loop behavior, they serve different purposes.
The break statement is used to terminate the loop entirely. When a break statement is encountered, the control
immediately exits the loop and transfers to the statement following the loop. It is commonly used when a
specific condition is met and further loop execution is unnecessary, such as when searching for a particular
value.
On the other hand, the continue statement does not terminate the loop. Instead, it skips the remaining
statements of the current iteration and transfers control back to the loop condition for the next iteration. This is
useful when certain values or conditions should be ignored without stopping the loop entirely.
In summary, break exits the loop completely, while continue skips only the current iteration and allows the
loop to continue executing.
Q3. Explain the concept of arrays in C programming. How are arrays declared and
initialized? Discuss with examples.
An array in C is a collection of elements of the same data type stored in contiguous memory locations. Arrays
allow programmers to store and manipulate multiple values using a single variable name, which improves code
organization and efficiency. Each element in an array is accessed using an index, starting from zero.
Arrays are particularly useful when dealing with large amounts of data, such as storing marks of students,
temperatures of different days, or a list of numbers. Since the elements are stored in contiguous memory,
accessing any element is fast and efficient.
To declare an array in C, the data type is specified followed by the array name and the number of elements in
square brackets. For example, int a[5]; declares an integer array capable of storing five elements. This
reserves memory for five integers.
Initialization of arrays can be done at the time of declaration or later. When initialized during declaration,
values are enclosed within curly braces. For example, int a[5] = {1, 2, 3, 4, 5}; assigns values to all
elements of the array. Partial initialization is also allowed, where unspecified elements are automatically set to
zero.
Arrays can also be initialized without specifying size, as the compiler automatically determines it. For example,
int b[] = {10, 20, 30}; creates an array of size three.
Accessing array elements is done using indices. For example, a[0] refers to the first element, and a[4] refers
to the last element. Arrays can be used with loops to perform operations such as searching, sorting, and
summation.
Thus, arrays are fundamental data structures in C that enable efficient handling of multiple related data items
under a single name.
SET – II
Q4. Define a string. Explain how strings are declared and initialized in C. Describe various
string handling functions with examples.
A string in C is a sequence of characters stored in an array and terminated by a null character ( \0). Unlike some
high-level languages, C does not have a built-in string data type, so strings are handled using character arrays.
The null character marks the end of the string and helps functions identify where the string terminates.
Strings are declared as character arrays. For example, char name[20]; declares a string capable of storing up
to 19 characters plus a null character. Strings can be initialized during declaration by enclosing characters
within double quotes, such as char city[] = "Delhi";. In this case, the compiler automatically adds the null
character at the end.
Strings can also be initialized character by character, such as char str[6] = {'H','e','l','l','o','\
0'};. Both methods produce the same result, but string literals are simpler and more readable.
C provides several built-in string handling functions in the string.h library. The strlen() function is used to
find the length of a string excluding the null character. The strcpy() function copies one string into another,
while strcat() concatenates two strings. The strcmp() function compares two strings and returns a value
indicating whether they are equal or not.
For example, strcpy(dest, src); copies the contents of src into dest. Similarly, strcat(str1, str2);
appends str2 at the end of str1. These functions simplify string manipulation and reduce manual coding.
Thus, strings in C are managed using character arrays and standard library functions, making them flexible and
efficient for text processing.
Q5. Explain the concept of recursion in C programming. What are the necessary conditions
for a function to be recursive? Provide an example of a recursive function in C that calculates
the factorial of a number.
Recursion in C programming refers to a process where a function calls itself directly or indirectly to solve a
problem. It is particularly useful for problems that can be divided into smaller, similar subproblems, such as
factorial calculation, Fibonacci series, and tree traversal.
A recursive function must satisfy certain conditions to work correctly. First, it must have a base condition that
stops further recursive calls. Without a base condition, the function will call itself indefinitely, leading to stack
overflow. Second, the recursive call should move toward the base condition by reducing the problem size in
each call.
The execution of a recursive function involves two phases: the calling phase and the returning phase. During
the calling phase, function calls are stored in the call stack. Once the base condition is reached, the function
starts returning values back through the stack, completing execution.
A common example of recursion is calculating the factorial of a number. The factorial of a number n is defined
as n * factorial(n-1), with the base condition being factorial(0) = 1.
For example, when calculating factorial of 5, the function repeatedly calls itself with decreasing values until it
reaches zero. After reaching the base condition, the function returns values step by step, multiplying them to
produce the final result.
Although recursion makes code simpler and more readable for certain problems, it requires more memory due
to function call overhead. Therefore, it should be used carefully where it provides clear advantages over
iterative solutions.
Q6. Explain the different types of loops in C. Provide examples.
Loops in C are used to execute a block of statements repeatedly based on a condition. They reduce code
repetition and improve program efficiency. There are three main types of loops in C: for, while, and do-
while.
The for loop is generally used when the number of iterations is known in advance. It consists of initialization,
condition checking, and increment or decrement in a single line. This makes it compact and easy to understand.
It is commonly used in counting and array traversal.
The while loop is an entry-controlled loop where the condition is checked before executing the loop body. If
the condition is false initially, the loop body will not execute at all. It is useful when the number of iterations is
not known beforehand.
The do-while loop is an exit-controlled loop where the loop body executes at least once before the condition is
checked. This loop is useful in situations such as menu-driven programs where the loop must run at least once.
Each loop serves different programming needs, and choosing the appropriate loop depends on the logic and
requirements of the program. Together, these loops form the backbone of iterative processing in C
programming.