Date post: | 13-Dec-2014 |
Category: |
Education |
Upload: | prabhu-govind |
View: | 154 times |
Download: | 0 times |
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;
Example
struct Person{
int id;char name[5];
}P1;P1.id = 1;P1.name = “vasu”;
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.
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] ;
UNION
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.
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);}
ENUM
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.
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.