Manual-Programming in C
Manual-Programming in C
Practical: 1
Process of creating C program:
The process of creating C programs is starting with writing the code and executing the binary,
which can be divided into 6 steps. These steps group into three: writing the code (Step-1),
building the program (Step- 2 to 4) and running the program (Step- 5 to 6). The steps are as
follows:
2. Preprocessing
3. Compiling
4. Linking
5. Loading
6. Executing
How to Compile and Run a C Program on Ubuntu Linux using the gcc compiler
Step 1. Open up a terminal
This command will invoke the GNU C compiler to compile the file hello.c and output (-o)
the result to an executable called hello.
1
Programming in 'C'
Data types
Data types specify how we enter data into our programs and what type of data we enter.
Following are the examples of some very common data types used in C:
char: It stores a single character and requires a single byte of memory in almost all
compilers.
int: An int variable is used to store an integer.
float: It is used to store decimal numbers (numbers with floating point value) with single
precision.
double: It is used to store decimal numbers (numbers with floating point value) with
double precision.
void type: void type means no value. This is usually used to specify the type of functions
which returns nothing.
Different data types also have different ranges up to which they can store numbers.
Integer type
2
Programming in 'C'
Character type
Type Size(bytes) Range
char or signed char 1 -128 to 127
unsigned char 1 0 to 255
3
Programming in 'C'
Practical: 1
Aim: a) Introduction to Linux programming and compiling process of C program.
4
Programming in 'C'
Practical: 1
Aim: b) Write a program to print "Hello World".
Code:
Output:
5
Programming in 'C'
Practical: 1
Aim: c) Write a program to print values of different data types.
Code:
Output:
6
Programming in 'C'
Practical: 1
Aim: d) Write a program to take input from command line arguments and print
it on screen.
Code:
Output:
7
Programming in 'C'
Practical: 1
Post Laboratory Questions
8
Programming in 'C'
#include (stdio.h)
print (“Hello”);
Grade
Sign
9
Programming in 'C'
Practical: 2
scanf function:
It is used to read the formatted data from the user.
It is included in the stdio.h header file.
Syntax: scanf(“format string”, arg1,arg2...argn);
o The format string specifies the field format in which data is to be entered.
o The arguments arg1,arg2...argnare the variable names in which entered values
should be stored.
Format string contains field specification which is used in interpretation of input data.
Field specification consists of conversion character %, a data type character and field width
(optional).
Examples:
scanf(“%c %d”, &ch,&a); - It reads character data and stored in ch, reads integer data
and stored in a.
scanf(“%d-%d”,&a,&b); - It reads the integer data into form 40-60, stores 40 in a and 60
in b.
scanf(“%3d %5d”,&i,&j); - It reads three digits number into i and five digits number into
j. 3 and 5 specifies width of the data.
10
Programming in 'C'
Practical: 2
Aim: a) Write a program to take two numbers from the user and print its
addition and subtraction on screen.
Code:
Output:
11
Programming in 'C'
Practical: 2
Aim: b) Write a program to print ASCII code for a given character.
Code:
Output:
12
Programming in 'C'
Practical: 2
Aim: c) Write a program to swap values of two variables without using a third
variable.
Code:
Output:
13
Programming in 'C'
Practical: 2
Post Laboratory Questions
A K
G e
P 0
s $
3. What is variable?
14
Programming in 'C'
ii) The scanf function can be used to read only one value at a time.
100
‘R’
15
Programming in 'C'
45.89
9876666
9. What will be the values of variables after executing this code for given inputs?
void main()
int a,b;
scanf(“%2d %4d”,&a,&b);
inputs: 3467 87
a= ______________
b= ______________
Grade
Sign
16
Programming in 'C'
Practical: 3
What is Typecasting in C?
Typecasting is converting one data type into another one. It is also called as data
conversion or type conversion.
C programming provides two types of type casting operations:
o Implicit type casting
o Explicit type casting
Implicit type conversion happens automatically when a value is copied to its compatible
data type. If the operands are of two different data types, then an operand having lower
data type is automatically converted into a higher data type.
#include<stdio.h>
int main(){
short x=5; //initializing variable of short data type
int y; //declaring int variable
y=x; //implicit type casting y is of type int and x is of type short
printf("%d\n",x); short data type is copied into the variable
printf("%d\n",y); a which is of an int data type.
}
Output
5
5
17
Programming in 'C'
(type-name) expression
Here,
• The type name is the standard 'C' language data type.
• An expression can be a constant, a variable or an actual expression.
Let us write a program to demonstrate implementation of explicit type-casting in 'C'.
#include<stdio.h>
int main()
{
float X = 1.7;
Int Y = (int)X + 1; If you write, int Y = X
printf("Value of a is %f\n", X); Then Compiler will throw an error because x is of
printf("Value of b is %d\n",Y); type float and y is of type int
return 0;
}
Output:
Value of X is 1.700000
Value of Y is 2
18
Programming in 'C'
Practical: 3
Aim: a) Write a program to find area of circle.
Code:
Output:
19
Programming in 'C'
Practical: 3
Aim: b) Write a program to convert number from integer to float, float to
integer and integer to character.
Code:
Output:
20
Programming in 'C'
Practical: 3
Aim: c) Write a program to convert temperature from celsius to fehrenhit.
Code:
Output:
21
Programming in 'C'
Practical: 3
Post Laboratory Questions
Grade
Sign
22
Programming in 'C'
Practical: 4
Decision Making Statement
Decision Making
Simple if-else
Nested if-else
Ladder else-if
Simple if-else:
if(condition)
{
Conditio
statement-1;
n
}
else
{ else part if part
statement-2; (statement (stateme
} -2) nt-1)
statement-3; Next line
(statement
-3)
Nested if else:
23
Programming in 'C'
if(condition-1)
{ Tru
if(condition-2) Condition- e
{ 1
statement-1;
}
else
{
Fal
statement-2; se
Condition-
} 2
}
else
{ Tru Fal
e se
if(condition-3)
{ Cond. 1:
Cond. 1: true
statement-3; true Cond. 2:
} Cond. 2: false
else true Statement-
{ Statement- 2
statement-4; 1
Condition-
} 3
}
Next statement;
Tru Fal
e se
Cond. 1:
false Cond. 1:
Cond. 3: false
true Cond. 3:
Statement- false
3 Statement-
4
Next
Statement
else-if ladder
24
Programming in 'C'
if(condition-1)
{ Cond. 1: true
statement-1; Condition-1
Statement-1
}
else if(condition-2)
{
statement-2;
}
Condition-2 Cond. 1: false
else if(condition-3)
Cond. 2: true
{ Statement-2
statement-3;
}
else
{ Cond. 1: false
statement-4; Condition-3 Cond. 2: false
} Cond. 3: true
Next statement; Statement-3
Next Statement
Switch statement
25
Programming in 'C'
switch (condition)
{ Switch
case Label1: Condition
statement-1;
case Label2:
statement-2;
default:
statement-3; case Label-1 statement-1;
} break;
Next statement;
statement-2;
case Label-2
break;
default block;
statement-3;
Next statement
26
Programming in 'C'
Practical: 4
Aim: a) Write a program to find whether a given number is positive, negative
or zero.
Code:
Output:
27
Programming in 'C'
Practical: 4
Aim: b) Write a program to check whether a given number is odd or even.
Code:
Output:
28
Programming in 'C'
Practical: 4
Aim: c) Write a program to find the maximum of three numbers.
Code:
Output:
29
Programming in 'C'
Practical: 4
Aim: d) Write a program using switch statement to check whether a given
character is vowel or not.
Code:
Output:
30
Programming in 'C'
Practical: 4
Post Laboratory Questions
int level=1;
if(level==3)
{
printf(“You are excellent!”);
}
else if(level==2)
{
printf(“Keep it up!!”);
}
else if(level==1)
{
printf(“Work Hard!!”);
}
else
{
printf(“Invalid level..”);
31
Programming in 'C'
4. The _____________ statement transfers the control out of the switch statement.
6. State true / false: else part can be worked without any if statement.
Grade
Sign
32
Programming in 'C'
Practical: 5
What is Loop?
Loops in programming come into use when we need to repeatedly execute a block of
statements.
For example: Suppose we want to print “Hello CGPIT” 10 times.
For Loop: A for loop is a repetition control structure that allows you to efficiently write a loop
that needs to execute a specific number of times.
In Loop, the statement needs to be written only once and the loop will be executed 10 times as shown
below.
In computer programming, a loop is a sequence of instructions that is repeated until a certain condition
is reached.
33
Programming in 'C'
Initialization
Condition
If condition is true
Increment
/Decrement
Next Statement
34
Programming in 'C'
Practical: 5
Aim: a) Write a program to print numbers from 1 to N in ascending and
descending order, where N is the user input.
Code:
Output:
35
Programming in 'C'
Practical: 5
Aim: b) Write a program to find addition of numbers between N1 to N2 which
are divisible by 5, where N1 and N2 are user input.
Code:
Output:
36
Programming in 'C'
Practical: 5
Aim: c) Write a program to find factorial of given number.
Code:
Output:
37
Programming in 'C'
Practical: 5
Post Laboratory Questions
3. Which are the three constructs for performing loop operation in C language?
38
Programming in 'C'
10. The _______ statement is used to skip a part of the statements in a loop.
Grade
Sign
39
Programming in 'C'
Practical: 6
while loop
while(condition)
{ Condition
statement-1;
}
statement-2;
Statement-1
Statement-2
do-while loop
do
{ Statement-1
statement-1;
}while(condition);
statement-2;
Condition
Statement-2
40
Programming in 'C'
Practical: 6
Aim: a) Write a program to count odd numbers present in given range.
Code:
Output:
41
Programming in 'C'
Practical: 6
Aim: b) Write a program to find sum of digits of a given number.
Code:
Output:
42
Programming in 'C'
Practical: 6
Aim: c) Write a program to check whether given number is Armstrong or not.
Code:
43
Programming in 'C'
Output:
44
Programming in 'C'
Practical: 6
Aim: d) Write a program to print prime numbers present between given
range.
45
Programming in 'C'
Practical: 6
Post Laboratory Questions
2. Write equivalent while loop and do-while loop for following for loop.
for(i=20;i>=2;i--)
{
printf(“%d\n”,i);
}
Grade
Sign
46
Programming in 'C'
Practical: 7
Nested loop
Nested loop means a loop statement inside another loop statement. That is why
nested loops are also called as “loop inside loop “.
Syntax for Nested For loop:
while(condition) {
while(condition) {
// statement of inside loop }
// statement of outer loop }
do{ do{
// statement of inside loop
}while(condition);
Note: There is no rule that a loop must be nested inside its own type. In fact, there can be
any type of loop nested inside any type and to any level.
do {
while(condition) {
for (initialization; condition; increment) {
// statement of inside for loop
}
// statement of inside while loop
}
// statement of outer do-while loop
} while(condition);
47
Programming in 'C'
Practical: 7
Aim: a) Write a program to print following pattern:
*
**
***
Code:
Output:
48
Programming in 'C'
Practical: 7
Aim: b) Write a program to print following pattern:
1
22
333
Code:
Output:
49
Programming in 'C'
Practical: 7
Aim: c) Write a program to print following pattern:
1
23
456
Code:
Output:
50
Programming in 'C'
Practical: 7
Aim: d) Write a program to print following pattern:
A
B C
D E F
51
Programming in 'C'
Practical: 7
Post Laboratory Questions
Grade
Sign
52
Programming in 'C'
Practical: 8
Array:
Array is the collection of elements having the same data type.
Multiple values can be stored into one name.
Elements are stored in contiguous memory locations.
Elements can be accessed randomly using index.
The starting index of an array is 0.
Syntax:
Declaration: datatype variable_name[size];
o datatype variable_name[size]={v1,v2,...vn};
Accessing: variable_name[index]=value;
Example:
Declaration: int ary[5];
Accessing: ary[2]=100;
Memory Representation:
int a[6]={10,20,30,40,50,60};
2000 10 a[0]
2004 20 a[1]
2008 30 a[2]
2012 40 a[3]
2016 50 a[4]
2020 60 a[5]
53
Programming in 'C'
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
5 2020
54
Programming in 'C'
Practical: 8
Aim: a) Write a program to store values in an array and print each element on
screen.
Code:
Output:
55
Programming in 'C'
Practical: 8
Aim: b) Write a program to find addition of array elements.
Code:
56
Programming in 'C'
Output:
57
Programming in 'C'
Practical: 8
Aim: c) Write a program to print elements present in even indices.
Code:
Output:
58
Programming in 'C'
Practical: 8
Post Laboratory Questions
i) int number [ ] = { 0 , 0 , 0 , 0, 0 } ;
59
Programming in 'C'
Grade
Sign
60
Programming in 'C'
Practical: 9
Syntax:
Declaration: datatype variable_name[rows][columns];
Initialization:
Example: int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}}; OR
arr[4][3]={1,2,3,2,3,4,3,4,5,4,5,6}
Accessing: variable_name[row][columns]=value;
Example: arr[2][1]=4
Memory representation:
int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
61
Programming in 'C'
Practical: 9
Aim: a) Write a program to copy elements from one matrix to another matrix.
Code:
62
Programming in 'C'
Output:
63
Programming in 'C'
Practical: 9
Aim: b) Write a program to find addition of two matrices.
Code:
64
Programming in 'C'
Output:
65
Programming in 'C'
Practical: 9
Aim: c) Write a program to find maximum elements from a matrix.
Code:
66
Programming in 'C'
Output:
67
Programming in 'C'
Practical: 9
Post Laboratory Questions
3. An array that uses more than two subscript is referred to as _______ array.
Grade
Sign
68
Programming in 'C'
Practical: 10
String:
To represent the string in C language, a character array is used.
Character array is terminated by a null character '\0'.
To initialize string: char c*6+=,‘H’,’e’,’l’,’l’,’o’,’\0’-; OR char c*+=”Hello”;
Syntax Description
scanf(“%s”,str); Read the string until one white space is found, and store it in
str.
69
Programming in 'C'
Practical: 10
Aim: a) Write a program to demonstrate following:
Code:
Output:
70
Programming in 'C'
Practical: 10
Aim: b) Write a program to find length of a string.
Code:
Output:
71
Programming in 'C'
Practical: 10
Aim: c) Write a program to convert string from upper case to lower case.
Code:
Output:
72
Programming in 'C'
Practical: 10
Aim: d) Write a program to find whether given string is palindrome or not
using string handling function.
73
Programming in 'C'
Practical: 10
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+);-
74
Programming in 'C'
Grade
Sign
75
Programming in 'C'
Practical: 11
User define Function
A function is a block of code that performs a specific task.
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 defintion
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 */
}
76
Programming in 'C'
#include<stdio.h>
int add(int , int );
int main()
{
………..
Sum=add(x , y);
……
}
int add(int n1, int n2 )
{
………..
………..
}
77
Programming in 'C'
Practical: 11
Aim: a) Write a program to find last digit of a given number using user
defined function.
Code:
Output:
78
Programming in 'C'
Practical: 11
Aim: b) Write a program to check whether the character is alphabet or not
using user defined function.
Code:
Output:
79
Programming in 'C'
Practical: 11
Aim: c) Design calculator using user defined function.
Code:
80
Programming in 'C'
Output:
81
Programming in 'C'
Practical: 11
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
82
Programming in 'C'
Practical: 12
Recursion:
When a function is calling itself is called recursion.
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
83
Programming in 'C'
Practical: 12
Aim: a) Write a program to find addition of first N numbers using recursion.
Code:
Output:
84
Programming in 'C'
Practical: 12
Aim: b) Write a program to find factorial using recursion.
Code:
Output:
85
Programming in 'C'
Practical: 12
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
{
86
Programming in 'C'
printf("+");
f(x-1);
}
}
Grade
Sign
87
Programming in 'C'
Practical: 13
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;
88
Programming in 'C'
Practical: 13
Aim: Define structure data type called Result having structure member
enrolment number, marks of three subjects. Write a program to calculate
result percentage of 3 students using structure Result.
Code:
89
Programming in 'C'
Output:
90
Programming in 'C'
Practical: 13
Post Laboratory Questions
5. Write two different ways of assigning values to structure members with example.
Grade
Sign
91
Programming in 'C'
Practical: 14
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
92
Programming in 'C'
Practical: 14
Aim: a) Write a program to swap values of two variables using pointer.
Code:
Output:
93
Programming in 'C'
Practical: 14
Aim: b) Write a program to perform addition of two numbers using pointer.
Code:
Output:
94
Programming in 'C'
Practical: 14
Post Laboratory Questions
1. What is pointer?
*( m + 1 ) = 100 ;
*m=*(m+1);
printf( “ % d ” , m [ 0 ] ) ;
95
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
96