0% found this document useful (0 votes)
22 views34 pages

IP Lab Manual

Ip manual

Uploaded by

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

IP Lab Manual

Ip manual

Uploaded by

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

Department of Computer Science & Engineering

Introduction to Programming (IP LAB)


Lab Manual
(Academic Regulation-2023)
List of Experiments:

1. Write C-Program to familiarization of with Programming


environment
2. Write simple c-programs with printf() and scanf() statements
3. Write C-programs to simple computational problems using arithmetic
expressions
4. Write C-Program to computational problems using operator
precedence and associativity
5. Write C-programs involving if-then-else structures
6. Write C-programs on while and for loops
7. Write c-Programs using single dimensional array
8. Write C-Programs using two dimensional array
9. Write c-programs on string handling functions
10. Write C-programs to implement call by value and call by reference
mechanisms
11. Write a C-programs to perform operations on addresses of variables
using pointers
12. Write C-programs to allocate memory using dynamic memory
allocation methods
13. Write C-programs using structures and unioins and File Handling
Functions
Experiment-1
Write C-Programs to familiarization of with Programming environment

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.

The C program goes through the following phases during compilation:

C Hello World Program


#include <stdio.h>// main function - where the execution of program
int main()
{
printf("Hello World");
return 0;
}
C Program to Print Your Own Name
#include <stdio.h>
int main()
{
printf("Name : Your Name ");
printf(“Roll No :Your Rollno”);
printf(“Address :Your Address”);
return 0;
}
Experiment-2
2) C Program to Print an Integer Entered By the User

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

C Program to Find the Size of int, float, double, and char

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

C Program to Calculate Fahrenheit to Celsius


#include <stdio.h>
// Function to convert Degree Fahrenheit to Degree Celsius
float fahrenheit_to_celsius(float f)
{
return ((f - 32.0) * 5.0 / 9.0);
}
int main()
{
float f = 40;
// Passing parameter to function
printf("Temperature in Degree Celsius : %0.2f",
fahrenheit_to_celsius(f));
return 0;
}
C Program to Find Simple Interest
#include <stdio.h>
int main()
{
float P = 1500, R = 1, T = 1;
float SI = (P * T * R) / 100;
printf("Simple Interest = %f\n", SI);
return 0;
}
C Program for Area And Perimeter Of Rectangle
#include <stdio.h>
int main()
{ int l = 10, b = 10;
printf("Area of rectangle is : %d", l * b);
printf("\nPerimeter of rectangle is : %d", 2 * (l + b));
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

Precedence Description Associativity

() Parentheses (function call)

Array Subscript (Square


[]
Brackets)
1 Left-to-Right
. Dot Operator

-> Structure Pointer Operator

++ , — Postfix increment, decrement

++ / — Prefix increment, decrement

+/– Unary plus, minus

Logical NOT, Bitwise


!,~
complement
2 Right-to-Left
(type) Cast Operator

* Dereference Operator

& Addressof Operator

Sizeof Determine size in bytes

3 *,/,% Multiplication, division, modulus Left-to-Right


Operator

Precedence Description Associativity

4 +/- Addition, subtraction Left-to-Right

Bitwise shift left, Bitwise shift


5 << , >> Left-to-Right
right

Relational less than, less than or


< , <=
equal to
6 Left-to-Right
Relational greater than, greater
> , >=
than or equal to

Relational is equal to, is not equal


7 == , != Left-to-Right
to

8 & Bitwise AND Left-to-Right

9 ^ Bitwise exclusive OR Left-to-Right

10 | Bitwise inclusive OR Left-to-Right

11 && Logical AND Left-to-Right

12 || Logical OR Left-to-Right

13 ?: Ternary conditional Right-to-Left

14 = Assignment Right-to-Left

+= , -= Addition, subtraction assignment

Multiplication, division
*= , /=
assignment

%= , &= Modulus, bitwise AND


Operator

Precedence Description Associativity

assignment

Bitwise exclusive, inclusive OR


^= , |=
assignment

Bitwise shift left, right


<<=, >>=
assignment

15 , comma (expression separator) Left-to-Right

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 Two Numbers


#include<stdio.h>
void main()
{
int a,b;
printf("Enter any two values");
scanf("%d%d",&a,&b);
if(a>b)
printf("%d",a);
else
printf("%d",b);
}

Write a C-Program for Given Number is Even or Odd.


#include<stdio.h>
void main()
{
int n;
printf("\n Enter n Value");
scanf("%d",&n);
if(n%2==0)
printf("\n Even Number");
else
printf("\n Odd Number");

}
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.

A loop statement allows programmers to execute a statement or group


of statements multiple times without repetition of code.

There are mainly two types of loops in C Programming:

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.

Write a C-Program for Sum of First N Natural Numbers.

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

Write a C-Program for Given Number is Palindrome or Not.

#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 Given Number is Prime or Not.


#include<stdio.h>
void main()
{
int n,c=0,i;
printf("\n Enter any Number");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(n%i==0)
c=c+1;
}
if(c==2)
printf("\n Prime Number");
else
printf("\n Not a Prime Number");

}
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

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 types such as int,
char, float, etc., and also derived and user-defined data types such as pointers,
structures, etc.

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

