Chapter – 2. STRUCTURES

2006 Delhi:

1.b. Illustrate the use of #define in C++ to define a macro. 2

Ans: The #define preprocessor can be used in the creation of macros (code substitution). Eg: #define SQUARE(x) x*x Before compilation, if the C++ preprocessor finds SQUARE(x),

where x is any value in the source code, it replaces it with its square (ie x*x). Here a macro substitutes text only; It does not check for data types.

1.C. Rewrite the following program after removing the syntactical error(s), if any.Underline each correction. 2

#include<iostream.h>
void main( )
{
struct STUDENT
{
char stu_name[20];
char stu_sex;
int stu_age=17;
}
student; gets(stu_name);
gets(stu_sex);
}

Ans:

#include<iostream.h>
#include<stdio.h>
void main( )
{
struct STUDENT
{
char stu_name[20];
char stu_sex;
int stu_age; //Initialization of variables inside a structure is not allowed.
}student; gets(student.stu_name);
cin>>student.stu_sex); //A single character cannot be read using gets
}

1.f. What are Nested Structures? Give an example. 2

Ans: Nested structures are structures as member of another structure. For example, the date of birth is astructure within the structure of a student as shown below. These types of structures are known as nested structures.