0% found this document useful (0 votes)
8 views5 pages

Structure

The document discusses several C programs that demonstrate the use of structures in different ways: 1) Defining and initializing structure variables to store student details like name, age, roll number and printing their values. 2) Passing a structure as a parameter to a function to calculate the area of a rectangle. 3) Using an array of structures to store information of multiple students and printing them. 4) Comparing the gets() and fgets() functions for reading strings, noting that fgets() is safer as it checks array bounds while gets() is prone to buffer overflows.

Uploaded by

Raja Prasant
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)
8 views5 pages

Structure

The document discusses several C programs that demonstrate the use of structures in different ways: 1) Defining and initializing structure variables to store student details like name, age, roll number and printing their values. 2) Passing a structure as a parameter to a function to calculate the area of a rectangle. 3) Using an array of structures to store information of multiple students and printing them. 4) Comparing the gets() and fgets() functions for reading strings, noting that fgets() is safer as it checks array bounds while gets() is prone to buffer overflows.

Uploaded by

Raja Prasant
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/ 5

 WACP using struct display Name, Roll No.

and Age of  WACP to assign a value to structure variable from


three different student. another structure variable.

#include<stdio.h> #include <stdio.h>

#include<conio.h> #include<conio.h>

#include<string.h> struct Point {

struct student int x;

{ int y;

char name[20]; }p1 = {10, 20}, p2;

int age,rollno; int main() {

}; p2 = p1; // Assigning p1 to p2

int main() printf("p1: (%d, %d)\n", p1.x, p1.y);

{ printf("p2: (%d, %d)\n", p2.x, p2.y);

struct student s1={"XYZ",25,100}; getch();

struct student s2,s3; return 0;

strcpy(s2.name,"MNO"); }

s2.age=20;  WACP to Understand of memory allocation of


