0% found this document useful (0 votes)
13 views6 pages

C Structure for Student Information

Structures allow users to define complex data types that can group together data of different types. They are like arrays but can hold different data types. A structure is defined using the struct keyword followed by the structure tag name and members inside curly braces. Structure variables are declared like normal variables and members can be accessed using the dot operator. Structures can be initialized at declaration or by assigning values to members separately. Arrays of structures can also be defined where each element is a structure.

Uploaded by

arindam samanta
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)
13 views6 pages

C Structure for Student Information

Structures allow users to define complex data types that can group together data of different types. They are like arrays but can hold different data types. A structure is defined using the struct keyword followed by the structure tag name and members inside curly braces. Structure variables are declared like normal variables and members can be accessed using the dot operator. Structures can be initialized at declaration or by assigning values to members separately. Arrays of structures can also be defined where each element is a structure.

Uploaded by

arindam samanta
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

Structure:

Structure is a user-defined datatype in C language which allows us to combine data of different types
together.

Structure helps to construct a complex data type which is more meaningful. It is somewhat similar to
an Array, but an array holds data of similar type only. But structure on the other hand, can store
data of any type, which is practical more useful.

For example: If I have to write a program to store Student information, which will have Student's
name, age, branch, permanent address, father's name etc, which included string values, integer
values etc, how can I use arrays for this problem, I will require something which can hold data of
different types together.

In structure, data is stored in form of records.

Defining a structure
struct keyword is used to define a structure. struct defines a new data type which is a collection of
primary and derived data types.

Syntax:

struct structure_tag

Datatype member_variable_1;

Datatype member_variable_2;

Datatype member_variable_3;

...

}structure_variables;

Explanation:

As you can see in the syntax above, we start with the struct keyword, then it's optional to provide
your structure a name, we suggest you to give it a name, then inside the curly braces, we have to
mention all the member variables, which are nothing but normal C language variables of different
types like int, float, array etc.

After the closing curly brace, we can specify one or more structure variables, again this is optional.
Note: The closing curly brace in the structure type declaration must be followed by a semicolon(;).

Example of Structure definition:

struct Student

char name[25];

int age;

char branch[10];

// F for female and M for male

char gender;

};

Explanation: Here struct Student declares a structure to hold the details of a student which consists
of 4 data fields, namely name, age, branch and gender. These fields are called structure elements or
members.

Each member can have different datatype, like in this case, name is an array of char type and age is
of int type etc. Student is the name of the structure and is called as the structure tag.

Declaring Structure Variables:

It is possible to declare variables of a structure, either along with structure definition or after the
structure is defined. Structure variable declaration is similar to the declaration of any normal
variable of any other datatype. Structure variables can be declared in following two ways:

1) Declaring Structure variables separately

struct Student
{
char name[25];
int age;
char branch[10];
//F for female and M for male
char gender;
};
struct Student S1, S2; //declaring variables of struct Student
2) Declaring Structure variables with structure definition

struct Student
{
char name[25];
int age;
char branch[10];
//F for female and M for male
char gender;
}S1, S2;

Here S1 and S2 are variables of structure Student. However, this approach is not much
recommended.

Accessing Structure Members


Structure members can be accessed and assigned values in a number of ways.

Structure members have no meaning individually without the structure.

In order to assign a value to any structure member, the member name must be linked with the
structure variable using a dot . operator also called period or member access operator.

For example:

#include<stdio.h>
#include<string.h>
struct Student //structure definition
{
char name[25];
int age;
char branch[10];
char gender[1];
};
int main()
{
struct Student s1; //structure variable declaration
[Link] = 18;
strcpy([Link], "Viraaj");
strcpy([Link], "ECE");
strcpy([Link], "M");
printf("Name of Student 1: %s\n", [Link]);
printf("Age of Student 1: %d\n", [Link]);
printf("branch of Student 1: %s\n", [Link]);
printf("gender of Student 1: %s\n", [Link]);
return 0;
}
Output:

Name of Student 1: Viraaj

Age of Student 1: 18

branch of Student 1: ECE

gender of Student 1: M

Structure Initialization:
Like a variable of any other datatype, structure variable can also be initialized at compile time.

struct Patient
{
float height;
int weight;
int age;
};
struct Patient p1 = { 180.75 , 73, 23 }; //initialization

OR

struct Patient p1;

