C Structure Example in easy steps
What is a structure in C Programming?
In C Structure Example,let’s start with defination of Structure.Structure is a user-defined data type which can hold the datatypes of different kind.Typically,array is a collection of similar kinds of variables.In contrast to array,structure can hold the multiple data types.In general programming,programmers should interact with different types of data in their life.So,it is very hard to keep records by using the arrays only.In stead of that, a structure is defined.In C Structure Example,Structure defination depends upon the types of data we use in keeping records.For example: a company wants to keep records its employees with individual id,name,salary and so on.To cope up with these types of datatypes.First,a structure is defined by using the struct keyword.In C Structure Example,Struct keyword is responsible for making new datatype by using the primitive datatypes like int,float or char.After all,a new user-defined datatypes is created which is capable of storing the records according to its needs.Here is a sample code of user defined datatypes:
struct emp { int id; char name[20]; float sal; };
In this piece of code,emp is a new user defined datatypes with the help of struct keyword.According to the rule,our new user-defined datatype consists of three piece of primitive datatypes which are int,char and float in C Structure Example
A simple code-C Structure Example
Once the datatype is created in C Structure Example,a new variable can be declared.In this particular example,’e’ is a structure variable.This variable reserve 26 bytes in memory.4,2 and 20 bytes for float,int and string variables respectively.When user enters the respective values,then every value is assigned to there respective position which is governed by the following line of code:
printf("\nEnter the id,name and salary\n"); scanf("%d%s%f",&e.id,e.name,&e.sal);
Finally,the stored values in memory can be accessed and we can print the result in C Structure Example.
[c] #include#include
struct emp
{
int id;
char name[20];
float sal;
};
void main()
{
struct emp e; //single variable of structure for single employee
clrscr();
printf(“\nEnter the id,name and salary\n”);
scanf(“%d%s%f”,&e.id,e.name,&e.sal);
printf(“\nId\tName\tSalary\n”);
printf(“%d\t%s\t%.2f”,e.id,e.name,e.sal);
getch();
}
[/c]
Output-C Structure Example
Here is the output in C Structure Example:
Click Here For Resources
Click Here For Resource
Click Here For Another Resources