Upload
carmella-colleen-horton
View
216
Download
0
Embed Size (px)
Citation preview
April 19, 2023
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)
Declare structure variable Struct book_bank { char title [20]; char author[15]; int pages; float price; } book_bank book1;
Accessing structure members 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);
Structure member initialization Compile time main() { struct student { char name[20]; int marks; }stud; stud. marks={50}; …………… }
method of initialize structure 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);
}
Operations on individual member
If(stud1.number == 111) { float
sum=stud1.marks+stud2.marks; stud2.marks *=0.5; }
Array of structure Struct marks { int sub1; int sub2; int sub3; }marks student main() { Struct marks
student[3]={{45,34,61}, {23,55,67},{44,66,55}}; }
Array initializes members like
student[0].subject1=45; student[0].subject2=34; ………………… student[2].subject3= 55 ;
Arrays within structure 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.
Structure within structure Struct salary { char name; char dept; Struct { int houserent; int da; } allowance;}Employee; Now for refer member we can use
employee.allowance.houserent
ENUM is closely related to the #define preprocessor.
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 };
April 19, 2023 15
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 */ }
April 19, 2023 16
April 19, 2023 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 */
}
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
April 19, 2023 18
#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
April 19, 2023 19
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
April 19, 2023 20
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 macronamestatement 1;statement 2;statement 3;
#endif If macroname has been #defined, the
block will be processed as usual; otherwise not
April 19, 2023 21
There are two more preprocessor directives available, though they are not very commonly used. They are:
#undef #pragma
April 19, 2023 22