DCA1102 - Programming in C
DCA1102 - Programming in C
SET - I
Question 1.
Answer:
The C programming language is a feature-rich programming language which has all the
required features that a developer would want a programming language to have.
a. Simple and efficient: It is a simple language that is easy to learn even for a beginner
and is super-efficient to use both in terms of the time of development and time of
execution. If anyone wants to create a software using C language, they can do so in
quite less time depending upon the size of the software that they want to code. The
learning curve for C language is less which can be learnt by new learners quite easily
compared to other programming languages.
b. Portability: C Language program runs the same way everywhere. A program that has
been written in a simple C program like a program to find sum of N numbers in C, on
a Windows operating system and then compiled and run, then the compiled code can
be run on any other operating system or machine, like, Linux or macOS, etc., the C
program will always return the same result. Hence, we can say that the C
programming language is very portable.
d. Powerful: The C programming language has broad range of support for features like
data types, operators, keywords as well as complex data structures like arrays and
pointers which helps in creating functions, loops and decision-making statements,
making C a resourceful programming language.
Question 2.
Answers:
Flow control refers to the execution order of statements or instructions in a program which
allows the users to control the flow and sequence of code execution based on certain
conditions or criteria. Flow control statements are used to make decisions, repeat blocks of
code, and jump to specific sections of code.
There are several types of flow control statements commonly used in C programming:
Question 3.
Answers:
Function: A function is a block of code which only runs when it is called. Functions are used
to perform certain actions, and they are important for reusing code. For example, main() is a
function, which is used to execute code.
There are four types of user-defined functions divided based on arguments they accept and
the value they return:
• Function with no arguments and a return value: This type of functions has
no arguments but have some return values. Such functions are used to perform
specific operations and return their value. Syntax:
return_type function_name()
{
// program
return value;
}
• Function with arguments and no return value: This type of functions has
arguments but no return values. Such functions are used to display or perform
some operations on given arguments. Syntax:
void function_name(type1 argument1, type2 argument2,...typeN argumentN)
{
// Program
return;
}
• Function with arguments and with return value: This type of functions has
arguments and some return value. These functions are used to perform specific
operations on the given arguments and return their values to the user. Syntax:
return_type function_name(type1 argument1, type2 argument2,...typeN
argumentN)
{
// program
return value;
}
SET - II
Question 4.
Answer:
Array: an array is a data structure that stores a collection of similar data items in contiguous
memory locations. It is used to store the collection of primitive data types such as int, char,
float, etc., and also derived and user-defined data types such as pointers, structures, etc.
Syntax of an standard array is written as:
int my_array[10];
Arrays can also be multidimensional, meaning that they can store arrays of arrays. For
example, the following declaration creates a two-dimensional array of integers:
int my_2d_array[10][10];
Onc Dimensional array: In a one-dimensional array the data is stored in a single row in
contiguous memory locations. At first an array needs to be declared to be used by the user for
storing data.
Declaration of array is done in similar to how we declare any variable, while declaring an
array we can use any type as required in the code as well as any name can be used. Syntax:
int arr[5]; //where arr is the array name of type integer, and 5 is the size of the array and int is
the data type.
Initialization of a single dimensional array: In static uninitialized arrays, all the elements
initially contain garbage values, which can be explicitly initialize them at their declaration.
One-Dimensional arrays in C are initialized either at Compile Time or Run Time.
#include <stdio.h>
int main(){
int nums[3]={0,1,2};
printf(" Compile-Time Initialization Example:\n");
printf(" %d ",nums[0]);
printf("%d ",nums[1]);
printf("%d ",nums[2]);
}
int main() {
int nums[5];
printf("\n Run-Time Initialization Example:\n");
printf("\n Enter array elements: ");
return 0;
}
Question 5.
Answers:
A. Structures: The structure in C is a user-defined data type that can be used to group
items of possibly different types into a single type. The struct keyword is used to
define the structure in the C programming language. The items in the structure are
called its member and they can be of any valid data type.
struct structure_name {
data_type member_name1;
data_type member_name1;
....
....
};
To access a structure in C, we use the dot operator (.). For example, if we have a
structure called student with a member variable called name, we would access it like
this:
student.name
We can also use the arrow operator (->) to access structure members through a pointer
to a structure. For example, if we have a pointer to a structure called student_ptr, we
would access the name member variable like this:
student_ptr->name
Question 6.
Answers:
Memory allocation is a process by which computer programs and services are assigned with
physical or virtual memory space. The memory allocation is done either before or at the time
of program execution. There are two types of memory allocations:
In the static memory allocation, variables get In the Dynamic memory allocation, variables
allocated permanently, till the program get allocated only if your program unit gets
executes or function call finishes. active.
Static Memory Allocation is done before Dynamic Memory Allocation is done during
program execution. program execution.
It uses stack for managing the static It uses heap for managing the dynamic
allocation of memory allocation of memory
Example: This static memory allocation is Example: This dynamic memory allocation is
generally used for array. generally used for linked list.
1. malloc()
The malloc() function allocates a specified number of bytes from the heap memory
and returns a pointer to the first byte of the allocated memory block. It takes the size
in bytes as an argument. Syntax:
ptr = (cast-type*) malloc(byte-size)
2. calloc()
The calloc() function allocates memory for an array of elements, initializing all bytes
to zero. It takes the number of elements and the size of each element as arguments.
Syntax:
ptr = (cast-type*)calloc(n, element-size);
3. realloc()
The realloc() function changes the size of the memory block pointed to by ptr to size
bytes. The contents of the memory block are preserved up to the lesser of the new and
old sizes. Syntax:
ptr = realloc(ptr, newSize);
4. free()
The free() function deallocates the memory previously allocated by malloc(), calloc(),
or realloc(). It takes a pointer to the memory block to be deallocated as an argument.
Syntax:
free(ptr);