0% found this document useful (0 votes)
2 views11 pages

Programming in c

The document is an internal assignment for a BCA student named Pritam Karar, covering topics related to the C programming language. It includes features of C, flow control statements, user-defined functions, arrays, and structures, along with examples and explanations. The assignment is structured into two sets of questions, detailing the syntax and usage of various programming concepts in C.

Uploaded by

adhysatyam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views11 pages

Programming in c

The document is an internal assignment for a BCA student named Pritam Karar, covering topics related to the C programming language. It includes features of C, flow control statements, user-defined functions, arrays, and structures, along with examples and explanations. The assignment is structured into two sets of questions, detailing the syntax and usage of various programming concepts in C.

Uploaded by

adhysatyam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

INTERNAL ASSIGNMENT

NAME PRITAM KARAR


ROLL NUMBER 2314510198
SESSION AUGUST 2023
PROGRAM BCA
SEMESTER I
COURSE CODE DCA1102
COURSE NAME PROGRAMMING IN C

1
SET-I

Q. No.1. Describe various features of the C programming language.

Ans.1. C is the most popular programming language in the world. If you know C, you will have
no problem learning other languages such as Java, Python, C++ and C# because the syntax is
similar. C is very versatile; it can be used in both applications and technologies. C was
developed by Dennis Richie and was implemented at Bell Laboratories in 1972. C has varieties
range of features which can be easily extended by the users. Some important features are
discussed below.

1. Simple: Every C program can be written in simple English language so that it can be
easily understandable and developed by the programmer.

2. Speed: C programs run faster. C is also called middle-level language. It has advantages
of both high-level language and middle-level language. Programs written in C language
run at the speed equals to that same programs written in assembly language. Therefore,
C languages are mostly used in system software development.

3. Rich Libraries: C comes with extensive set of libraries with several built-in functions
that make the life of a programmer easy. Robust libraries and functions in C help even
a beginner to code with ease. The language encourages a user to write additional library
functions of their own.

4. Memory Management: C has the feature of supporting dynamic memory allocation.


You can free the allocate memory by calling free() function in C.

5. C Compiler: C compiler is commonly available for computers of all sizes. The compiler
is generally compact and it generates object programs that are small and highly efficient
when compared with program compiled from other high-level languages.

6. Modularity: Modularity is the ability to breakdown the large module into small sub
module that can easily be managed. Modular programming is an important feature of
structured programming languages like C.

7. Portability: C program language is portable such that allows it to run on any different
operating systems. Because of the portability features, programs written in C work on
different computer systems with little or no modifications.

8. Extendibility: Another feature of C is extendibility. It is the ability to extend the existing


program by adding some new features.

9. Pointers: C provides efficient use of pointers. By using pointers, you can directly
interact with the memory.

10. Statically language: C programming language is a statically type language. Meaning


the type of variable checked at the time of compilation but not at run time. That means

2
each time a programmer types a program they have to mention the type of variable
used.

11. General-purpose language: From system programming to photo editing software, C is


used in various types of applications. Some examples of applications where C is used
are:
I. Operating Systems: Windows, Linux, IOS and Android.
II. Databases: Oracle, MySQL.

Q. No.2. Explain various flow control statements in C with examples.

Ans.2. A flow control statement in C describes the order in which statements will be executed
first. The three types of flow control statements in C are Decision, Looping and Jumping
Control statements.

1. In Decision Control Statements group of statements are executed when conditions are
true. If conditions are false then else part statements are executed. The five types of
decision control statements are if statements, if else statements, if else if ladder, nested
if else statements and switch case statements.

I. In if statements, when the condition is only true then the execution will be
possible. If the condition is false then the compiler skips the statement. In C we
can have any number of if statements.

Example:
#include <stdio.h>
int main() {
int a=2,b=2;
if(a==b)
printf("a and b are equal");
return 0;
}

II. In if else control statements, group of statements are executed when condition
is true. If condition is false then else part will execute.

III. The if else if ladder is the extension of if else statements. If the condition is true
then the statement defined in the if block will be executed, otherwise the
condition defined in else if block will be executed. If no conditions are true,
then else block will be executed.

Example:
#include <stdio.h>
int main() {
int mark;
printf("Enter the marks of the students:\n");
scanf("%d, &mark");
if(mark>=60)
printf("1st Division");
else if (mark>=40)

3
printf("2nd Division");
else if(mark>=25)
printf("Pass mark");
else
printf("Fail");
return 0;
}

IV. In Nested if else statements, one if else statement can be inside of another if else
statement.

Example:
#include <stdio.h>
int main() {
int n;
printf("Enter a number:\n");
scanf("%d, &n");
if(n%2==0){
printf("Even Number ");
if(n%4==0){
printf("and divisible by 4");
}
else{
printf("and not divisible by 4");
}
}
else{
printf("Odd Number");
}
return 0;
}

V. In Switch statement in C allows us to execute of multiple operations for the


different possible values of a single variable. It is an alternate version of if else
if ladder.

2. Looping statements in C execute the sequence of statements many times until the stated
condition becomes false. The three types of looping statements are while, for and do-
while loop.

