Structure A Structure is a collection of different

  • Slides: 35
Download presentation
Structure • A Structure is a collection of different data items, that are stored

Structure • A Structure is a collection of different data items, that are stored under a common name. • Syntax: structure_name { structure element 1; structure element 2; …………. }; Noornilo Nafees 1

 • Example: struct stud { int sno; char name[10]; int mark; }s; Noornilo

• Example: struct stud { int sno; char name[10]; int mark; }s; Noornilo Nafees 2

Example #include<stdio. h> #include<conio. h> struct stud { int regno; char name[10]; int m

Example #include<stdio. h> #include<conio. h> struct stud { int regno; char name[10]; int m 1; int m 2; int m 3; }; Noornilo Nafees 3

void main() { struct stud s; float tot, avg; printf("n. Enter the student regno,

void main() { struct stud s; float tot, avg; printf("n. Enter the student regno, name, m 1, m 2, m 3: "); scanf("%d%s%d%d%d", &s. regno, &s. name, &s. m 1, &s. m 2, &s. m 3 ); tot=s. m 1+s. m 2+s. m 3; avg=tot/3; printf("n. The student Details are: "); printf("n%dt%st%f", s. regno, s. name, tot, avg); } Noornilo Nafees 4

Output Enter the student regno, name, m 1, m 2, m 3: 100 aaa

Output Enter the student regno, name, m 1, m 2, m 3: 100 aaa 87 98 78 The student Details are: 100 aaa 263. 000000 87. 666664 Noornilo Nafees 5

Example #include<stdio. h> #include<conio. h> struct stud { int regno; char name[10], grade; int

Example #include<stdio. h> #include<conio. h> struct stud { int regno; char name[10], grade; int m 1, m 2, m 3; float avg, tot; }; void main() { struct stud s[10]; int i, n; printf("n. Enter the no. of students: "); scanf("%d", &n); Noornilo Nafees 6

for(i=0; i<n; i++) { printf("n. Enter the student regno, name, m 1, m 2,

for(i=0; i<n; i++) { printf("n. Enter the student regno, name, m 1, m 2, m 3: "); scanf("%d%s%d%d%d", &s[i]. regno, &s[i]. name, &s[i]. m 1, &s[i]. m 2, &s[i]. m 3); s[i]. tot=s[i]. m 1+s[i]. m 2+s[i]. m 3; s[i]. avg=s[i]. tot/3; if(s[i]. m 1<35||s[i]. m 2<35||s[i]. m 3<35) s[i]. grade='f'; else { if(s[i]. avg>=75) s[i]. grade='d'; else if(s[i]. avg>=60) Noornilo Nafees 7

s[i]. grade='A'; else if(s[i]. avg>=50) s[i]. grade='B'; else if(s[i]. avg>=35) s[i]. grade='C'; } }

s[i]. grade='A'; else if(s[i]. avg>=50) s[i]. grade='B'; else if(s[i]. avg>=35) s[i]. grade='C'; } } printf("n. STUDENT MARK LISTn"); printf("n. REGNOt. NAMEt. TOTALt. Avgt. GRADE"); for(i=0; i<n; i++) printf("n%dt%st%ft%c", s[i]. regno, s[i]. name, s[i]. tot, s[i]. avg, s[i]. grade); getch(); } Noornilo Nafees 8

Enter the no. of students: 2 Enter the student regno, name, m 1, m

Enter the no. of students: 2 Enter the student regno, name, m 1, m 2, m 3: 101 aaa 89 98 78 Enter the student regno, name, m 1, m 2, m 3: 102 bbb 59 68 76 STUDENT MARK LIST REGNO NAME TOTAL Avg GRADE 101 aaa 265. 000000 88. 333336 d 102 bbb 203. 000000 67. 666664 A Noornilo Nafees 9

Union • An Union is a collection of different data items, that are stored

Union • An Union is a collection of different data items, that are stored under a common name. Here same memory is shared by its members. • Syntax: union _name { union element 1; union element 2; ………………… }; Noornilo Nafees 10

 • Example: union result { int mark; float avg; char grade; }s; Noornilo

• Example: union result { int mark; float avg; char grade; }s; Noornilo Nafees 11

Example #include<stdio. h> #include<conio. h> union stud { int a; char b[2]; }; void

Example #include<stdio. h> #include<conio. h> union stud { int a; char b[2]; }; void main() { union stud c; Noornilo Nafees 12

c. a=256; printf("nc. a value is%d", c. a); } Output: c. a value is

c. a=256; printf("nc. a value is%d", c. a); } Output: c. a value is 256 Noornilo Nafees 13

Storage classes • Every C variable has a storage class, • Storage classes determines

Storage classes • Every C variable has a storage class, • Storage classes determines : – – The part of memory where storage is allocated for variable What will be the initial value of the variable if not assigned How long the storage allocation continues to exists. The scope which specifies the part of the program over which a variable name is visible • Categories of storage classes in C: – – Automatic External Static Register Noornilo Nafees 14

Automatic (auto) • By default, variables in C uses the auto storage class. •

