21 August (B.V.Patel institute of BMC & IT) STRUCTURE AND UNIONS
21 August Defining a structure Struct book_bank { char title [20]; char author[15]; int pages; float price; }; A structure is convenient tool for handling a group of logically related different data items. for ex.customer(address,id,name)
Struct book_bank { char title [20]; char author[15]; int pages; float price; } book_bank book1;
The link between member and variable is establish using dot operator For ex.book1.pages=250; Another method for ex. Strcpy(book1.author,”balagurusamy”); Another method we cal also scanf to values scanf(“%d\n”,&book1.pages);
Compile time main() { struct student { char name[20]; int marks; }stud; stud. marks={50}; …………… }
Rum time Scanf (“%d \n ”,&stud. marks);
Structure can be initialize within main or outside the main main() { struct student { char name[20]; int marks; }stud; stud. marks={50}; …………… } struct student { char name[20]; int marks; }stud; Main() { stud. marks={50); }
If(stud1.number == 111) { float sum=stud1.marks+stud2.marks; stud2.marks *=0.5; }
Struct marks { int sub1; int sub2; int sub3; }marks student main() { Struct marks student[3]={{45,34,61}, {23,55,67},{44,66,55}}; }
student[0].subject1=45; student[0].subject2=34; ………………… student[2].subject3= 55 ;
Struct marks { int number; float subject[3]; }student[2]; here the member subject contains three elements subejct[0], subejct[1], subejct[2]. these elements can be accessed using appropriate subscript like student[1].subject[2] refer marks obtain in third subject by second student.
Struct salary { char name; char dept; Struct { int houserent; int da; } allowance; } Employee; Now for refer member we can use employee.allowance.houserent
Union item { int m; float x; char c; }code; code.m=379;
It is like structure but main difference is in term of storage C M X
ENUM is closely related to the #define preprocessor.#define It allows you to define a list of aliases which represent integer numbers. For example if you find yourself coding something like: #define MON 1 #define TUE 2 #define WED 3 You could use enum as below. enum week { Mon=1, Tue, Wed, Thu, Fri Sat, Sun} days; or enum boolean { FALSE = 0, TRUE }; 21 August
int main() { /* * Define a list of aliases */ enum months {Jan=1, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec}; enum months month; /* define 'month' variable of type 'months‘ */ printf("%d\n", month=Sep); /* Assign integer value via an alias * This will return a 9 */ } 21 August
21 August int main() { /* * Define a list of aliases */ enum days {Jan=31, Feb=28, Mar=31, Apr=30, May=31, Jun=30, Jul=31, Aug=31, Sep=30, Oct=31, Nov=30, Dec=31}; printf("%d\n", month=Feb); /* Assign integer value via an alias * This will return 28 */ }
It is a program that processes our source program before it is passed to the complier. The preprocessor offers several features called preprocessor directives. Each of these preprocessor directives begins with # symbol. The directives can be placed anywhere in the program but most often placed at the beginning of program. There are four types of preprocessor directives as follow. Macro expansion File inclusion Conditional compilation Miscellaneous directives 21 August
#define PI The above statement is called macro definition or macro. During preprocessing the processor replaces every occurrence of PI in program with It is also called macro templates whereas are called macro expansions 21 August
This preprocessor directive causes one file to be included in another. The preprocessor command for file inclusion looks like this: #include “filename” and it simply easy causes the entire contents of filename to be inserted into the source code at that point in the program 21 August
if we want, have the compiler skip over part of a source code by inserting the preprocessing commands #ifdef and #endif, which have the general form: #ifdef macroname statement 1; statement 2; statement 3; #endif If macroname has been #defined, the block will be processed as usual; otherwise not 21 August
There are two more preprocessor directives available, though they are not very commonly used. They are: #undef #pragma 21 August
23