I. In while loop, a condition is evaluated before the body processes. If a condition


is true, only then the body of the loop executed.

Example:
#include <stdio.h>
int main(){
int n=234;
while(n>0){
d=n%10;
printf("%d\n",d);

4
n=n/10;
}
return 0;
}

II. In For loop, the initial value is performed only once, then the condition tests and
compares the counter to a fixed value for each iteration. The loop will stop when
false is returned.

Example:
#include <stdio.h>
int main(){
int n;
for(n=1;n<=10;n++){
printf("%d ",n);
}
return 0;
}

III. In do-while loop, the condition is always executed after the body. It is also
called an exit-controlled loop.

Example:
#include <stdio.h>
int main(){
int n=1;
do{
printf("%d ",n);
n++;
}while(n<=10);
return 0;
}

3. Jump statement makes the control jump in another section of the program
unconditionally when encountered. It is used to terminate the loop or switch-case
instantly. The three jump statements are break, continue and goto statements.

Q. No.3. Define a function. List and explain the categories of user-defined functions.

Ans.3. A function is a block of statements that perform a specific task. C function contains set
of instructions enclosed by “()” which performs specific operation. C program contains one or
more functions. One of these functions must be called main. A function has a body containing
the actual instructions to carry out the task the function is supposed to perform and may have
returned a value of a particular type. When you need to perform the same task more than once,
you have two options. Either use the same set of statements every time you want to perform
the task or create a function to perform the task and just call it every time you perform that
task.

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

5
1. Functions with no arguments and no return type can either be used to display
information or to perform any task on variable.

Example:
#include <stdio.h>
void sum();
int main() {
sum();
return 0;
}
void sum(){
int x,y,s=0;
printf("Enter x and y:\n");
scanf("%d %d", &x, &y);
s=x+y;
printf("Sum of %d and %d are = %d", x, y, s);
}

2. Functions with no arguments and a return type is used to perform specific


operations and return their value.

Example:
#include <stdio.h>
int sum();
int main() {
int s=0;
s=sum();
printf("Sum of x and y is %d", s);
return 0;
}
int sum(){
int x,y,s=0;
printf("Enter x and y:\n");
scanf("%d %d", &x, &y);
s=x+y;
return s;
}

3. Functions with arguments and no return type is used to display or perform some
operations on given arguments.

Example:
#include <stdio.h>
void sum (int,int);
int main() {
int x,y;
printf("Enter the number:\n");
scanf("%d %d", &x, &y);
sum(x,y);
return 0;

6
}
void sum (int x, int y){
int s=0;
s=x+y;
printf("Sum of %d and %d is = %d", x, y, s);
}

4. Functions with arguments and a return type is used to perform specific


operations and return their values to the users.

Example:
#include <stdio.h>
int sum (int,int);
int main() {
int x,y,s=0;
printf("Enter the number:\n");
scanf("%d %d", &x, &y);
s=sum(x,y);
printf("Sum of %d and %d is = %d", x, y, s);
return 0;
}
int sum (int x, int y){
int s=0;
s=x+y;
return s;
}

SET-II

Q. No.4. Define an array. How to initialize a one-dimensional array? Explain with suitable
examples.

Ans.4. Array in C is the most used data structures in C programming. It is a simple and fast
way of storing multiple values in a single name. An array in C is a fixed-size collection of
similar data items stored in contiguous memory locations. It can be used to store the collection
of primitive data type such as int, float, double and also derived and user-defined data types
such as pointers, structures. However, in order to store together in a single array, all the
elements should be of the same data type. The elements are stored in left to right with the
leftmost index being the 0th index and the rightmost index being the (n-1) index.
Array in C are two types which are single dimensional array and
multidimensional array.

One Dimensional Array Initialization: -


In order to initialize, we have to first declare the array like any other variable before
using it in C programming language. We can declare an array by specifying its name, the data
type of its elements and the size of its dimensions. When we declare an array, the compiler
allocates the memory block of the specified size to the array name. The array is static in nature
i.e., it allocates the memory at compile time.
The Syntax of a one-dimensional array declaration:
data_type array_name [size];

7
The initialization in C is the process to assign some initial value to the variable. When the array
is declared or allocated memory, the elements contain some garbage value. So, we need to
initialize the array to some meaningful value. There are multiple ways to initialize an array in
C.
1. In Array initialization with declaration method, we initialize the array along
with its declaration. We use an initializer list to initialize multiple elements of
the array. An initialize list is the list of values enclosed within braces { }
separated by a comma.
Syntax: data_type array_name [size] = {value1, value2……valueN};

2. If we initialize an array using an initializer list, we can skip declaring the size
of the array. As the size of the array in these cases is equal to the elements
present in the initializer list, the compiler can automatically deduce the size of
the array.
Syntax: data_type array_name [ ] = {1,2,3,4,5};
The size of the above array is 5 which is automatically deducted by compiler.

3. We initialize the array after the declaration by assigning the initial value to each
element individually. We can use for, while and do-while loop to assign the
value to each element of the array.
Syntax: for (int i = 0; i < N; i++){
array_name [i] = value of i;
}

Example: C program to demonstrate single dimensional array initialization


