Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
218 views

Computer Programming-Unit 5

Structures allow grouping of different data types together under one name. Unions share memory between members but structures allocate separate memory for each member. The preprocessor directives like #include, #define are used to include header files and define macros. Structures are used to create a mark sheet program by defining a student structure with name, register number, average and grade members and reading details of multiple students into an array of structures and printing the results.

Uploaded by

jaba123jaba
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
218 views

Computer Programming-Unit 5

Structures allow grouping of different data types together under one name. Unions share memory between members but structures allocate separate memory for each member. The preprocessor directives like #include, #define are used to include header files and define macros. Structures are used to create a mark sheet program by defining a student structure with name, register number, average and grade members and reading details of multiple students into an array of structures and printing the results.

Uploaded by

jaba123jaba
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

UNIT 5  Structures and Unions

Part – A (2mark questions) 5. What are the differences between a structure and
an union?
1. Define structure. Structure Union
 It is a user define data type. Memory space allocated Memory space allocated for
 It contains variables of different data type. for all members the highest size member
 Variables inside the structure = structure members Each member has own All members share same
 We can store int, float, char, etc in a struct memory space memory space
 Memory space is created for all members It is used when all It is used when members
 Struct keyword is used. members are are not accessed at same
 Ex:- independently used time
struct student Members can be accessed Members cannot be
{ at the same time accessed at the same time
int roll; 6. What are the different types of storage classes in
char name[10]; C?
float marks; (or)
} What are the different types of variable scope?
2. What is the purpose of structures? Storage class Keyword
(or) Automatic variables auto
What is the use of structured data type? Static variables static
External variables extern
 In arrays, same data type elements are stored. Register variables register
 In struct, Different data type elements can be stored.
 Difficult data types can be handled 7. Define preprocessor in C.
 We can create pointers to the structure  It is a program that converts C program with pre-
 User can define their own data type. processor directives to the modified version
 That modified version can be converted to object
3. Mention the differences between structure and
code by the compiler
arrays
 It gives three facilities:-
Structure Array
o Include header files
Contains different data Contains same data type
o Macro expansion
type elements elements
o Conditional compilation
Dynamic memory Static memory allocation
allocation C Program Modified version
Preprocessor
(.) dot operator is used to Array index is used to Of C program
access structure members access array members
It is not a pointer It is a base pointer
compiler
4. Define Union in C.
 It is a user define data type
 It contains variables of different data type Object code
 Variables inside the union = union members 8. Write a C program to use macro expansion
 We can store int, float, char, etc in an union #include<stdio.h>
 Memory space is not created for all members. #include<conio.h>
 Memory space is created for Highest Byte size #define area(r) (3.14* (r) * (r) )
member void main()
{
 That memory will be shared by all members
clrscr();
 Union keyword is used int r = 2;
Ex:- printf(“%f”, area(r) );
union student getch();
{ O/p:-
}
int roll; 12.56
char name[10];
float marks;
}

“My failure created fear to me; My failure after a success created fear to my opponents” – Sachin Tendulkar
9. What is the use of #define pre-processor? struct student
 It is used to define a macro {
 The substitution value should be mentioned char name[20];
 The macro will be substituted by its value char reg[20];
 Ex:- int avg;
char grade;
#include<stdio.h>
} s[50], *p;
#include<conio.h> printf(“enter number of students”);
#define a 10 scanf(“%d”,&n);
O/p:-
void main() for(i = 0; i < n; i + + )
{ 10 {
printf(“%d”,a); printf(“enter name”);
getch(); scanf(“%s”,s[i].name);
} printf(“enter register number”);
10. Define static storage class. scanf(“%s”,s[i].reg);
 To declare variables statically printf(“enter average”);
scanf(“%d”,s[i].avg);
 Static variables are stored in main memory
}
 It can be used locally or globally p = s;
 Static variables are automatically initialized to 0 for(p = s; p < s +n; p ++)
 Ex:- {
#include<stdio.h> if(p - > avg < 50) p - > grade = ‘u’;
#include<conio.h> else if(p - > avg < 55) p - > grade = ‘e’;
void main() else if(p - > avg < 60) p - > grade = ‘d’;
{ O/p:- else if(p - > avg < 70) p - > grade = ‘c’;
847(garbage value) else if(p - > avg < 80) p - > grade = ‘b’;
int a;
static int b;
0 else if(p - > avg < 90) p - > grade = ‘a’;
else p - > grade = ‘s’;
printf(“%d”,a);
}
printf(“%d”,b); for(p = s; p < s +n; p ++)
getch(); {
} printf(“%s”, p - > name);
printf(“%s”, p - > reg);
Part-B ( 8 and 16 marks) printf(“%d”, p - > avg);
printf(“%c”, p - > grade);
1. What is a structure? Write a C program to create a }
mark sheet for students using structure. getch();
 It is a user define data type. }
 It contains variables of different data type. O/p:-
 Variables inside the structure = structure members Enter number of students
 We can store int, float, char, etc in a struct 2
 Memory space is created for all members Enter name
Sachin
 Struct keyword is used. Enter register number
 Ex:- 001
