PC Lab_Manual_(Programming in C)
PC Lab_Manual_(Programming in C)
Practical: 1
Array:
An Array is defined as the collection of similar type of data (same data type) items
stored at contiguous memory locations (store multiple data in a single variable). The array is
the simplest data structure where each data element can be randomly accessed by using its
index number. Array indexes start with 0: [0] is the first element. [1] is the second element.
For example, if we want to store the marks of a student in 5 subjects, then we don't need to
define different variables for the marks in the different subject. Instead of that, we can define
an array which can store the marks in each subject at the contiguous memory locations.
Advantage
1) Code Optimization: Less code to the access the data.
2) Ease of traversing: By using the for loop, we can retrieve the elements of an array easily.
3) Random Access: We can access any element randomly using the array.
Disadvantage
1) Fixed Size: Whatever size, we define at the time of declaration of the array, we can't exceed
the limit. So, it doesn't grow the size dynamically like LinkedList.
Syntax:
1. Declaration of Array:
2. Initialization of Array:
OR
variable_name[index] = value;
1
Programming in 'C'
Memory Representation:
int marks[4] = { 70, 80, 45, 59, 68};
Base address is the address of the first element of an array, pointed by array name.
Index Address
0 2000
1 2004
2 2008
3 2012
4 2016
2
Programming in 'C'
Practical: 1 (A)
Aim: Write a program to read 4 elements in 1D array and display it.
Code:
Output:
3
Programming in 'C'
Practical: 1 (B)
Aim: Write a program to read 4 elements in 2D array and display in matrix
form.
Code:
4
Programming in 'C'
Output:
5
Programming in 'C'
Practical: 1
Post Laboratory Questions
i) int number [ ] = { 0 , 0 , 0 , 0, 0 } ;
6
Programming in 'C'
Grade
Sign
7
Programming in 'C'
Practical: 2
1. Declaration of 2D Array:
2. Initialization of 2D Array:
3. Access the Elements of a 2D Array: To access an element of a two-dimensional array, you must
specify the index number of both the row and column.
Syntax: variable_name[row][columns]=value;
Example: Matrix [0][2];
Output: 3
Memory representation:
int Matrix[4][3] = {{1,2,4},{2,3,4},{3,4,5},{4,5,6}};
8
Programming in 'C'
Practical: 2 (A)
Aim: Write a program to copy elements of one array to another.
Code:
9
Programming in 'C'
Output:
10
Programming in 'C'
Practical: 2 (B)
Aim: Write a program to calculate sum of array elements.
Code:
11
Programming in 'C'
Output:
12
Programming in 'C'
Practical: 2 (C)
Aim: Write a program to find out minimum and maximum number from 1D
array.
Code:
13
Programming in 'C'
Output:
14
Programming in 'C'
Practical: 2
Post Laboratory Questions
3. An array that uses more than two subscript is referred to as _______ array.
Grade
Sign
15
Programming in 'C'
Practical: 3 (A)
Code:
16
Programming in 'C'
Output:
17
Programming in 'C'
Practical: 3 (B)
Aim: Write a program to insert an element in array at particular position.
Code:
18
Programming in 'C'
Output:
19
Programming in 'C'
Practical: 3 (C)
Aim: Write a program to delete an element in array at particular position.
Code:
20
Programming in 'C'
Output:
21
Programming in 'C'
Practical: 3
Post Laboratory Questions
A. Only int type B. Only char type C. Char and int type D. All of them have same type
Grade
Sign
22
Programming in 'C'
Practical: 4 (A)
Aim: Write a program to sort an array in ascending and descending order.
Code:
23
Programming in 'C'
Output:
24
Programming in 'C'
Practical: 4 (B)
Aim: Write a program to read values of two matrices in 2D arrays and multiply
two matrices.
Code:
25
Programming in 'C'
Output:
26
Programming in 'C'
Practical: 4
Post Laboratory Questions
#include<stdio.h>
int main()
{ char str[5] = “ABC”;
printf(str[3]);
printf(str);
return 0; }
Grade
Sign
27
Programming in 'C'
Practical: 5
String:
Strings are used for storing text/characters. For example, "Hello World" is a string
of characters. In C programming, a string is a one-dimensional array of characters that ends
with the special character ‘\0’. Character arrays or strings are used to manipulate text, such as
words or sentences. In strings, the termination character (‘\0’) is very important because it is
the only way to detect where the string ends. Strings can be written as character arrays or
string literals in the C language.
Syntax:
1. Declaration of String:
2. Initializing of String:
Four Ways to Initialize a String in C.
1. Assigning a string literal without size: String literals can be assigned without size. Here,
the name of the string str acts as a pointer because it is an array.
2. Assigning a string literal with a predefined size: String literals can be assigned with a
predefined size. But we should always account for one extra space which will be assigned to
the null character. If we want to store a string of size n then we should always declare a string
with a size equal to or greater than n+1.
3. Assigning character by character with size: We can also assign a string character by
character. But we should remember to set the end character as ‘\0’ which is a null character.
char str[] = { 'C', 'o', 'm', 'p', 'u', 't', 'e', 'r','\0'};
28
Programming in 'C'
Syntax Description
scanf(“%s”,str); Read the string until one white space is found, and store it in
str.
29
Programming in 'C'
Practical: 5
Aim: Read a string “Hello Diwaliba!!!” from terminal using below functions:
scanf (), Edit set command[^\n] , getchar(), gets() .Display above string
on terminal using below functions: printf() , putchar() ,puts().
Code:
30
Programming in 'C'
Output:
31
Programming in 'C'
Practical: 5
Post Laboratory Questions
Grade
Sign
32
Programming in 'C'
Practical: 6 (A)
Aim: Write a program to find length of the string and display on terminal.
Code:
Output:
33
Programming in 'C'
Practical: 6 (B)
Aim: Write a program to copy one string to another and display on terminal.
Code:
Output:
34
Programming in 'C'
Practical: 6 (C)
Aim: Write a program to compare two strings and display on terminal.
Output:
35
Programming in 'C'
Practical: 6
Post Laboratory Questions
3. What is the output of the following code segment? How does it come?
printf(“%d”, strcmp(“push”,”pull”));
4. Assume string has the value “ Hello CGPIT.” What will be the output for following
segment:
a. printf(“%s”,string);
b. puts(string);
c. for(i=0;string[i]!=’ ‘;i++)
{ printf(“%c”,string[i]);}
36
Programming in 'C'
Grade
Sign
37
Programming in 'C'
Practical: 7 (A)
38
Programming in 'C'
Output:
39
Programming in 'C'
Practical: 7 (B)
Aim: Write a program which reads your name from the keyboard and outputs
a list of ASCII codes which represent your name.
Output:
40
Programming in 'C'
Practical: 7
char str1[15];
char str2[15];
int mat;
strcpy(str1, "abcdef");
strcpy(str2, "ABCDEF");
mat= strncmp(str1, str2, 4);
if(mat< 0)
printf("str1 is not greater than str2");
else if(mat> 0)
printf("str2 is is not greater than str1");
else
printf("both are equal");
Q.2 The______ function returns the number of characters that are present before the
terminating null character.
Grade
Sign
41
Programming in 'C'
Practical: 8
Functions:
➢ A function is a block of code that performs a specific task.
➢ We can divide a large program into the basic building blocks known as function.
➢ Functions are used to perform certain actions, and they are important for reusing code:
Define the code once, and use it many times.
➢ C allows you to define functions according to your need. These functions are known as
user-defined functions.
➢ Any function (library or user-defined) has 3 things
1. Function declaration
2. Function calling
3. Function definition
➢ Function declaration:
Syntax: return data_typefunction_name (arguments list);
➢ Function Definition:
Syntax:
return data_typefunction_name (argument list)
{
Body;
}
➢ Function Calling:
Syntax: Function_name (param_list);
➢ Formal Arguments: The arguments which are given at the time of function declaration or
function definition are called formal arguments.
➢ Actual Arguments:
➢ The arguments which are given at the time of function calling are called actual arguments.
Example:
#include <stdio.h>
#include <conio.h>
void CGPIT(); /* Function Declaration */
void main()
{
CGPIT(); /* Function Calling */
}
42
Programming in 'C'
{
printf("\n WELCOME TO THE WORLD OF CGPIT");
}
#include<stdio.h>
int add(int , int );
int main()
{
………..
Sum=add(x , y);
……
}
int add(int n1, int n2 )
{
………..
………..
}
43
Programming in 'C'
Practical: 8 (A)
Aim: Write a program using user-defined function for addition of N numbers.
Code:
Output:
44
Programming in 'C'
Practical: 8 (B)
Aim: Write a program using user-defined function which accepts two
arguments and return the power of them.
Code:
Output:
45
Programming in 'C'
Practical: 8 (C)
Aim: Write a program using user-defined function which calculates area of
circle.
Code:
Output:
46
Programming in 'C'
Practical: 8
Post Laboratory Questions
4. The main is a user-defined function. How does it differ from other user-defined
functions?
i) void xyz( )
ii) xyz( )
Grade
Sign
47
Programming in 'C'
Practical: 9
Recursion:
➢ When a function is calling itself is called recursion.
➢ Recursion is the process of repeating items in a self-similar way. In programming
languages, if a program allows you to call a function inside the same function, then it
is called a recursive call of the function.
➢ It has two things normally: Steps to repeat and terminating condition.
Example:
int m1(int a)
{
if(a==1)
return -1;
else
return a-m1(a-1);
}
Result:4 -1
0 3
48
Programming in 'C'
Practical: 9
Aim: Write a program using user-defined function which finds the sum of digits
of a number using recursion.
Code:
Output:
49
Programming in 'C'
Practical: 9
Post Laboratory Questions
1. What is the output of following code:
main ( )
main ( ) ;
#include<stdio.h>
main()
{
int n,i;
n=f(6);
printf("%d",n);
}
f(int x)
{
if(x==2)
return 2;
else
{
50
Programming in 'C'
printf("+");
f(x-1);
}
}
Grade
Sign
51
Programming in 'C'
Practical: 10
Structure
➢ Structure is a group of variables of different data types represented by a single name.
➢ Lets say we need to store the data of students like student name, age, address, id etc. One
way of doing this would be creating a different variable for each attribute, however when
you need to store the data of multiple students then in that case, you would need to create
these several variables again for each student.
➢ We can solve this problem easily by using structure. We can create a structure that has
members for name, id, address and age and then we can create the variables of this
structure for each student.
struct struct_name {
DataType member1_name;
DataType member2_name;
DataType member3_name;
…
};
How to declare variable of a structure?
struct struct_namevar_name;
or
struct struct_name {
DataType member1_name;
DataType member2_name;
DataType member3_name;
…
} var_name;
52
Programming in 'C'
Practical: 10
Aim: Define a structure called time_struct containing three members integer
hour, integer minute and integer second. Develop a program that would assign
values to individual members and display the time in the following format:
18:30:25.
Code:
53
Programming in 'C'
Output:
54
Programming in 'C'
Practical: 10
Post Laboratory Questions
5. Write two different ways of assigning values to structure members with example.
Grade
Sign
55
Programming in 'C'
Practical: 11
Aim: Define a structure called bank containing account no., holder name and
balance and display them for N holders whose balance is less than 5000.
Code:
56
Programming in 'C'
Output:
57
Programming in 'C'
Practical: 11
Post Laboratory Questions
Grade
Sign
58
Programming in 'C'
Practical: 12
Aim: Define a structure called student_record containing name, date of birth and
total marks obtained. Use the date structure to represent the date of birth. Develop
a program to read data for 10 students in a class and list them rank-wise.
Code:
59
Programming in 'C'
Output:
60
Programming in 'C'
Practical: 12
Post Laboratory Questions
Grade
Sign
61
Programming in 'C'
Practical: 13
Pointers:
➢ Pointers are special variables used to store the address of a variable.
➢ The address of any variable can be found using the special operator (&).
➢ For example: address of variable a is identified using &a.
➢ To declare any variable as pointer, the specified operator (*) will be used.
Pointer declaration:
➢ Syntax: data type * variable_name;
➢ Example:
○ int *p1;
○ int* p1;
○ int * p1;
Assigning address:
int a=10;
int *p1=&a;
Variable a is declared as integer and has the value 10.
Variable p1 is declared as a pointer to integer and has the address of variable a.
Example:
void main()
{
int a=300; // a has the value 300.
int *p=&a; // p is the pointer storing the address of variable a.
printf(“%d\n”,a); //300
printf(“%d\n”,&a); //Address of a
printf(“%d\n”,p); // Address of p
printf(“%d\n”,&p); // Address of p
62
Programming in 'C'
Practical: 13 (A)
Aim: Write a program that demonstrate the use of address (&) and pointer (*)
operators.
Code:
Output:
63
Programming in 'C'
Practical: 13 (B)
Aim: Write a program using user-defined function to interchange the value of two
variables using pointer.
Code:
Output:
64
Programming in 'C'
Practical: 13
Post Laboratory Questions
1. What is pointer?
*( m + 1 ) = 100 ;
*m=*(m+1);
printf( “ % d ” , m [ 0 ] ) ;
65
Programming in 'C'
b. int a, *b=&a;
c. int m;
int **x=&m;
int x=18,y=10;
a. (*p1)++
b. *p1+(*p2)--
c. (*p1)-x+(*p2)-y
Grade
Sign
66
Programming in 'C'
Practical: 14 (A)
Aim: Write a program to compute the sum of all elements stored in an array using
pointers.
Code:
67
Programming in 'C'
Output:
68
Programming in 'C'
Practical: 14 (B)
Aim: Write a program to copy one string to another using pointer and display on
terminal.
Code:
69
Programming in 'C'
Output:
70
Programming in 'C'
Practical: 14 (C)
Aim: Write a program to join two strings using pointer and display on terminal.
Code:
71
Programming in 'C'
Output:
72
Programming in 'C'
Practical: 14
#include <stdio.h>
int main()
{
int iVal;
char cVal;
void *ptr; // void pointer
iVal=50; cVal=65;
ptr=&iVal;
printf("value =%d,size= %d\n",*(int*)ptr,sizeof(ptr));
ptr=&cVal;
printf("value =%d,size= %d\n",*(char*)ptr,sizeof(ptr));
return 0;
}
Grade
Sign
73
Programming in 'C'
Practical: 15
74
Programming in 'C'
Output:
75
Programming in 'C'
Practical: 15
#include <stdio.h>
void fun(int *ptr)
{
*ptr=100;
}
int main()
{
int num=50;
int *pp=#
fun(& *pp);
printf("%d,%d",num,*pp);
return 0;
}
Grade
Sign
76
Programming in 'C'
Practical: 16
File Management:
File Handling is the storing of data in a file using a program. In C programming language, the programs
store results, and other data of the program to a file using file handling in C. Also, we can extract/fetch
data from a file to work with it in the program.
We must open a file before it can be read, write, or update. The fopen() function is used to open a
file. The syntax of the fopen() is given below.
➢ FILE *fopen( const char * filename, const char * mode );
77
Programming in 'C'
The file name (string). If the file is stored at some specific location, then we must mention the path
at which the file is stored. For example, a file name can be like "c://some_folder/some_file.ext".
The mode in which the file is to be opened. It is a string.
We can use one of the following modes in the fopen() function.
Mode Description
r opens a text file in read mode
w opens a text file in write mode
a opens a text file in append mode
r+ opens a text file in read and write mode
w+ opens a text file in read and write mode
a+ opens a text file in read and write mode
rb opens a binary file in read mode
wb opens a binary file in write mode
ab opens a binary file in append mode
opens a binary file in read and write
rb+
mode
opens a binary file in read and write
wb+
mode
opens a binary file in read and write
ab+
mode
78
Programming in 'C'
1. malloc() function:
The malloc() function allocates single block of requested memory. It doesn't initialize memory at
execution time, so it has garbage value initially. It returns NULL if memory is not sufficient.
ptr=(cast-type*)malloc(byte-size)
2. calloc() function:
The calloc() function allocates multiple block of requested memory. It initially initialize all bytes to
zero. It returns NULL if memory is not sufficient.
ptr=(cast-type*)calloc(number, byte-size)
3. realloc() function
If memory is not sufficient for malloc() or calloc(), you can reallocate the memory by realloc()
function. In short, it changes the memory size.
ptr=realloc(ptr, new-size)
4. free() function
The memory occupied by malloc() or calloc() functions must be released by calling free() function.
Otherwise, it will consume memory until program exit.
free(ptr)
79
Programming in 'C'
Practical: 16 (A)
Aim: Write a program to find sum of n elements and allocate memory dynamically
using malloc () function.
Code:
80
Programming in 'C'
Output:
81
Programming in 'C'
Practical: 16 (B)
82
Programming in 'C'
Output:
83
Programming in 'C'
Practical: 16 (C)
84
Programming in 'C'
Output:
85
Programming in 'C'
Practical: 16
Q. 1 Which header file should be included to use functions like malloc() and calloc()?
Q. 3 How will you free the memory allocated by the following program?
#include<stdio.h>
#include<stdlib.h>
#define MAXROW 3
#define MAXCOL 4
int main()
{
int **p, i, j;
p = (int **) malloc(MAXROW * sizeof(int*));
return 0;
}
Grade
Sign
86
Programming in 'C'
Practical: 17 (A)
Aim: Write a program to create a file and check whether exist or not.
Code:
87
Programming in 'C'
Output:
88
Programming in 'C'
Practical: 17 (B)
Aim: Write a program to read the content of a file and display it on terminal.
Code:
89
Programming in 'C'
Output:
90
Programming in 'C'
Practical: 17 (C)
Aim: Write a program to read string from keyboard and store the string content in
a file.
Code:
91
Programming in 'C'
Output:
92
Programming in 'C'
Practical: 17
Grade
Sign
93
Programming in 'C'
Practical: 18 (A)
Aim: Write a program to read content of a file and append the same content in
another file.
Code:
94
Programming in 'C'
Output:
95
Programming in 'C'
Practical: 18 (B)
Aim: Write a program to copy the content of source file into destination file.
Code:
96
Programming in 'C'
Output:
97
Programming in 'C'
Practical: 18 (C)
Aim: Write a program that compares two files and return 0 if they are equal and 1
is they are not.
Code:
98
Programming in 'C'
Output:
99
Programming in 'C'
Practical: 18
#include <stdio.h>
int main(){
FILE *fp;
char *str;
fp=fopen("demo.txt","r");// demo.txt :you are a good programmer
while(fgets(str,6,fp)!=NULL)
puts(str);
fclose(fp);
return 0;
}
Grade
Sign
100
Programming in 'C'
Practical: 19
Aim: Write a program to read content of file and display last n character on
terminal.
Code:
101
Programming in 'C'
Output:
102
Programming in 'C'
Practical: 19
Q. 4 Which function will return the current file position for stream?
Grade
Sign
103