#include <stdio.h>
int main() {
// array initialization using intializer list
int arr[7]={10,20,30,40,50,60,70};
//array initialization using intializer list without specifying size
int arr1[]={1,3,5,7,9,11,13};
//array initialization using for loop
int arr2[5],i;
for(i=0; i<5; i++){
arr2[i]=i+1;
}
return 0;
}

Q. No.5. (a) Define Structure and write the general syntax for declaring and accessing
members.
(b) List out the differences between unions and structures.

Ans.5. (a). During programming, there is a need to store multiple logically related elements
under one roof. In such cases, C language provides structures to the job for us. The structure 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 a structure. The items are the structure are called
members and they can be any valid data type. All the data members inside the structure are
accessible function defined outside the structure.

8
We can define a structure by declaring the data members
of different data types according to our need. This declaration takes place before the main
program in C. We can use the struct keyword to define the structure in C using following
general syntax:

struct structure_name {
data_type data_member1;
data_type data_member2;
……….
……….
}
The above syntax is also called structure template or structure prototype and no memory is
allocated to the structure in the declaration.

For accessing the data members in the main function, we need to create a structure variable.
To create structure variable, first declare structure variable after structure template. The syntax
for declaring structure variable is:
struct structure_name structure_variable;

For pointer variables the syntax is same as creating a pointer of integer like,
struct structure_name *structPtrVariable;

We can access structure member by using the (.) dot operator. For accessing structure member
through structure variable, the syntax is:
structure_variable.data_member1;
structure_variable.data_member2;

In the case of pointer variables, we use arrow operator to access the member.
structPtrVariable => data_member1;
structPtrVariable => data_member2;

Ans.5. (b).

Features Structure Union

Keyword ‘struct’ is used to Keyword ‘union’ is used to


Keyword
define a structure. define a union.

The size of the structure is


The size of the union is the
the sum of the size of all the
Size size of its data member
data members and the
which is largest in size.
packing size.

Every data members are All data members shared a


provided appropriate single memory address and
Memory allocation
memory at different memory occupied the memory of
addresses. largest data member.

9
Initialization of data All the data members can be Only one data member can
members initialized at once. be initialized at a time.

Generally, more memory Saves more memory by


Memory usage used due to separate sharing space amongst all
memory allocation. the data members.

Inefficient and requires


Memory management Efficient.
packing memory.

Where complex data


Where memory efficiency is
Use structure with various
crucial.
members are present.

Q. No.6. Explain the difference between static memory allocation and dynamic memory
allocation in C. Explain various dynamic memory allocation function in C.

Ans.6.

Static Memory Allocation Dynamic Memory Allocation

Static memory allocation done before Dynamic memory allocation done during
program execution. program execution.

The reusability feature is available here. A


In this memory allocation, unused memory user can allocate more memory when
cannot be reused. required and also can release memory when
it is needed.

It uses stack for managing static allocation It uses heap for managing dynamic
of memory. allocation of memory.

The memory is allocated at compile time. The memory is allocated at run time.

This is generally used for array. This is generally used for link list.

Dynamic memory allocation can be defined as a procedure in which the size of data structure
can be changed during run time. There are four library functions defined under <stdlib.h>
header file to facilitate dynamic memory allocation in C programming language. They are
malloc ( ), calloc ( ), free ( ) and realloc ( ).

1. The “malloc” or “memory allocation” method is used to dynamically allocate a single


large block of memory with the specified size. It returns a pointer of type void which

10
can be a pointer of any data type. It initializes each block with a default garbage value
initially. Syntax of malloc ( ) in C:
ptr = (data_type*) malloc (byte_size);
For example:
ptr = (int*) malloc ( 20 * sizeof (int) );
Since integer has a size of 4 bytes, the statement will allocate 80 bytes of memory. The
pointer ptr holds the address of the first byte in the allocated memory.

2. The “calloc” or “contiguous allocation” memory is used to dynamically allocate the


specified number of blocks of memory of the specified type. It is very similar to malloc
( ) but has two different points, that are it initializes each block with a default value 0
and has two parameters compared to malloc ( ). Syntax of calloc ( ) in C:
ptr = (data_type*) calloc ( n, element size);
Here, n is the number of elements and element size is size of each element.
For example:
Ptr = (int*) calloc ( 5, sizeof (int) );
This statement allocates contiguous space in memory for 5 elements each with the size
of 4 bytes.

3. The “free” method is used to dynamically de-allocate the memory. The memory
allocated using functions malloc ( ) and calloc ( ) is not de-allocated on their own.
Hence, free ( ) method is used and helps to reduce wastage od memory by freeing it.
The syntax of free ( ) in C:
free (ptr);

4. The “realloc” or “re-allocation” method is used to dynamically change the memory


allocation of the previously allocated memory. If the memory allocated by malloc ( ) or
calloc ( ) is insufficient, then realloc can be used to dynamically re-allocate memory.
Re-allocation of memory maintains the already present value and initializes new blocks
with the default garbage value. The syntax of realloc ( ) in C:
ptr = realloc( ptr, newsize);
where pointer is re-allocated with new size ‘newsize’.

11

You might also like