[Link] = 180.75; //initialization of each member separately

[Link] = 73;

[Link] = 23;

Array of Structure:
We can also declare an array of structure variables. in which each element of the array will represent
a structure variable.

Example : struct employee emp[5];

The below program defines an array emp of size 5. Each element of the array emp is of type
Employee.
#include<stdio.h>
struct Employee
{
char ename[10];
int sal;
};
struct Employee emp[5];
int i, j;
void enter_empdetail()
{
for(i = 0; i < 3; i++)
{
printf("\nEnter %dst Employee record:\n", i+1);
printf("\nEmployee name:\t");
scanf("%s", emp[i].ename);
printf("\nEnter Salary:\t");
scanf("%d", &emp[i].sal);
}
printf("\nDisplaying Employee record:\n");
for(i = 0; i < 3; i++)
{
printf("\nEmployee name is %s", emp[i].ename);
printf("\nSalary is %d", emp[i].sal);
}
}
void main()
{
enter_empdetail();
}

Output:
Enter 1st Employee record:

Employee name: manoj

Enter Salary: 100

Enter 2st Employee record:

Employee name: mahesh

Enter Salary: 200

Enter 3st Employee record:

Employee name: satish


Enter Salary: 180

Displaying Employee record:

Employee name is manoj


Salary is 100
Employee name is mahesh
Salary is 200
Employee name is satish
Salary is 180

...Program finished with exit code 0


Press ENTER to exit console.

Common questions

Powered by AI

Structures allow for effective management of records by enabling the storage of varied data types in a single, cohesive unit, which cannot be done with basic data types that can only store one type of data. This is particularly advantageous for complex data management, such as maintaining student records with diverse information including name, age, and branch, or employee data with names and salaries .

Accessing structure members individually is not meaningful because they are part of a larger data structure that defines how they should be used together to represent a cohesive entity. In C, structure members must be accessed using a specific structure variable and the dot operator (.) to link the member with its structure variable. This ensures that the correct context and variable relationships are maintained, as demonstrated with s1.age or s1.name in a student record .

An array of structures in C is declared by defining the structure type followed by the array variable, such as struct Employee emp[5];. This allows each array element to represent a separate structure instance. Practically, this can be used to manage large datasets of structured records, such as processing employee details in bulk, allowing for efficient indexing and iteration over multiple records .

Directly declaring structure variables alongside definitions may not be recommended because it can reduce code readability, complicate code maintenance, and lead to confusion in larger projects where clear separation of type definitions and variable declarations is preferred. Having defined types separately from the variables that utilize them makes the code more modular and scalable .

A structure significantly simplifies data management in scenarios like managing student enrollment details in a university database. Suppose each student has associated data like name, ID, age, course, and grade. Individually declaring separate variables for each data point would lead to cumbersome declaration and potential errors during access and modification. By using a struct Student that encapsulates all these related fields, the program becomes much more organized, reducing complexity and aligning data use with logical groupings, making it easier to manage and extend .

The 'struct' keyword in C defines a user-defined data type that enables the grouping of different data types into a single entity. This enhances data handling capabilities by allowing complex data models to be built with minimal use of memory and by enabling easy data organization, retrieval, and manipulation, illustrated by struct Student for holding student information .

Structure variables in C can be initialized at compile time by listing values within braces, or they can be initialized one member at a time. For example, a struct Patient can be initialized using the syntax struct Patient p1 = {180.75, 73, 23}; for compile-time initialization, or each member can be initialized separately by assigning values for each member variable post-declaration .

The main difference between a structure and an array in C programming is that an array holds data of a similar type only, whereas a structure can store data of different types together. This makes structures more practical in scenarios where heterogeneous data is involved, such as storing student information, which may include a variety of data types like strings for names and integers for age .

Using structure member variables during initialization can lead to clearer, more organized code, and helps prevent errors by setting initial values when the structure variable is created. This makes the program more readable and ensures that no uninitialized memory is accessed, which can save debugging time and prevent undefined behaviour .

The process of accessing and outputting structure member values using the dot operator ensures that each data point within the structure is handled in the context of its parent structure, thus promoting data encapsulation. It keeps data integrity intact by allowing operations on data only via the defined structure instance, providing a layer of abstraction that prevents accidental modifications to other unrelated data, as demonstrated in the example where student details are accessed individually via s1.name, s1.age, etc. .

You might also like