0% found this document useful (0 votes)
25 views

Program Discussed in The Class (Structure) : //example For Simple Structure

The document discusses examples of using structures in C programming. It defines an Employee structure with name, code, and salary fields. It then shows examples of declaring structure variables, initializing structures, passing structures to functions, and defining a nested structure with a birthdate sub-structure. The examples demonstrate reading and printing employee details using structures.

Uploaded by

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

Program Discussed in The Class (Structure) : //example For Simple Structure

The document discusses examples of using structures in C programming. It defines an Employee structure with name, code, and salary fields. It then shows examples of declaring structure variables, initializing structures, passing structures to functions, and defining a nested structure with a birthdate sub-structure. The examples demonstrate reading and printing employee details using structures.

Uploaded by

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

Program discussed in the class (Structure)

//Example for simple structure


#include<stdio.h>
struct Employee
{
char emp_name[20];
int emp_code;
float emp_salary;
}e1;
void main(){
struct Employee e2={"XYZ", 1000,45000.00};
struct Employee e3;
printf("Enter employee e1 details\n");
printf("Enter employee name:\n");
scanf("%s",&e1.emp_name);
printf("Enter employee code:\n");
scanf("%d",&e1.emp_code);
printf("Enter employee salary:\n");
scanf("%f",&e1.emp_salary);
e3=e1;
printf("The employee detailes are:\n\n");
//printf("Emp_name\t Emp_code\t Emp_salary\n");
printf("%s\t%d\t%f\n\n", e1.emp_name,e1.emp_code,e1.emp_salary);
printf("%s\t%d\t%f\n\n", e2.emp_name,e2.emp_code,e2.emp_salary);
printf("%s\t%d\t%f\n\n", e3.emp_name,e3.emp_code,e3.emp_salary);
}

//Example for structure using array concept


#include<stdio.h>
struct Employee
{
char emp_name[20];
int emp_code;
float emp_salary;
};
void main(){
struct Employee emp[2];
int i;
for(i=0;i<2;i++)
{
printf("Enter employee emp_%d details\n",i+1);
printf("Enter employee name:\n");
scanf("%s",&emp[i].emp_name);
printf("Enter employee code:\n");
scanf("%d",&emp[i].emp_code);
printf("Enter employee salary:\n");
scanf("%f",&emp[i].emp_salary);
}
printf("The employee detailes are:\n\n");
for(i=0;i<2;i++)
{
printf("%s\t%d\t%f\n\n", emp[i].emp_name,emp[i].emp_code,emp[i].emp_salary);
}}

//Example for structure with function


#include<stdio.h>
struct Employee
{
char emp_name[20];
int emp_code;
float emp_salary;
};
void fun_with_stru(struct Employee);
void main()
{
struct Employee e1={"XYZ", 1000,45000.00};
fun_with_stru(e1);
}
void fun_with_stru(struct Employee e)
{
printf("Employee details has given below:\n");
printf("%s\t%d\t%f\n\n", e.emp_name,e.emp_code,e.emp_salary);
}

//Example for Nested structure with function


#include<stdio.h>
struct Employee
{
char emp_name[20];
int emp_code;
float emp_salary;
struct Birth_date
{
int date;
int month;
int year;
}BD;
};
void fun_with_stru(struct Employee);
void main()
{
struct Employee e1={"XYZ", 1000,45000.00,15,07,1974};
fun_with_stru(e1);
}
void fun_with_stru(struct Employee e)
{
printf("Employee details has given below:\n");
printf("%s\t%d\t%f\t%d\t%d\t%d\n\n",
e.emp_name,e.emp_code,e.emp_salary,e.BD.date,e.BD.month,e.BD.year);
}

You might also like