Structure Union Enum Typedef
Structure Union Enum Typedef
Declaring a Structure
Before declararion structure, first we have to design the structure.
Syntax:
struct Structurename
{
Member1;
Member 2;
Member 3;
};
Here structurename represents the name of structure given by the user and member
represent the information that we want to store in this structure.
struct Student
{
char name[20];
int rollno;
float marks;
}
struct structname
{
Member1;
Member 2;
Member 3;
}
struct structname Structurevariablename;
struct Student
{
char name[20];
int rollno;
float marks;
};
//initialization of structure
struct Student
{
char name[50];
int rollno;
float marks;
};
struct Student s1= { "Manas",13, 54.5 } ;
struct Student s2= { "Karan",18, 90 } ;
struct Student s3= { 0 } ;
//runtime initialization:
It will run with the help of scanf().
Sanjay 30 5500.500000
Sanjay 30 5500.500000
Sanjay 30 5500.500000
Nested structure:
When the member of a structure is a structure itself, then it is called nested structure.
output...
Let us C YPK 101
//Structure Pointer
How to access elements when structure variable is a pointer
The way we can have a pointer pointing to an int, or a pointer pointing to a char, similarly we
can have a pointer pointing to a struct. Such pointers are known as ‘structure pointers’.
# include <stdio.h>
int main( )
{
struct book
{
char name[ 25 ] ;
char author[ 25 ] ;
int callno ;
};
struct book b1 = { "Let us C", "YPK", 101 } ;
struct book *ptr ;
ptr = &b1 ;
printf ( "%s %s %d\n", b1.name, b1.author, b1.callno ) ;
printf ( "%s %s %d\n", ptr->name, ptr->author, ptr->callno ) ;
return 0 ;
}
#include<stdio.h>
struct employee
{
int ecode;
char ename[20];
char designation[20];
int salary;
char dept[20];
char citypost[20];
}emp[100];
int main()
{
int i;
for(i=0;i<100;i++)
{
printf("Employee Code: ");
scanf("%d",&emp[i].ecode);
printf("Name : ");
scanf("%s",emp[i].ename);
printf("Designation : ");
scanf("%s",emp[i].designation);
printf("Salary : ");
scanf("%d",&emp[i].salary);
printf("Department : ");
scanf("%s",emp[i].dept);
printf("City of posting : ");
scanf("%s",emp[i].citypost);
printf("-----------------------------\n");
}
for(i=0;i<100;i++)
{
if(emp[i].salary>20000)
printf("%s\n",emp[i].ename);
}
return 0;
}
/*Define structure with syntax .Also write a program that compares two given dates. To
store date use structure say date that contains three members namely date, month and
year. If the dates are equal then display message as "Equal" otherwise "Unequal". */
#include<conio.h>
struct dmy
{
int date;
int month;
int year;
};
int main()
{
struct dmy a, b;
int flag;
printf("\nEnter the first date (dd mm yyyy) : ");
scanf("%d%d%d", &a.date, &a.month, &a.year);
printf("\nEnter the second date (dd mm yyyy) : ");
scanf("%d%d%d", &b.date, &b.month, &b.year);
#include<stdio.h>
struct employee
{
int id,age,salary;
char name[25];
}emp[100];
void main()
{
int i,n;
printf("Enter the no of employees\n");
scanf("%d",&n);
printf("Enter employee info as id , name , age , salary\n");
for(i=0;i<n;i++)
{
printf("\nenter %d employee detail",i+1);
scanf("%d %s %d %d",&emp[i].id,emp[i].name,&emp[i].age,&emp[i].salary);
}
printf("\nEMP_NAME\tEMP_NAME\tEMP_AGE\t\tEMP_SAL\n");
for(i=0;i<n;i++)
{
printf("%d\t\t%s\t\t%d\t\t%d\n",emp[i].id,emp[i].name,emp[i].age,emp[i].salary);
}
}
/*Write a program in C using structure to enter and print the record of 100 books
available in your library. Following fields may be included in the record: -
book_title, book_price and number_of_pages. */
# include <stdio.h>
void linkfloat( ) ;
int main( )
{struct book
{
char book_title ;
float book_price ;
int number_of_pages ;
};
struct book b[ 100 ] ;
int i ;
for ( i = 0 ; i <= 99 ; i++ )
{
printf ( "Enter name, price and pages " ) ;
fflush ( stdin ) ;
scanf ( "%c %f %d", &b[ i ].book_title, &b[ i ].book_price, &b[ i ].number_of_pages ) ;
}
for ( i = 0 ; i <= 99 ; i++ )
printf ( "%c %f %d\n", b[ i ].book_title, b[ i ].book_price, b[ i ].number_of_pages ) ;
return 0 ;
}
void linkfloat( )
{
float a = 0, *b ;
b = &a ; /* cause emulator to be linked */
a = *b ; /* suppress the warning - variable not used */
}
/*Write a program in C to create a database of fifty students to store personal details such
as roll no, name and marks. Display the details of those students who secured marks
greater than 80.*/
#include <stdio.h>
struct student
{
char firstName[50];
int roll;
float marks;
} s[50];
int main() {
int i;
printf("Enter information of students:\n");
// storing information
for (i = 0; i < 50; ++i)
{
s[i].roll = i + 1;
printf("\nFor roll number%d,\n", s[i].roll);
printf("Enter first name: ");
scanf("%s", s[i].firstName);
printf("Enter marks: ");
scanf("%f", &s[i].marks);
}
printf("Displaying Information:\n\n");
// displaying information
for (i = 0; i < 50; ++i)
{
if(s[i].marks> 80)
{
printf("\nRoll number: %d\n", i + 1);
printf("First name: ");
puts(s[i].firstName);
printf("Marks: %.1f", s[i].marks);
printf("\n");
}
}
return 0;
}
Other programs
1. Write a program to create a record of student that consist student name, rollno and
marks of a subject and age.
2. Write a program to create database of an employee (name,income) of an
organization. Print the name of an employee if his salary is greater than 20000
3. Write a program to create a record of 5 student that consist student name, rollno
and marks of a subject and age.
Array of structure
1. Write a program to create database of 20 students that include name, rollno, marks
in subject and display the detail and sum.
2. Write a program to create database of 20 students that include name, rollno, marks
in subject and display the detail of student to secured the higher marks..
3. Write a program to create database of Employees of an organization to print the
details of employees whose age is greater than 30 years and salary is greater than
15000
Union
# include <stdio.h>
int main( )
{
union a
{
short int i ;
char ch[ 2 ] ;
};
union a key ;
key.i = 512 ;
printf ( "key.i = %d\n", key.i ) ;
printf ( "key.ch[ 0 ] = %d\n", key.ch[ 0 ] ) ;
printf ( "key.ch[ 1 ] = %d\n", key.ch[ 1 ] ) ;
return 0 ;
}
output
key.i = 512
key.ch[ 0 ] = 0
key.ch[ 1 ] = 2
union a
{
short int i ;
char ch[ 2 ] ;
};
union a key ;
//write a program to create record of student using union
#include<stdio.h>
union student
{
char name[20];
int rollno;
float marks;
} u;
void main()
{
printf("enter name");
gets(u.name);
puts(u.name);
scanf("%d",u.rollno);
printf("%d",u.rollno);
scanf("%f",&u.marks);
printf("%f",u.marks);
}
The struct keyword is used to define it. Union keyword is used to define it.
Sizeof struct will be equal to the sum of Size of union will be equal to the sizeof
sizeof each member. the largest member.
Each member has his unique memory All members has a common memory
space. A separate memory location is space. Only one member is active at a
assigned time . Single memory is allotted to all.
Change in one does not affect another. Change in one affects other entities.
All the members are active More memory efficient.
simultaneously. Less memory efficient.
Typedef
The first part declares the data type and specifies its possible values. These values are
called ‘enumerators’..
Internally, the compiler treats the enumerators as integers. Each value on the list of
permissible values corresponds to an integer, starting with 0. Thus, in our example,
single is stored as 0, married is stored as 1, divorced as 2 and widowed as 3.
//program on enum
# include <stdio.h>
# include <string.h>
int main( )
{
enum emp_dept
{
assembly, manufacturing, accounts, stores
};
struct employee
{
char name[ 30 ] ;
int age ;
float bs ;
enum emp_dept department ;
};
struct employee e ;
strcpy ( e.name, "Lothar Mattheus" ) ;
e.age = 28 ;
e.bs = 5575.50 ;
e.department = manufacturing ;
printf ( "Name = %s\n", e.name ) ;
printf ( "Age = %d\n", e.age ) ;
printf ( "Basic salary = %f\n", e.bs ) ;
printf ( "Dept = %d\n", e.department ) ;
if ( e.department == accounts )
printf ( "%s is an accounant\n", e.name ) ;
else
printf ( "%s is not an accounant\n", e.name ) ;
return 0 ;
}