Structure in C
1. What is Structure in C ?
Ans: A Structure is a user-defined data type that allows you to group together variables of different data types under a single name, creating a composite data type to represent complex entities.
Syntax : struct [Structure_name]
{
Datatype Var1;
Datatype Var2;
Datatype Var3;
};
Simple Explanation :
In this image we can see that A group of Employees have given a task that they need to collect the data of all the employees in the company using c. The company contains 500 employees. It is not easy to take a single variable for a single employees. The structure in c is a container for all type of datatype. It contains various datatypes under a single variable. Which will make the employs work more easy.
1. Deceleration of structure.
We can declare the structure along with structure definition or separately.
ex:
struct Student { // Structure declaration
char name[50]; // Member (char array for name)
int rollNumber; // Member (integer for roll number)
float marks; // Member (float for marks)
};
int main() {
struct Student s1; // Declare a structure variable
return 0;
}
2. Initialization of structure :
struct Student { // Structure declaration
char name[50]; // Member (char array for name)
int rollNumber; // Member (integer for roll number)
float marks; // Member (float for marks)
};
int main() {
struct Student s1; // Declare a structure variable
// Accessing members
strcpy(s1.name, "Alice");
s1.rollNumber = 101;
s1.marks = 85.5;
return 0;
}
Note: The Dot used in s1.rollnumber is used to operate the member of structure Which is also known as structure member operator.
1. WAP a program to store a value of employes.
0 Comments