IP Lab Manual
IP Lab Manual
What is C?
C is a general-purpose, procedural, high-level programming
language used in the development of computer software and applications, system
programming, games, and more.
C language was developed by Dennis M. Ritchie at the Bell Telephone
Laboratories in 1972.
It is a powerful and flexible language which was first developed for the
programming of the UNIX operating System.
C is one of the most widely used programming languages.
C programming language is known for its simplicity and
efficiency. It is the best choice to start with programming as it gives you a
foundational understanding of programming.
Compiling a C Program:
The compilation is the process of converting the source code of the C language
into machine code. As C is a mid-level language, it needs a compiler to convert it
into an executable code so that the program can be run on our machine.
#include <stdio.h>
int main()
{
// Declare the variables
int num;
printf("Enter the integer: ");
scanf("%d", &num);
printf("Entered integer is: %d", num);
return 0;
}
C Program to Print the ASCII Value of a Character
#include <stdio.h>
int main()
{
char c = 'k';
printf("The ASCII value of %c is %d", c, c);
return 0;
}
#include <stdio.h>
int main()
{
int integerType;
char charType;
float floatType;
double doubleType;
printf("Size of int is: %ld", sizeof(integerType));
printf("\nSize of char is: %ld", sizeof(charType));
printf("\nSize of float is: %ld", sizeof(floatType));
printf("\nSize of double is: %ld", sizeof(doubleType));
return 0;
}
Experiment-3
C Program to Add Two Numbers
#include <stdio.h>
int main()
{
int A, B, sum = 0;
printf("Enter two numbers A and B : \n");
scanf("%d%d", &A, &B);
sum = A + B;
printf("Sum of A and B is: %d", sum);
return 0;
}
C Program to Swap Two Numbers
#include<stdio.h>
int main()
{
int x, y;
printf("Enter Value of x ");
scanf("%d", &x);
printf("\nEnter Value of y ");
scanf("%d", &y);
// Using a temporary variable to swap the values
int temp = x;
x = y;
y = temp;
printf("\nAfter Swapping: x = %d, y = %d", x, y);
return 0;
}
Experiment-4
Operator precedence and Associativity:
A single expression in C may have multiple operators of different types. The C
compiler evaluates its value based on the operator precedence and associativity of
operators.
The precedence of operators determines the order in which they are evaluated in an
expression. Operators with higher precedence are evaluated first.
Operator
* Dereference Operator
12 || Logical OR Left-to-Right
14 = Assignment Right-to-Left
Multiplication, division
*= , /=
assignment
assignment
Operator precedence :
#include <stdio.h>
main()
int a = 20;
int b = 10;
int c = 15;
int d = 5;
int e;
e = (a + b) * c / d; // ( 30 * 15 ) / 5
printf("Value of (a + b) * c / d is : %d\n", e );
e = ((a + b) * c) / d; // (30 * 15 ) / 5
printf("Value of ((a + b) * c) / d is : %d\n" , e );
e = (a + b) * (c / d); // (30) * (15/5)
printf("Value of (a + b) * (c / d) is : %d\n", e );
e = a + (b * c) / d; // 20 + (150/5)
printf("Value of a + (b * c) / d is : %d\n" , e );
return 0;
}
2.Write a C-Program for b2-4ac ((b*b) – (4*a*c))
#include<stdio.h>
main()
{
int a,b,c,s;
printf("Enter a,b,c values");
scanf("%d%d%d",&a,&b,&c);
s=(b*b)-(4*a*c);
printf("%d",s);
}
3.Write a C-Program for (a+b)2 ((a*a) + (b*b) + (2*a*b))
#include<stdio.h>
void main()
{
int a,b,c;
printf("Enter a,b values");
scanf("%d%d",&a,&b);
c=((a*a)+(b*b)+(2*a*b));
printf("\n The value of (a+b)2 is: %d",c);
}
1.Write a C-Program for (A+B*C+(D*E)+F*G)
#include<stdio.h>
void main()
{
int a,b,c,d,e,f,g,h;
printf("Enter a,b,c,d,e,f,g values");
scanf("%d%d%d%d%d%d%d",&a,&b,&c,&d,&e,&f,&g);
h=(a+b*c+(d*e)+f*g);
printf("\n The value is: %d",h);
}
Experiment-5
Decision control structure in C can be implemented by using:-
If statement
If-else statement
Nested if else statement
Else-if ladder
Switch-case
}
Write a C-Program for Largest of Three Numbers.//
#include<stdio.h>
void main()
{
int a,b,c;
printf("\n Enter a,b,c Values");
scanf("%d%d%d",&a,&b,&c);
if((a>b)&&(a>c))
printf("%d",a);
else
if(b>c)
printf("%d",b);
else
printf("%d",c);
}
Write a C-Program for Arithmetic Operations Using Switch Statement
#include<stdio.h>
void main()
{
int a,b,c,op;
printf("\n Enter any Two values and Case value");
scanf("%d%d%d",&a,&b,&op);
switch(op)
{
case 1: c=a+b;
printf("\n%d",c);
break;
case 2: c=a-b;
printf("\n%d",c);
break;
case 3: c=a*b;
printf("\n%d",c);
break;
case 4: c=a/b;
printf("\n%d",c);
break;
case 5: c=a%b;
printf("\n%d",c);
break;
default: printf(“\n case value is out of range”);
}
}
Write a C-Program for Months of a Year Using Switch Statement.
#include<stdio.h>
void main()
{
int n;
printf("\n Enter n value");
scanf("%d",&n);
switch(n)
{
case 1: printf("\n January");
break;
case 2: printf("\n February");
break;
case 3: printf("\n March");
break;
case 4: printf("\n April");
break;
case 5: printf("\n May");
break;
case 6: printf("\n June");
break;
case 7: printf("\n July");
break;
case 8: printf("\n August");
break;
case 9: printf("\n September");
break;
case 10: printf("\n October");
break;
case 11: printf("\n November");
break;
case 12: printf("\n December");
break;
default: printf("\n case value is out of range");
}
}
Experiment – 6
Loops in programming are used to repeat a block of code until the specified
condition is met.
Entry Controlled loops: In Entry controlled loops the test condition is checked
before entering the main body of the loop. For Loop and While Loop is Entry-
controlled loops.
Exit Controlled loops: In Exit controlled loops the test condition is evaluated at
the end of the loop body. The loop body will execute at least once, irrespective of
whether the condition is true or false. do-while Loop is Exit Controlled loop.
#include<stdio.h>
void main()
{
int a[10],n,i,p=0;
printf("\n Enter how many Numbers");
scanf("%d",&n);
printf("\n Enter Elements");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
p=p+a[i];
}
printf("\n%d",p);
}
Write a C-Program for Sum of Digits.
#include<stdio.h>
void main()
{
int n,s,p=0;
printf("\n Enter any Number");
scanf("%d",&n);
while(n>0)
{
s=n%10;
p =p+s;
n=n/10;
}
printf("%d",p);
}
#include<stdio.h>
main()
{
int s,n,r=0,m;
printf("\n Enter any Number");
scanf("%d",&n);
m=n;
while(n>0)
{
s=n%10;
r=r*10+s;
n=n/10;
}
if(r==m)
printf("\n Pallindrome Number");
else
printf("\n Not a Pallindrome Number");
}
Write a C-Program for Factorial of a Given Number Using Do-While Loop.
#include<stdio.h>
void main()
{
int n,f=1;
printf("enter any number");
scanf("%d",&n);
do
{
f=f*n;
n--;
}while(n>0);
printf("\n the factorial of a given number is: %d",f);
}
}
Write a C-Program for Fibonacci Series.
#include<stdio.h>
void main()
{
int a=0,b=1,i,n,c;
printf("\n Enter N values");
scanf("%d",&n);
printf("\t%d\t%d",a,b);
for(i=3;i<=n;i++)
{
c=a+b;
printf("\t%d",c);
a=b;
b=c;
}
}
Experiment-7
Arrays in C
Types of Array in C
There are two types of arrays based on the number of dimensions it has. They are
as follows:
One Dimensional Arrays (1D Array)
Multidimensional Arrays
Syntax: Array_name[size];
#include<stdio.h>
void main()
{
int a[10],max,n,i;
printf("Enter size of an Array");
scanf("%d",&n);
printf("\n Enter elements in an Array");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
max=a[0];
for(i=0;i<n;i++)
{
if(a[i]>max)
max=a[i];
}
printf("\n The Maximum Element in an Array is: %d", max);
}
Experiment-8
Two-Dimensional Array in C
Experiment-9
C String Functions
The C string functions are built-in functions that can be used for
various operations and manipulations on strings.
These string functions can be used to perform tasks such as string copy,
concatenation, comparison, length, etc.
The <string.h> header file contains these string functions.
String Functions in C
Some of the commonly used string functions in C are as follows:
1. strcat() Function
The strcat() function in C is used for string concatenation. It will append
a copy of the source string to the end of the destination string.
1) Strcat()
#include <stdio.h>
#include<string.h>
int main() {
char s1[30],s2[30];
printf("Enter first string");
gets(s1);
printf("Enter Second String");
gets(s2);
strcat(s1,s2);
printf("After concatenation:%s",s1);
return 0;
}
2) strcmp()
#include <stdio.h>
#include <string.h>
int main()
{
Experiment-10
Call By Value And Call By reference Mechanisms
#include <stdio.h>
// Function Prototype
void swapx(int x, int y);
int main()
{
int a = 10, b = 20;
swapx(a, b); // Actual Parameters
printf("In the Caller:\na = %d b = %d\n", a, b);
return 0;
}
t = x;
x = y;
y = t;
printf("Inside Function:\nx = %d y = %d\n", x, y);
}
OUTPUT:
Inside Function:
x = 20 y = 10
In the Caller:
a = 10 b = 20
#include <stdio.h>
// Function Prototype
void swapx(int*, int*);
// Main function
int main()
{
int a = 10, b = 20;
// Pass reference
swapx(&a, &b); // Actual Parameters
return 0;
}
Experiment-11
Pointers Basic Programs
1)Assigning Addresses to other variables:
#include <stdio.h>
int main()
{
int a=10,b=20,c,*p,*q;
p=&a;
q=&b;
c=*p;
printf("value of a=%d\n",a);
printf("value of b=%d\n",b);
printf("address of a=%x\n",&a);
printf("address of a=%x\n",p);
printf("address of b=%x\n",q);
printf("value of c=%d",*p);
return 0;
}
2)
#include <stdio.h>
int main()
{
int a=10,b=20,c,*p,*q;
p=&a;
q=&b;
printf("%d,%d,%d",a,*p,*q);
return 0;
}
3)
#include<stdio.h>
int main()
{
int arr[5] = { 1, 2, 3, 4, 5 };
int *ptr = arr;
printf("%p\n", ptr);
return 0;
}
4)
#include<stdio.h>
int main()
{
// Pointer to an integer
int *p;
int main()
{
int* ptr = (int*)malloc(sizeof(int));
Output:Memory freed
Experiment-12
Write C-programs to allocate memory using dynamic memory allocation methods
return 0;
}
OUTPUT:
Enter number of elements:5
Entered number of elements: 5
Memory successfully allocated using malloc.
The elements of the array are: 1, 2, 3, 4, 5,
// This pointer will hold the base address of the block created
int* ptr;
int n, i;
return 0;
}
OUTPUT:
Enter number of elements: 5
Memory successfully allocated using calloc.
The elements of the array are: 1, 2, 3, 4, 5,
Experiment-13
STRUCTURES AND UNIOINS AND FILES
return 0;
}