struct student Enter average
{ 91
int roll;
char name[10]; Enter name
float marks; Tendulkar
} Enter register number
002
Program:- Enter average
#include<stdio.h> 82
#include<conio.h>
void main() Sachin 001 91 S
{ Tendulkar 002 82 A
int i, n;

“My failure created fear to me; My failure after a success created fear to my opponents” – Sachin Tendulkar
2. What is a structure? Write a C program to store and 3. What are the storage classes in C? Explain them in
display employee details using structure. detail with example.
 It is a user define data type. Storage class Keyword
 It contains variables of different data type. Automatic variables auto
 Variables inside the structure = structure members Static variables static
 We can store int, float, char, etc in a struct External variables extern
 Memory space is created for all members Register variables register
 Struct keyword is used. Automatic variables:-
 Ex:-  Ordinary variables
struct employee O/p:-  Defined inside a function or a block
{ Enter number of employees  Default storage class
char name[10]; 2  “auto” keyword is used
char id[10];  Its lifetime is, within that function or block
Enter name
float salary;  After execution, they are erased from memory.
Rahul
} Enter ID  Ex:-
Program:- 001 #include<stdio.h>
#include<stdio.h> Enter salary #include<conio.h>
#include<conio.h> 39000 void f1();
void main() void f2();
{ Enter name void main()
Dravid {
int i, n;
Enter ID
struct employee clrscr();
002
{ Enter salary int a = 10; O/p:-
char name[10]; 51000 printf(“%d”, a);
char id[10]; f1(); 10
float salary; Rahul 001 39000 associate f2();
20
char post[10]; Dravid 002 51000 chairman getch();
} s[50], *p; } 30
for(i = 0; i < n; i++) void f1()
{ {
printf(“enter name”); int a = 20;
scanf(“%s”,s[i].name); printf(“%d”,a);
printf(“enter ID”); }
scanf(“%s”,s[i].id);
void f2()
printf(“enter salary”);
{
scanf(“%f”,s[i].salary);
} int a = 30;
p = s; printf(“%d”,a);
for(p = s; p < s +n; p ++) }
{ Static variables:-
if (p - > salary < 10000) p - > post=”trainee”;  To declare variables statically
else if(p - > salary <20000) p -> post=”executive”;  Static variables are stored in main memory
else if(p - > salary <30000) p -> post=”associate”;  It can be used locally or globally
else if(p - > salary <40000) p -> post=”director”;  “static” keyword is used
else if(p - > salary <50000) p -> post=”manager”;
 Static variables are automatically initialized to 0
else p -> post=”chairman”;
}  Ex:-
for(p = s; p < s +n; p ++) #include<stdio.h>
{ #include<conio.h>
printf(“%s”, p - > name); void main()
printf(“%s”, p - > id); { O/p:-
printf(“%d”, p - > salary); int a; 847(garbage value)
printf(“%c”, p - > post); static int b; 0
} printf(“ %d ” ,a);
getch(); printf(“ %d ” ,b);
} getch();
}

“My failure created fear to me; My failure after a success created fear to my opponents” – Sachin Tendulkar
External variables:- Include header files:-
 They are like global variables  They are used to include header files
 They can be used anywhere in the function  Program will be linked with that header file
 They are declared outside the functions  Ex:- printf() and scanf() are present in “stdio.h”
 They are declared before main() function #include<stdio.h> Printf() and scanf()
 “extern” keyword is used #include<conio.h> Getch(), gets(), puts(), …
 Ex:- #include<string.h> Strlen(), strrev(), strcmp(), …
#include<stdio.h> #include<math.h> Sin(), cos(), sqrt(), …
#include<conio.h>
int n = 10; O/p:- Macro expansion:-
void main() 10  It is defined by #define pre-processor directive.
{  Macro contains an item with its associated text.
clrscr();  In the program,macro will be substituted by its value
printf(“%d”, n);
 This is called as Macro substitution
getch();
 It is done before compilation of program.
}
#define To define a macro
Register variables:-
#undef To undefined a macro
 They are stored in registers
Example:-
 They are not stored in main memory
#include<stdio.h>
 “Register” keyword is used
#include<conio.h>
 For the purpose of fast access #define area(r) (3.14 * (r) * (r) )
 More variables cannot be stored in register. void main()
 Ex:- { O/p:-
#include<stdio.h> clrscr();
#include<conio.h> int r = 2;
12.56
O/p:-
void main() printf(“area = %f”, area(r) );
0
{ getch();
1
clrscr(); }
2
register int a = 1; 3 Conditional compilation:-
for(a=0;a<5;a++) 4 #ifdef If macro is defined
{ #ifndef If macro is not defined
printf(“%d”,a); #endif To end the macro “if”
} #if “if” conditional compilation
getch(); #elif “else if”
} #else “else”
4. What are pre-processor directives? Explain any
four directives with example. “#ifdef” and “#ifndef” #if...#elif...#else
 It is a program that converts C program with pre- #include<stdio.h> #include<stdio.h>
processor directives to the modified version #include<conio.h> #include<conio.h>
 That modified version can be converted to object void main() void main()
code by the compiler { {
 It gives three facilities:- clrscr(); clrscr();
o Include header files #define A 10 #define A 10
o Macro expansion #ifdef A #if A<0
o Conditional compilation printf(“defined”); printf(“negative”);
#endif #elif A>0
C Program Preprocessor Modified version printf(“positive”);
Of C program #undef A #else
#ifndef A printf(“Zero”);
compiler printf(“undefined”); #endif
#endif O/p:- getch(); O/p:-
getch(); Defined } positive
Object code } undefined

“My failure created fear to me; My failure after a success created fear to my opponents” – Sachin Tendulkar

You might also like