DCA6102 Harshitha 2314509928
DCA6102 Harshitha 2314509928
SET-I
1. Describe various features of the C programming Language.
Solution: Features of the C Programming Language:
Procedural Paradigm: C adopts a procedural programming paradigm, allowing developers to organize a
program through the use of procedures or functions.
Low-Level Access: C provides developers with low-level access to memory, making it well-suited for
system-level programming where direct manipulation of memory is necessary.
Efficient Execution: C programs are known for their efficient and fast execution, making them suitable
for performance-critical applications.
Diverse Set of Operators: C boasts a comprehensive set of operators, encompassing arithmetic,
relational, logical, bitwise, and other operations, providing flexibility in expressing computations.
Structured Programming Support: C supports structured programming principles, such as the use of
loops, conditionals, and functions, aiding in the creation of well-organized and readable code.
Platform Portability: C programs can be compiled and run on various platforms with minimal
modifications, enhancing their portability and reducing the need for extensive code adjustments.
Robust Standard Library: C comes with an extensive standard library that provides a wide range of
functions for common tasks, offering pre-built solutions and saving developers time and effort.
Static Typing: C is statically typed, meaning that variable types are determined at compile-time. This
enhances code reliability by catching type-related errors during the compilation phase.
#include <stdio.h>
int main()
{
int num = 10;
if (num > 0)
{
printf("Number is positive.\n");
}
Else
{
printf("Number is non-positive.\n");
}
return 0;
}
Switch Statement: The switch statement is used to select one of many code blocks to be executed. It
provides an efficient way to handle multiple cases based on the value of an expression.
#include <stdio.h>
int main() {
char grade = 'B';
switch (grade) {
case 'A':
printf("Excellent!\n");
break;
case 'B':
printf("Good!\n");
break;
case 'C':
printf("Satisfactory.\n");
break;
default:
printf("Invalid grade.\n");
}
return 0;
}
For Loop: The for loop is used to repeatedly execute a block of code for a specified number of
iterations.
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
printf("%d ", i);
}
return 0;
}
While Loop: The while loop is used to repeatedly execute a block of code as long as a specified
condition is true.
#include <stdio.h>
int main() {
int i = 1;
while (i <= 5) {
printf("%d ", i);
i++; }
return 0;
}
Do-while Loop: The do-while loop is similar to the while loop, but it ensures that the block of code is
executed at least once before checking the condition.
#include <stdio.h>
int main() {
int i = 1;
do {
printf("%d ", i);
i++;}
while (i <= 5);
return 0;}
3. Define a function. List and explain the categories of user-defined functions.
Solution: A function in C is a reusable block of code designed to perform a specific task. It can take input
parameters, execute operations, and optionally return a value.
// Function definition
int add(int a, int b)
{
return a + b;
} In this example, add is a function that takes two parameters (a and b) and
returns their sum.
int main()// Function call
{
int result = add(3, 4);
return 0;
}
Function with Return Value and parameters: This type of function returns a value and can take
parameters.
int add(int a, int b) {
return a + b;
}
int result = add(3, 4);
Recursive Function: A recursive function is a function that calls itself either directly or indirectly to
solve a problem.
int factorial(int n) {
if (n == 0 || n == 1)
return 1;
else
return n * factorial(n - 1);
}
int result = factorial(5);
These categories illustrate the versatility of user-defined functions in C programming, offering modularity and
code organization.
SET-II
4. Define an array. How to initialize a one-dimensional array? Explain with suitable examples.
Solution: An array in C is a grouping of elements of the same data type, identified by a shared name. Each
element within the array is accessed by its index or position. Arrays are effective for storing and managing sets
of data.
Initialization of a One-Dimensional Array: Initialization of a one-dimensional array in C can occur
either during or after declaration. Here are examples for both scenarios:
a. Initialization during Declaration: Declaration and initialization of an integer array
int numbers[5] = {1, 2, 3, 4, 5};
In this instance;
int: The data type of the array elements.
numbers: The array's name.
[5]: The array's size, denoting the number of elements.
{1, 2, 3, 4, 5}: Values used for initialization.
b. Initialization after Declaration:
int numbers[5];
numbers[0] = 10; In this example: int numbers[5];: The declaration of an integer array
numbers[1] = 20; with five elements. Subsequent lines: The initialization of array
numbers[2] = 30; elements with specific values.
numbers[3] = 40;
numbers[4] = 50;
Complete example: This illustrates the process of defining and initializing a one-dimensional array in C.
#include <stdio.h>
int main() { // Declaration and initialization of an integer array
int numbers[5] = {1, 2, 3, 4, 5};
printf("Array Elements: "); // Accessing and printing array elements
for (int i = 0; i < 5; i++) {
printf("%d ", numbers[i]);
}
return 0;
}
This program declares and initializes an integer array named numbers with values 1 to 5. It then prints these
values using a for loop. The output will be:
Array Elements: 1 2 3 4 5
5. A) Define Structure and write the general syntax for declaring and accessing members.
Solution: In C, a structure is a user-defined data type that allows you to group together variables of different
data types under a single name. Each variable within the structure is referred to as a member.
In this example, a structure named Point is defined with two members (x and y). An instance of the structure
(p1) is declared, values are assigned to its members, and then the values are accessed and printed. The output
will be:
Coordinates: (10, 20)
Example
Structure:
struct Point {
int x;
int y;
};
Union:
union Status {
int errorCode;
char errorMessage[50];
};
6. Explain the difference between static memory allocation and dynamic memory allocation in C. Explain
various dynamic memory allocation functions in c.
Solution: The difference between static memory allocation and dynamic memory allocation in C
Static Memory Allocation: Static memory allocation refers to the process of allocating memory for
variables during the compile-time or before the program execution begins. The size of memory is
determined at the time of writing the program and remains fixed throughout its execution.
Characteristics:
Size Determined at Compile-Time: The size of the allocated memory is known and fixed during the
compilation phase.
Memory Allocated on the Stack or Data Segment: Variables are typically allocated on the stack or in the
data segment of the program.
Lifetime of Variables: Variables have a fixed lifetime during the program's execution.
No Explicit Release of Memory: Memory is automatically released when the variable goes out of scope.
int main() {
int x = 10; // Static allocation of memory for variable x
return 0;
}
Dynamic Memory Allocation:Dynamic memory allocation refers to the process of allocating memory
during the runtime or while the program is executing. It allows for the creation of variables whose size
can be determined and modified at runtime.
Characteristics:
Size Determined at Runtime: The size of the allocated memory can be determined during program
execution.
Memory Allocated on the Heap: Variables are typically allocated on the heap, a region of memory
separate from the stack.
Lifetime Determined by Programmer: The programmer is responsible for managing the memory and
explicitly releasing it when it's no longer needed.
Allows for Dynamic Data Structures: Enables the creation of dynamic data structures such as linked
lists, trees, etc.
Dynamic Memory Allocation Functions in C:
malloc (Memory Allocation):
Syntax: void* malloc(size_t size);
Description: Allocates a block of memory of a specified size and returns a pointer to the beginning of
the block.
calloc (Contiguous Allocation):
Syntax: void* calloc(size_t num_elements, size_t element_size);
Description: Allocates a block of memory for an array of elements, initializes the memory to zero, and
returns a pointer to the beginning of the block.
realloc (Reallocate Memory):
Syntax: void* realloc(void* ptr, size_t new_size);
Description: Changes the size of the previously allocated block of memory pointed to by ptr to the new
size.
free (Free Memory):
Syntax: void free(void* ptr);
Description: Deallocates the memory block pointed to by ptr, making it available for further allocations.
Example:
#include <stdio.h>
#include <stdlib.h>
int main() {
int* dynamicArray;
dynamicArray = (int*)malloc(5 * sizeof(int)); // Allocate memory for an array of 5 integers
if (dynamicArray != NULL) {
dynamicArray[0] = 1; // Use the allocated memory
dynamicArray[1] = 2;
free(dynamicArray); // Free the allocated memory when done
}
return 0;
}
In this example, malloc is used to allocate memory for an array of integers, and free is used to release
the allocated memory when it's no longer needed. Dynamic memory allocation is particularly useful
when the size of data structures is not known at compile-time or needs to change during runtime.
SubmissionI
D
455828