Presentation is loading. Please wait.

Presentation is loading. Please wait.

21 August 2015 1 (B.V.Patel institute of BMC & IT) STRUCTURE AND UNIONS.

Similar presentations


Presentation on theme: "21 August 2015 1 (B.V.Patel institute of BMC & IT) STRUCTURE AND UNIONS."— Presentation transcript:

1 21 August 2015 1 (B.V.Patel institute of BMC & IT) STRUCTURE AND UNIONS

2 21 August 2015 2 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)

3  Struct book_bank  {  char title [20];  char author[15];  int pages;  float price;  } book_bank book1;

4  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);

5  Compile time main() { struct student { char name[20]; int marks; }stud; stud. marks={50}; …………… }

6  Rum time Scanf (“%d \n ”,&stud. marks);

7  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); }

8  If(stud1.number == 111) { float sum=stud1.marks+stud2.marks; stud2.marks *=0.5; }

9  Struct marks { int sub1; int sub2; int sub3; }marks student main() { Struct marks student[3]={{45,34,61}, {23,55,67},{44,66,55}}; }

10  student[0].subject1=45; student[0].subject2=34; ………………… student[2].subject3= 55 ;

11  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.

12  Struct salary { char name; char dept; Struct { int houserent; int da; } allowance; } Employee; Now for refer member we can use employee.allowance.houserent

13  Union item { int m; float x; char c; }code; code.m=379;

14  It is like structure but main difference is in term of storage 1000 1001 1002 1003 C M X

15  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 2015 15

16 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 2015 16

17 21 August 2015 17  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 */ }

18  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 2015 18

19  #define PI 3.1415  The above statement is called macro definition or macro. During preprocessing the processor replaces every occurrence of PI in program with 3.1415. It is also called macro templates whereas 3.1415 are called macro expansions 21 August 2015 19

20  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 2015 20

21  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 2015 21

22  There are two more preprocessor directives available, though they are not very commonly used. They are:   #undef  #pragma 21 August 2015 22

23 23


Download ppt "21 August 2015 1 (B.V.Patel institute of BMC & IT) STRUCTURE AND UNIONS."

Similar presentations


Ads by Google