One Dimensional Array in C


The One-dimensional arrays, also known as 1-D arrays in C are those arrays that
have only one dimension.

Syntax: Array_name[size];

Linear Search is a method for searching an element in a collection of


elements.

In Linear Search, each element of the collection is visited one by one in a


sequential fashion to find the desired element.

Linear Search is also known as Sequential Search.

Write a C-Program for Linear or Sequential Search.


#include<stdio.h>
main()
{
int a[10],i,flag=0,n,x;
clrscr();
printf("\n Enter how many Numbers");
scanf("%d",&n);
printf("\n Enter Elements");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n Enter Search Element");
scanf("%d",&x);
for(i=0;i<n;i++)
{
if(a[i]==x)
flag=1;
}
if(flag==1)
printf("\n Element is Found");
else
printf("\n Element is Not Found");
}

Write a C-Program for Finding Maximum Number in an Array

#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

A Two-Dimensional array or 2D array in C is an array that has


exactly two dimensions. They can be visualized in the form of rows and columns
organized in a two-dimensional plane.

Write a c-program for matrix addition


#include<stdio.h>
void main()
{
int i,j,m,n,a[10][10],b[10][10],c[10][10];
printf("\n Enter how many Rows and Columns");
scanf("%d%d",&m,&n);
printf("\n Enter elements of A");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\n Enter elements of B");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&b[i][j]);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
c[i][j]=a[i][j]+b[i][j];
printf("\n The Output Matrix is:");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("\t%d",c[i][j]);
}
}
Write a C-Program for Matrix multiplication
#include<stdio.h>
void main()
{
int i,j,m,n,k,a[10][10],b[10][10],c[10][10];
printf("\n Enter how many Rows and Columns");
scanf("%d%d",&m,&n);
printf("\n Enter elements of A");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\n Enter elements of B");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&b[i][j]);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
c[i][j]=0;
for(k=0;k<m;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
printf("\n The Output Matrix is:");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
printf("\t%d",c[i][j]);
}

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()
{

char str1[] = "ECE";

char str2[] = "For";

char str3[] = "ECEB SECTION IS SOO GOOD";

int result1 = strcmp(str1, str2);


int result2 = strcmp(str2, str3);
int result3 = strcmp(str1, str1);
printf("Comparison of str1 and str2: %d\n", result1);
printf("Comparison of str2 and str3: %d\n", result2);
printf("Comparison of str1 and str1: %d\n", result3);
return 0;
}
3) strcpy() function
#include <stdio.h>
#include <string.h>
int main()
{
char str1[] = "Hello World!";
char str2[] = "GfG";
char str3[40];
char str4[40];
char str5[] = "Cprogramming";
strcpy(str2, str1);
strcpy(str3, "Copy successful");
strcpy(str4, str5);
printf("str1: %s\nstr2: %s\nstr3: %s\nstr4:%s\n", str1,
str2, str3, str4);
return 0;
}
4) strrev() Function
#include <stdio.h>
#include <string.h>
int main()
{
char str[50] = "geeksforgeeks";
printf("The given string is =%s\n", str);
printf("After reversing string is =%s", strrev(str));
return 0;
}
5) Srting palindrome Program:
#include <stdio.h>
#include<string.h>
void main() {
char str[10],rev[10];
int result;
printf("Enter string\n");
scanf("%s",&str);
strcpy(rev,str);
strrev(rev);
if(strcmp(str,rev)==0)
printf("Given string is palindrome");
else
printf("given string is not palindrome");
}
The strlen() function calculates the length of a given string. It doesn’t
count the null character ‘\0’.
6) strlen()
#include <stdio.h>
#include <string.h>
int main()
{
char str[] = "Introduction to programming";
// getting length of str using strlen()
int length = strlen(str);
printf("Length of string is : %d", length);
return 0;
}
7) Strupr() function
#include<stdio.h>
#include<string.h>
int main()
{
char str[ ] = "nsrit is the best";
//converting the given string into uppercase.
printf("%s\n", strupr (str));
return 0;
}
8) strlwr() function
#include<stdio.h>
#include<string.h>
int main()
{
char str[ ] = "NSRIT IS THE BEST";
//converting the given string into lowercase.
printf("%s\n", strlwr(str));
return 0;
}

Experiment-10
Call By Value And Call By reference Mechanisms

1) Swap Two numbers Using Call by Value

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

// Swap functions that swaps two values


void swapx(int x, int y) // Formal Parameters
{
int t;

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

2) Swap two Numbers Using Call By Reference

#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

printf("Inside the Caller:\na = %d b = %d\n", a, b);

return 0;
}

// Function to swap two variables by references


void swapx(int* x, int* y) // Formal Parameters
{
int t;
t = *x;
*x = *y;
*y = t;

printf("Inside the Function:\nx = %d y = %d\n", *x, *y);


}
OUTPUT :
Inside the Function:
x = 20 y = 10
Inside the Caller:
a = 20 b = 10

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

