0% found this document useful (0 votes)
10 views

DCA1102 - Programming in C

The document provides an overview of the C programming language, highlighting its features such as simplicity, portability, structured programming, and a rich standard library. It also discusses flow control statements, user-defined functions, arrays, structures, and memory allocation in C, detailing their syntax and usage. Additionally, it compares static and dynamic memory allocation, explaining the functions used for dynamic memory management.

Uploaded by

Aditya Ray
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

DCA1102 - Programming in C

The document provides an overview of the C programming language, highlighting its features such as simplicity, portability, structured programming, and a rich standard library. It also discusses flow control statements, user-defined functions, arrays, structures, and memory allocation in C, detailing their syntax and usage. Additionally, it compares static and dynamic memory allocation, explaining the functions used for dynamic memory management.

Uploaded by

Aditya Ray
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

NAME ADITYA KUMAR RAY

ROLL NO. 2314501721


COURSE BCA
SUBJECT CODE DCA1102

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.

c. Structured Programming Language: The C programming language is a very


structured programming language which means that we can create functions in our c
programs which can be used to separate a specific set of operations from the main
program to be used again and again in a program. The C program does not limit its
structured feature to just functions, it also supports loops and conditional statements.

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.

e. Rich Standard Library: C programming Language supports various functions and


libraries that makes the development faster. The standard library support in C
language is very useful as it offers the users a lot of predefined keywords or ready-
made operations. These Libraries are called Header files in C language.
f. Separate Compilation: As in a standard compilation the small codes takes lesser
time when compared to big codes. The C language offers breaking the code into
multiple source code files. The C language complies these source code files separately
and the link them together to be used. This makes the compilation of the program
faster.

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:

1. Conditional Statements: These statements allow the program to make decisions


based on a condition. The most commonly used conditional statements are:
• if statement: this statement executes a block of code if a certain condition is
true. Syntax:
if (condition) {
// statements to execute if condition is true
}
• if-else statement: this statement executes one block of code if a certain
condition is true else it executes another block of code if the statement is false.
Syntax:
if (condition) {
// code to execute if condition is true
} else {
// code to execute if condition is false
}
• nested if-else statement: It has if statements inside another if statement and
runs and execute a code based on different sets of conditions. Syntax:
if (condition1) {
if (condition2) {
// code to be executed if condition2 is true
} else {
// code to be executed if condition2 is false
}
} else {
// code to be executed if condition1 is false
}
2. Looping statements: Looping statements allow a block of code to be repeated
multiple times. The most commonly used looping statements are:
• for loop: this loop executes a block of code for a fixed number of iterations.
Syntax:
for (initialization; condition; increment) {
// code to be executed
}
• while loop: this loop repeats a block of code as long as a certain condition is
true. Syntax:
while (condition) {
// code to be executed
}
• do while loop: this loop repeats a block of code at least once, and then
continues repeating as long as a certain condition is true. Syntax:
do {
// loop body
} while (condition);

3. Control Transfer Statements: These statements change the normal flow of


execution within a program. These statements are also called as Jumping statements.
• Break statements: this statement terminates the execution of a loop or switch
statement. Syntax:
int i;
for (i = 0; i < 10; i++) {
if (i == 5) {
break;
}
printf("%d\n", i);
}
• Continue statements: this statement skips the current iteration of a loop and
continues with the next iteration. Syntax:
int i;
for (i = 0; i < 10; i++) {
if (i % 2 == 0) {
continue;
}
printf("%d\n", i);
}
• Return statements: this statement ends the execution of a function and
returns a value to the caller. Syntax:
return(exp); // where exp is expression which we want to be returned.

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.

User-defined Functions: A user-defined function is a type of function in C language that is


defined by the user to perform some specific task. It provides code reusability and modularity
to the c program. User-defined functions are different from predefined functions as their
working is specified by the user and no header file is required for their usage.

There are four types of user-defined functions divided based on arguments they accept and
the value they return:

• Function with no arguments and no return value: This type of functions


has no arguments and no return values. Such functions can either be used to
display information or to perform any task on global variables. Syntax:
// void return type with no arguments
void function_name()
{
// no return value
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.

• Compile time initialization: Compile-Time initialization is also known as


static-initialization. In this, array elements are initialized when we declare the
array implicitly. Syntax:

<data_type> <array_name> [array_size]={list of elements};

For example: int nums[5] = {0, 1, 2, 3, 4};

A C program showing the compile time initialization:

#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]);
}

The output of the program would be 0,1,2.


we have initialized an array nums of size 3 and elements as 0,1 and 2 in the
list. This is compile-time initialization, and then at the end, we have printed all
its values by accessing index-wise.

• Run time compilation: Runtime initialization is also known as dynamic-


initialization. Array elements are initialized at the runtime after successfully
compiling the program.

For example: scanf("%d", &nums[0]); //initializing 0th index element at


runtime dynamically

A C program showing the run time initialization:

#include < stdio.h >

int main() {

int nums[5];
printf("\n Run-Time Initialization Example:\n");
printf("\n Enter array elements: ");

for (int i = 0; i < 5; i++) {


scanf("%d", & nums[i]);
}

printf(" Accessing array elements after dynamic Initialization: ");

for (int i = 0; i < 5; i++) {


printf("%d ", nums[i]);
}

return 0;
}

Input: Run-Time Initialisation Example:

Enter array elements: 10 20 30 40 50


Output: Accessing array elements after dynamic Initialization: 10 20 30 40 50

To demonstrate runtime initialization, we have just declared an array nums of


size 5 in this C programming code. After that, within a loop, we ask the user to
enter the array values to initialize them after compiling the code. In the end,
we have printed its values by accessing them index-wise.

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.

Syntax for declaring a structure:

We have to declare structure in C before using it in our program. In structure


declaration, we specify its member variables along with their datatype. We can use
the struct keyword to declare the structure in C using the following syntax:

struct structure_name {
data_type member_name1;
data_type member_name1;
....
....
};

Syntax for accessing a structure:

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

B. Difference between Structures and Union in C


Structure Union
The keyword ‘struct’ is used to The keyword ‘union’ is used to
Keywords define a structure. define a union.
The size of structure is greater The size of the union is equal to
Size than or equal to the sum of the the size of the largest member.
size of its members.
Each member is assigned a Memory allocated is shared by
Memory unique storage area of location. individual members of union.
Altering the value of a member Altering the value of any member
Value will not affect other members of will alter the value of other
Altering the structure. members.
Individual members can be Only one member can be
Accessing accessed at a time. accessed at a time.
Several members can be Only first member of the union
initialization initialize at once. can be initialize.

Question 6.

Answers:

Difference between Static and Dynamic Memory allocation

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:

• Compile-time or Static Memory Allocation


• Run-time or Dynamic Memory Allocation

Static Memory Allocation Dynamic Memory Allocation

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

In Static Memory Allocation, there is no In Dynamic Memory Allocation, there is


memory re-usability memory re-usability and memory can be
freed when not required

In static memory allocation, once the In dynamic memory allocation, when


memory is allocated, the memory size can memory is allocated the memory size can be
not change. changed.
In this memory allocation scheme, execution In this memory allocation scheme, execution
is faster than dynamic memory allocation. is slower than static memory allocation.

Example: This static memory allocation is Example: This dynamic memory allocation is
generally used for array. generally used for linked list.

Functions of Dynamic Memory Allocation:

Dynamic memory allocation is a process of allocating memory during the runtime of a


program. It is an essential part of C programming and allows for more flexibility and
efficiency in memory management. The most common functions used for dynamic memory
allocation in C are malloc(), calloc(), realloc(), and free().

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);

You might also like