structure
s2.rollno=101;
#include<stdio.h>
printf("Enter details of s3: \n"); #include<conio.h>
printf("Enter a Name: "); struct student
gets(s3.name); {
char name[20];
printf("Ener Age and Roll No of the Student:
int age,rollno;
");
};
scanf("%d %d",&s3.age,&s3.rollno);
int main()
printf("Details of Student S1 is as below:\n"); {
printf("Name: %s",s1.name); struct student s1={"XYZ",25,100};
printf("\nAge: %d",s1.age); printf("\nDetails of Student S1 are:");
printf("\nValues of S1: %s %d
printf("\nRoll No.: %d",s1.rollno);
%d",s1.name,s1.age,s1.rollno);
printf("\nDetails of Student S2 is as below:\ printf("\nAddress of S1: %u %u
n"); %u",&s1.name,&s1.age,&s1.rollno);
printf("Name: %s",s2.name); printf("\n%d",sizeof(s1.name));
printf("\nAge: %d",s2.age); getch();
printf("\nRoll No.: %d",s2.rollno); return 0;
}
printf("\nDetails of Student S3 is as below:\
/* From the above program we can see that the
n");
structure elements are stored sequentially or
printf("Name: %s",s3.name); serially.*/
printf("\nAge: %d",s3.age);
printf("\nRoll No.: %d",s3.rollno);
getch();
return 0;
}
Array of structure:

 WACP to demonstrates the usage of an array of


structures.
#include <stdio.h>
#include <conio.h>
struct Student {
char name[50];
int age;
};

int main() {
struct Student students[3] = {
{"Alice", 20},
{"Bob", 21},
{"Carol", 19}
};

for (int i = 0; i < 3; i++) {


printf("Student %d: Name = %s, Age =
%d\n", i+1, students[i].name, students[i].age);
}
getch();
return 0;
}
 WACP program which shows how to pass a
structure as a parameter to a function.
#include <stdio.h>
#include <conio.h>

struct Rectangle {
int length;
int width;
};

int calculateArea(struct Rectangle rect) {


return rect.length * rect.width;
}

int main() {
struct Rectangle r = {5, 10};
int area = calculateArea(r);

printf("Area of the rectangle: %d\n", area);


getch();
return 0;
}
 WACP to demonstrates the usage of an array of n : Maximum number of characters to be copied into
structures. str (including the terminating null-character).
#include <stdio.h> *stream : Pointer to a FILE object that identifies an
#include <conio.h> input stream. stdin can be used as argument to read
from the standard input.
struct Student {
returns : the function returns str
char name[50];
 It follow some parameter such as Maximum
int age;
length, buffer, input device reference.
};
 It is safe to use because it checks the array
bound.
int main() {  It keep on reading until new line character
struct Student students[3]; encountered or maximum limit of character
array.
for (int i = 0; i < 3; i++) { Example : Let’s say the maximum number of
printf("Enter name for student %d: ", i + 1); characters are 15 and input length is greater than 15
but still fgets() will read only 15 character and print it.
fgets(students[i].name, sizeof(students[i].name),
stdin);
// C program to illustrate
// fgets()
printf("Enter age for student %d: ", i + 1);
#include <stdio.h>
scanf("%d", &students[i].age);
#define MAX 15
getchar(); // Clear newline character from the
int main()
input buffer
{
}
char buf[MAX];
fgets(buf, MAX, stdin);
printf("\n--- Student Information ---\n");
printf("string is: %s\n", buf);
for (int i = 0; i < 3; i++) {
printf("Student %d: Name = %s, Age = %d\n", i +
1, students[i].name, students[i].age); return 0;
} }
getch();
Since fgets() reads input from user, we need to
return 0; provide input during runtime.
} Input:
Note: Hello and welcome to OCA Berhampur
For reading a string value with spaces, we can use
either gets() or fgets() in C programming language.
Output:
Here, we will see what is the difference between
gets() and fgets(). Hello and welc
gets()
fgets():-
Reads characters from the standard input (stdin) and
It reads a line from the specified stream and
stores them as a C string into str until a newline
stores it into the string pointed to by str. It stops
character or the end-of-file is reached.
when either (n-1) characters are read, the newline
Syntax:
character is read, or the end-of-file is reached,
whichever comes first. char * gets ( char * str );
Syntax : str :Pointer to a block of memory (array of char)
char *fgets(char *str, int n, FILE *stream) where the string read is copied as a C string.
str : Pointer to an array of chars where the string read returns : the function returns str
is copied.
 It is not safe to use because it does not check };
the array bound.
 It is used to read string from user until int main() {
newline character not encountered.
struct Student s;
Example : Suppose we have a character array of 15
characters and input is greater than 15 characters,
printf("Enter name: ");
gets() will read all these characters and store them
into variable.Since, gets() do not check the maximum fgets(s.name, sizeof(s.name), stdin);
limit of input characters, so at any time compiler may
return buffer overflow error. printf("Enter date of birth (day month year): ");
scanf("%d %d %d", &s.dob.day, &s.dob.month,
// C program to illustrate
&s.dob.year);
// gets()
#include <stdio.h>
printf("\n--- Student Information ---\n");
#define MAX 15
printf("Name: %s", s.name);
printf("Date of Birth: %d/%d/%d\n", s.dob.day,
int main() s.dob.month, s.dob.year);
{
char buf[MAX]; return 0;
}
printf("Enter a string: "); Explanation: This program showcases the concept of a
gets(buf); structure within a structure. We define two structures:
printf("string is: %s\n", buf); "Date" with members day, month, and year, and
"Student" with members name and dob of type
"Date". In the main function, we create a structure
return 0;
variable s to store student information. We prompt
} the user to enter the name and date of birth, storing
the values in the respective structure members.
Since gets() reads input from user, we need to provide Finally, we display the entered student information.
input during runtime.
 Nested Structure Definition
Input:
#include <stdio.h>
Hello and welcome to Berhampur
Output:
struct Date {
Hello and welcome to Berhampur
int day;
int month;
 Structure within Structure
int year;
#include <stdio.h>
};

struct Date {
struct Student {
int day;
char name[50];
int month;
struct Date dob;
int year;
};
};

int main() {
struct Student {
struct Student s = {"Alice", {10, 5, 2000}};
char name[50];
struct Date dob;
printf("Name: %s\n", s.name);
printf("Date of Birth: %d/%d/%d\n", s.dob.day,
s.dob.month, s.dob.year);

return 0;
}

You might also like