Pointers and Arrays:

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;

// Pointer to an array of 5 integers


int (*ptr)[5];
int arr[5];

// Points to 0th element of the arr.


p = arr;

// Points to the whole array arr.


ptr = &arr;
printf("p = %p, ptr = %p\n", p, ptr);
p++;
ptr++;
printf("p = %p, ptr = %p\n", p, ptr);
return 0;
}
Dangling Pointer: When a memory pointed by a pointer is deallocated the pointer
becomes a dangling pointer.
#include <stdio.h>
#include <stdlib.h>

int main()
{
int* ptr = (int*)malloc(sizeof(int));

// After below free call, ptr becomes a dangling pointer


free(ptr);
printf("Memory freed\n");
// removing Dangling Pointer
ptr = NULL;
return 0;
}

Output:Memory freed
Experiment-12
Write C-programs to allocate memory using dynamic memory allocation methods

Dynamic Memory Allacation:


#include <stdio.h>
#include <stdlib.h>
int main()
{

// This pointer will hold the


// base address of the block created
int* ptr;
int n, i;

// Get the number of elements for the array


printf("Enter number of elements:");
scanf("%d",&n);
printf("Entered number of elements: %d\n", n);

// Dynamically allocate memory using malloc()


ptr = (int*)malloc(n * sizeof(int));

// Check if the memory has been successfully


// allocated by malloc or not
if (ptr == NULL) {
printf("Memory not allocated.\n");
exit(0);
}
else {

// Memory has been successfully allocated


printf("Memory successfully allocated using malloc.\n");

// Get the elements of the array


for (i = 0; i < n; ++i) {
ptr[i] = i + 1;
}

// Print the elements of the array


printf("The elements of the array are: ");
for (i = 0; i < n; ++i) {
printf("%d, ", ptr[i]);
}
}

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,

callaoc() : Contiguous Memory Allocation


#include <stdio.h>
#include <stdlib.h>
int main()
{

// This pointer will hold the base address of the block created
int* ptr;
int n, i;

// Get the number of elements for the array


n = 5;
printf("Enter number of elements: %d\n", n);

// Dynamically allocate memory using calloc()


ptr = (int*)calloc(n, sizeof(int));

// Check if the memory has been successfully allocated by calloc or not


if (ptr == NULL) {
printf("Memory not allocated.\n");
exit(0);
}
else {

printf("Memory successfully allocated using calloc.\n");


// Get the elements of the array
for (i = 0; i < n; ++i) {
ptr[i] = i + 1;
}
// Print the elements of the array
printf("The elements of the array are: ");
for (i = 0; i < n; ++i) {
printf("%d, ", ptr[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

CREATING STRUCTURE OF A STUDENT :


#include<stdio.h>
#include <string.h>
struct student
{
int roll_no;
char name[30];
}s;
void main( )
{
s.roll_no = 1;
strcpy(s.name, “sai”);
printf("Student Enrollment No. : %d\n", s.roll_no);
printf("Student Name : %s\n", s.name);
}
Output:
Student Enrollment No. : 1
Student Name : sai

Structures Example Program-2


#include<stdio.h>
struct student
{
char name[30];
int htno;
char gender;
int marks;
};
void main()
{
struct student st={"SAI",999,'M',90};
printf("Student Name:%s\n",st.name);
printf("Roll No:%d\n",st.htno);
printf("Gender:%c\n",st.gender);
printf("Marks obtained:%d\n",st.marks);
}
Output:
Student Name:SAI
Roll No:999
Gender:M
Marks obtained:90
Nested Structures:
#include<stdio.h>
struct address
{
char city[20];
int pin;
char phone[14];
};
struct employee
{
char name[20];
struct address add;
};
void main ()
{
struct employee emp;
printf("Enter employee information?\n");
scanf("%s %s %d %s",emp.name,emp.add.city, &emp.add.pin, emp.add.phone);
printf("Printing the employee information ...\n");
printf("name:%s\nCity:%s\nPincode:%d\nPhone:
%s",emp.name,emp.add.city,emp.add.pin, emp.add.phone);
}
OUTPUT: Enter employee information?
Sai
vijayawada
123123
1234567890
Printing the employee information....
name: sai
City: Vijayawada
Pincode: 123123
Phone: 1234567890

SELF REFERENTIAL STRUCTURE:


In C, A self-referential structure is a structure that contains a pointer to a variable
of the same type. This allows the structure to refer to itself, creating a linked data
structure.

Syntax: struct node


{
Int data1;
Char data2;
struct node *link;
}
Files
#include <stdio.h>
#include <stdlib.h>
int main()
{
// file pointer
FILE* fptr;

// creating file using fopen() access mode "w"


fptr = fopen("file.txt", "w");

// checking if the file is created


if (fptr == NULL) {
printf("The file is not opened. The program will "
"exit now");
exit(0);
}
else {
printf("The file is created Successfully.");
}

return 0;
}

OUTPUT: The file is created Successfully

You might also like