Chapter 9 Structures and Union 1.define Structure
Chapter 9 Structures and Union 1.define Structure
1.Define structure.
A structure can be considered as a template used for defining a collection of variables
under a single name. Structures help a programmer in grouping elements of different data types
into a single logical unit. This is unlike arrays which permit a programmer to group only
elements of same data type. struct keyword is used to define a structure.
The compiler will automatically assign the values like the followings:
employee[0].employee_id = 1001;
employee[0].sex = 'M';
employee[0].age = 30;
employee[1].employee_id = 1002;
employee[1].sex = 'F';
employee[i].age = 33;
employee[2].employee_id = 1003;
employee[2].sex = 'M';
employee[2].age = 35;
employee[0].employee_id = 1001;
employee[0].sex = 'M';
employee[0].age = 30;
employee[1].employee_id = 1002;
employee[1].sex = 'F';
employee[i].age = 0;
employee[2].employee_id = 1003;
employee[2].sex = 0;
employee[2].age = 0;
For the above code, some members are not initialized. So, the compiler will set value
zero to them.
struct month {
int number_of_days;
char name[4];
};
this_month.number_of_days = 31;
strcpy( this_month.name, "Jan" );
printf("The month is %s\n", this_month.name );
Note that the array name has an extra element to hold the end of string null character.
15.Define Union.
Unions are conceptually similar to structures. A union is a variable which may hold
members of different sizes and types. The syntax of union is also similar to that of structure. The
only differences is in terms of storage. In structure each member has its own storage location,
whereas all members of union uses a single shared memory location which is equal to the size of
its largest data member.
16.Give the syntax for Union.
union union_name
{
<data-type> element 1;
<data-type> element 2;
<data-type> element 3;
}union_variable;
A union is declared using union keyword.
8.Define fopen().
The fopen() function is used to create a new file or to open an existing file. In order to
open a file, use the function fopen(). Use it as:
fp = fopen(filename, mode);
where:
filename is a string that holds the name of the file on disk (including a path like
/cs/course if necessary).
mode is a string representing how you want to open the file. Most often you'll open a file
for reading ("r") or writing ("w").
9.Define fclose().
The fclose() function is used for closing opened files. The only argument it accepts is the
file pointer.
General Syntax :
int fclose( FILE *fp );
Here fclose() function closes the file and returns zero on success, or EOF if there is an
error in closing the file. This EOF is a constant defined in the header file stdio.h.
13.Define ftell().
Function ftell() returns the current position of the file pointer in a stream. The return
value is 0 or a positive integer indicating the byte offset from the beginning of an open file. A
return value of -1 indicates an error. Prototype of this function is as shown below :
long int ftell(FILE *fp);