Automatic (auto) • By default, variables in C uses the auto storage class. • The variables are automatically created when needed and deleted when they fall out of the scope. • Ex: void main() { Output auto int a=10; { 30 auto int a=20; 20 { 10 auto int a =30; printf(“%d”, a); } printf(“n%d”, a); } Noornilo Nafees 15

Automatic (auto) • Storage • Scope • Life • Default value : Memory :

Automatic (auto) • Storage • Scope • Life • Default value : Memory : Within the block in which variable is defined : Till the program control is within the block : Un predictable (Garbage Value) Noornilo Nafees 16

External (Global) -extern • Global variables are accessible from within any block & or

External (Global) -extern • Global variables are accessible from within any block & or remains in existence for the entire execution of the program. • Using global variables we can transfer information into a function without using arguments • Ex: void sum(); int a=10, b=5; void main() Output { clrscr(); Added values : 15 sum(); getch(); } void sum() { int c; c=a+b; printf(“Added values : %d”, c); } Noornilo Nafees 17

External - extern • • Storage Scope Life Default value : Memory : Global

External - extern • • Storage Scope Life Default value : Memory : Global : During entire program execution : zer 0 Noornilo Nafees 18

Static variables - static • • • Storage: Memory Scope : Local to the

Static variables - static • • • Storage: Memory Scope : Local to the block in which the variable is defined Life : Value of the variables persist between different function calls Default value : zer 0 Ex: void incr(); void main() { incr(); } void incr() { static int a=1; printf(“%dt”, a); a=a+1; } Output 123 Noornilo Nafees 19

Register variables - register • Register variables are usually stored in memory and passed

Register variables - register • Register variables are usually stored in memory and passed back & forth to the processor as needed. • Storage: CPU Registers • Scope : Local to the block in which the variable is defined • Life : Till the program control is within the block in which variable is defined. • Default value : Unpredictable(Garbage). • Ex: void main() { register int counter; } Noornilo Nafees 20

Preprocessor • It is a program that processes the source program before compilation. •

Preprocessor • It is a program that processes the source program before compilation. • It operates under the following directives – File Inclusion – Macro substitution – Conditional inclusion Noornilo Nafees 21

File Inclusion • It is used to include some file that contains functions or

File Inclusion • It is used to include some file that contains functions or some definitions. • Syntax: #include<filename> (or) #include“filename” • Eg: #include<stdio. h> #include “ex. c” Noornilo Nafees 22

Example #include<stdio. h> #include<conio. h> #include "addition. txt" void main() { int a, b;

Example #include<stdio. h> #include<conio. h> #include "addition. txt" void main() { int a, b; printf("n. Enter the numbers: "); scanf("%d%d", &a, &b); printf("The Value is %d", add(a, b)); getch(); } Noornilo Nafees 23

addition. txt int add(int a, int b) { return(a+b); } Noornilo Nafees 24

addition. txt int add(int a, int b) { return(a+b); } Noornilo Nafees 24

Output Enter the numbers: 7 4 The Value is 11 Noornilo Nafees 25

Output Enter the numbers: 7 4 The Value is 11 Noornilo Nafees 25

Example #include<stdio. h> #include<conio. h> #include "fact. c" void main() { int a; printf("n.

Example #include<stdio. h> #include<conio. h> #include "fact. c" void main() { int a; printf("n. Enter the number: "); scanf("%d", &a); printf("The factorial of %d! is %d", a, rec(a)); getch(); } Noornilo Nafees 26

Macro Substitution • It is used to define and use integer, string, or identifier

Macro Substitution • It is used to define and use integer, string, or identifier in the source program • The three forms of macros are – Simple Macro – Argumented Macro – Nested Macro Noornilo Nafees 27

Simple Macro • It is used to define some constants • Syntax # define

Simple Macro • It is used to define some constants • Syntax # define identifier string/integer • Eg: #define pi 3. 14 #define CITY “chennai” Noornilo Nafees 28

Example #include<stdio. h> #include<conio. h> #define pi 3. 14 #define CITY "chennai" void main()

Example #include<stdio. h> #include<conio. h> #define pi 3. 14 #define CITY "chennai" void main() { printf("The Value is %f", 2*pi); printf("n. The Value CITY is %s", CITY); getch(); } Output: The Value is 6. 280000 The Value CITY is chennai Noornilo Nafees 29

Argumented Macro • It is used to define some complex forms in the source

Argumented Macro • It is used to define some complex forms in the source program. • Syntax: #define identifier (v 1, v 2, …. ) string/integer • Eg: #define cube(n) (n*n*n) Noornilo Nafees 30

Example #include<stdio. h> #include<conio. h> #define cube(n) (n*n*n) void main() { printf("The Value of

Example #include<stdio. h> #include<conio. h> #define cube(n) (n*n*n) void main() { printf("The Value of 3 cube is %d", cube(3)); getch(); } Output: The Value of 3 cube is 27 Noornilo Nafees 31

Nested Macro • Here one macro is used by another macro. • Eg: #define

Nested Macro • Here one macro is used by another macro. • Eg: #define a 3 #define sq a*a Noornilo Nafees 32

Example #include<stdio. h> #include<conio. h> #define a 3 #define sq a*a void main() {

Example #include<stdio. h> #include<conio. h> #define a 3 #define sq a*a void main() { printf("The Value is %d", sq); getch(); } Output: The Value is 9 Noornilo Nafees 33

Conditional Inclusion • It is used to include some conditional statements. Noornilo Nafees 34

Conditional Inclusion • It is used to include some conditional statements. Noornilo Nafees 34

Example #include<stdio. h> #include<conio. h> #define a 3 #ifdef a #define c a+5 #endif

Example #include<stdio. h> #include<conio. h> #define a 3 #ifdef a #define c a+5 #endif void main() { printf("n. The value C is %d", c); getch(); } Output: The value C is 8 Noornilo Nafees 35