DCA1107 (U)
DCA1107 (U)
Que.-1:- Explain the role of format specifiers in the printf function in C. Provide
examples of different format specifiers and their corresponding data types.
Ans.:- Format specifiers are essential in the printf function of the C programming language, as
they allow programmers to specify the type of data that will be printed to the output. Format
specifiers function as placeholders within a string, enabling the function to ascertain the
appropriate interpretation and formatting of the corresponding parameters for display. Each
format specifier is associated with a specific data type, guaranteeing that the data is output in the
desired format.
A % symbol is typically used to begin the syntax for format specifiers, which is followed by a
character or combination of characters that denote the data type. For instance, %d is employed to
print integers, whereas %f is employed to print floating-point numbers. Printf is a versatile and
potent function due to the flexibility it offers in formatting output, which is facilitated by these
specifiers.
Data Types of Common Format Specifiers:
%d and %i: Both %d and %i are used for signed integers. For e.g.:
%u: This is employed for unsigned integers. It does not exhibit a negative sign for negative
numbers, in contrast to %d.
unsigned int num = 100;
printf("Unsigned number: %u\n", num); // Output: Unsigned number: 100
%f: Used for floating-point numbers. By default, it displays decimal values with six digits
following the decimal point; however, this can be modified.
float pi = 3.14159;
printf("Value of pi: %f\n", pi); // Output: Value of pi: 3.141590
%c: Represents a single character. Character variables or constants are frequently printed using
this method.
char ch = 'A';
printf("Character: %c\n", ch); // Output: Character: A
%s: Used for strings, i.e., arrays of characters terminated by a null character (\0).
char name[] = "Alice";
printf("Name: %s\n", name); // Output: Name: Alice
1.
%x and %X: These are employed to print unsigned integers in hexadecimal format, with %x
generating lowercase letters and %X generating uppercase letters.
Que.-2:- In C programming, what are the decision control statements, and can you give an
example of each?
Ans.:- In the context of C programming, decision control statements are indispensable for
directing the execution process in accordance with predetermined conditions. These statements
enable a program to execute distinct segments of code and make decisions based on the outcome
of a condition, whether it is true or false. Decision control statements improve the functionality
and adaptability of a program by facilitating conditional logic. In C, the three primary categories
of decision control statements are if, if-else, and switch, each of which serves a unique purpose
in the decision-making process.
The simplest form of decision control is the if statement. It evaluates a condition, and if the
condition is satisfied, the block of code contained within the if statement is executed. The block
is skipped by the program if the condition is false. For instance,
int number = 5;
if (number > 0) {
printf("The number is positive.\n");
}
The program determines whether the value is greater than zero in this instance. The message is
printed if the condition is true.
Two alternative paths are provided by the if-else statement. One block of code is executed if the
condition is true; if it is false, another block is executed. For example,
In this instance, the else block is executed, resulting in the printing of the alternative message, as
the condition (number > 0) is false.
The if-else-if cascade is employed in circumstances that necessitate multiple conditions. It
enables the sequential assessment of multiple conditions. The initial block of the true condition is
executed, and the subsequent blocks are omitted. For instance,
int number = 0;
if (number > 0) {
printf("The number is positive.\n");
} else if (number < 0) {
printf("The number is negative.\n");
} else {
printf("The number is zero.\n");
}
This is where the number is evaluated against a variety of conditions to ascertain whether it is
positive, negative, or zero.
The switch statement is employed to compare multiple values for a single variable. It evaluates
an expression and executes the corresponding case. For instance,
A message is printed in this example in accordance with the grade value. The execution of
subsequent cases is prevented by the break statement.
In C programming, decision control statements such as if, if-else, and switch are essential tools
that allow developers to create programs that are responsive, efficient, and logical. Differentiated
advantages are provided by each statement, which are appropriate for distinct decision-making
scenarios.
Que.-3:- Explain the concept of arrays in C programming. How are arrays declared and
initialized? Discuss with examples.
Ans.:- An array in C programming is a collection of elements of the same data type that are
stored in contiguous memory locations. Arrays enable developers to efficiently manage and
manipulate large quantities of data by utilizing a single variable name, rather than designating
multiple individual variables. An index is used to access each element in an array, with the first
element being indexed at 0. When dealing with collections of data, such as marks of students, a
sequence of numbers, or characters in a string, arrays are particularly beneficial.
Declaring Arrays:
In order to designate an array in C, the data type, array name, and array size (number of
elements) are encapsulated in square brackets []. For instance,
In this context, the array "numbers" is capable of accommodating five integer values. The array's
capacity must be a constant expression, as it determines the amount of memory it will consume.
Initializing Arrays: Arrays may be initialized at the time of declaration or designated values at a
later time. Specific values are assigned to array elements during initialization. For instance,
int numbers[5] = {10, 20, 30, 40, 50}; // Initializes an array with values.
The array numbers consist of five elements: 10, 20, 30, 40, and 50. The remaining elements are
automatically initialized to zero if fewer values are provided during initialization:
int numbers[5] = {10, 20}; // Remaining elements will be 0.
The number of elements is used to infer the array size if the size is omitted during initialization:
int numbers[] = {10, 20, 30}; // Size is 3.
Array Elements Accessibility: The index of an array element is utilized in square brackets to
access it. For instance,
Example of Array Utilization: The following program computes the sum of the elements in an
integer array:
#include <stdio.h>
int main() {
int numbers[5] = {1, 2, 3, 4, 5};
int sum = 0;
for (int i = 0; i < 5; i++) {
sum += numbers[i]; // Add each element to the sum.
}
printf("Sum of array elements: %d\n", sum);
return 0;
}
The for loop in this example calculates the sum of the array elements by iterating through them.
In conclusion, arrays in C offer a streamlined approach to organizing collections of related data.
They are declared with a data type and capacity, and they can be initialized with specific values.
Arrays are a fundamental concept in C programming because they enable the access or
modification of individual elements through the use of indices.
SET-2
Que.-4:- Explain null-terminated strings in C programming. Discuss how they are different
from a regular character array. Provide an example to illustrate your answer.
Example to Illustrate:
Here is an example to demonstrate the difference:
#include <stdio.h>
int main() {
// Null-terminated string
char str1[] = "Hello";
return 0;
}
Que.-5:- 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.
In the absence of a base case, or if the recursive call fails to reach the base case, the function will
result in indefinite recursion, potentially leading to a stack overflow issue or computer crash.
Illustration of a Recursive Function: Computation of Factorials
A quintessential illustration of recursion is the computation of a number's factorial. The factorial
of a non-negative number n is the product of all integers from 1 to n, represented as n!. The
factorial is defined recursively as follows:
● Base Case: 0! = 1 (or 1! = 1)
● Recursive Case: n! = n * (n-1)!
int main() {
int number = 5;
printf("Factorial of %d is: %d\n", number, factorial(number));
return 0;
}
Que.-6:- Discuss the selection sort algorithm with an example. Write a C program to
implement selection sort.
Time Complexity:
● Best, Average, and Worst Case: O(n²)
Step 1: discover the smallest element in the array (11) and swap it with the 1st element:
[11, 25, 12, 22, 64]
Step 2: Identify the smallest element in the unsorted section (12) and exchange it with the second
element.
Step 4: Identify the smallest element in the unsorted segment (25) and exchange it with the
fourth element.
Step 5: The array is now sorted as [11, 12, 22, 25, 64].
C Program for Selection Sort:
#include <stdio.h>
int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);
return 0;
}