This document discusses structures and unions in C programming. It defines structures as a way to group dissimilar data types and define a new data type. Structures use the struct keyword and allow defining custom data types. Unions are similar to structures but can only store one data element at a time, with storage size equal to the largest member. The document provides examples of declaring and defining structures and accessing structure members using dot and arrow operators. It also discusses static and extern storage classes.
5. Definition : Structure
• Group of dissimilar Datatypes
• Defines a new type
• a new kind of data type that compiler regards as a single unit
• User Defined datatype
• Uses struct keyword to create
9. Structure Variable Declaration
• Creating variable of structure type
• Two ways as structure variable
• With structure declaration
• Using structure tag
• As structure pointer
16. Unions
• Differ in memory allocation.
• In Structure its “AND” and in Unions its “XOR”
• Accessing remains the same as structures
union sample {
float avg;
int rollno;
char *name;
};
/* either a float xor an int xor a string */
17. Definition
• Like Structures Unions can stores disimilar data elements
in a group.
• But you can store only one data element at a time.
• Once a new value is assigned to any member value the
older one is erased.
• Storage size of union is the size of its largest member
21. Need???
• defines the scope (visibility)
• life time of variables
• storage classes used in a C Program
• auto
• register
• static
• extern
22. Auto Storage Class
• auto is the default storage class for all local variables.
{
int Count;
auto int Month;
}
• The example above defines two variables with the same
storage class. auto can only be used within functions, i.e.
local variables.
23. Register Storage Class
• Used to define local variables
• Stored in Register instead of RAM
• Maximum size equal to register size
• Eg. Frequently used variables like counter
{
register int Miles;
}
24. Static Storage class
• provides a lifetime over the entire program,
• declared with the keyword static
• variables are automatically initialized to zero
• static int a;
• They continue to exist even after the block in which they
are defined terminates.
• storage allocated becomes permanent for the duration of
the program.
25. Extern Storage Class
• extern is used to give a reference of a global variable that
is visible to ALL the program files.
• extern int a;