structure in c
DESCRIPTION
Structure in c S-TeacherTRANSCRIPT
![Page 1: Structure in c](https://reader035.vdocuments.mx/reader035/viewer/2022081907/548b68d5b479596c438b4ad1/html5/thumbnails/1.jpg)
StructureStructures are user defined data typesIt is a collection of heterogeneous dataIt can have integer, float, double or character
data in itWe can also have array of structures
struct <<structname>>{
members;}element;
We can access element.members;
![Page 2: Structure in c](https://reader035.vdocuments.mx/reader035/viewer/2022081907/548b68d5b479596c438b4ad1/html5/thumbnails/2.jpg)
Example
struct Person{
int id;char name[5];
}P1;P1.id = 1;P1.name = “vasu”;
![Page 3: Structure in c](https://reader035.vdocuments.mx/reader035/viewer/2022081907/548b68d5b479596c438b4ad1/html5/thumbnails/3.jpg)
typedef statementUser Defined Data Types The C language provides a facility called typedef
for creating synonyms for previously defined data type names. For example, the declaration:
typedef int Length;
makes the name Length a synonym (or alias) for the data type int.
![Page 4: Structure in c](https://reader035.vdocuments.mx/reader035/viewer/2022081907/548b68d5b479596c438b4ad1/html5/thumbnails/4.jpg)
typedef(contd.)The data “type” name Length can now be
used in declarations in exactly the same way that the data type int can be used:
Length a, b, len ; Length numbers[10] ;
![Page 5: Structure in c](https://reader035.vdocuments.mx/reader035/viewer/2022081907/548b68d5b479596c438b4ad1/html5/thumbnails/5.jpg)
UNION
![Page 6: Structure in c](https://reader035.vdocuments.mx/reader035/viewer/2022081907/548b68d5b479596c438b4ad1/html5/thumbnails/6.jpg)
UNIONUnion has members of different data types,
but can hold data of only one member at a time.
The different members share the same memory location.
The total memory allocated to the union is equal to the maximum size of the member.
![Page 7: Structure in c](https://reader035.vdocuments.mx/reader035/viewer/2022081907/548b68d5b479596c438b4ad1/html5/thumbnails/7.jpg)
EXAMPLE#include <stdio.h>
union marks{ float percent; char grade;};int main ( ){ union marks student1; student1.percent = 98.5; printf( "Marks are %f address is %16lu\n", student1.percent, &student1.percent); student1.grade = 'A'; printf( "Grade is %c address is %16lu\n", student1.grade, &student1.grade);}
![Page 8: Structure in c](https://reader035.vdocuments.mx/reader035/viewer/2022081907/548b68d5b479596c438b4ad1/html5/thumbnails/8.jpg)
ENUM
![Page 9: Structure in c](https://reader035.vdocuments.mx/reader035/viewer/2022081907/548b68d5b479596c438b4ad1/html5/thumbnails/9.jpg)
ENUMERATED DATATYPE Enumeration is a user-defined data type.
It is defined using the keyword enum and the syntax is:
enum tag_name {name_0, …, name_n} ;
The tag_name is not used directly. The names in the braces are symbolic constants that take on integer values from zero through n.
![Page 10: Structure in c](https://reader035.vdocuments.mx/reader035/viewer/2022081907/548b68d5b479596c438b4ad1/html5/thumbnails/10.jpg)
Enumerated(contd.)
As an example, the statement: enum colors { red, yellow, green } ; creates three constants. red is assigned the value 0, yellow is assigned 1 and green is assigned 2.
![Page 11: Structure in c](https://reader035.vdocuments.mx/reader035/viewer/2022081907/548b68d5b479596c438b4ad1/html5/thumbnails/11.jpg)