C Programming and Data Structures: Unit II: C Programming – Advanced Features

Two Marks Questions with Answers

C Programming - Advanced Features | C Programming and Data Structures

C Programming and Data Structures: Unit II: C Programming – Advanced Features : Two Marks Questions with Answers

Two Marks Questions with Answers


Q.1 What is union ?

Ans. : Union is a user defined data structure which is just similar to structure. It is used to store members of different data types.

 

Q.2 What is the difference between structure and union ? AU: Dec.-18

Ans. :


 

Q.3 While handling the structure keyword struct is used.

Ans. :

1) Both the structure and union are user defined data types.

2) The structure and union are meant for storing the various member variables of different data types.

3) The dot operators are used to access the members of the union and structures.

 

Q.4 How do you access the address, where the elements of a matrix, whose index is given is stored ?

Ans : Following program shows how to access the address of particular array

#include<stdio.h>

 #include<conio.h>

void main()

{

int a[]={1,2,3,4,5},i;

int *ptr;

ptr=a;

for(i=0;i<5;i++)

{

printf("\nAddress of a[%d]=%u",i,ptr);

ptr++;

}

}

expected output will be

Address of a[0]=65516

Address of a[1]=65518

Address of a[2]=65520

Address of a[3]=65522

Address of a[4]=65524

Q.5 Declare a variable 'code' of type union item consisting of an integer m,float x and char c. Explain what would be the output of the following statements:

code.c='A';

code.m=385;

code.x=14.7;

printf("%c%d",code.c,code.m);

Ans.:

The output of above code is some garbage value

3 13107

These are basically garbage values because the union assigns memory of the member occupying highest memory space. In above example float requires highest memory space(i.e. 4 bytes). And at a time union keeps only one member active. We have lastly accessed

code.x=14.7;

Hence the variable x will be currently active and remaining members will hold the garbage values. Hence code.c and code.m are printing garbage values.

 

Q.6  What do you mean by tag of a structure ?

Ans. :

The structure tag is a name used along with the structure declaration. This tag is useful in declaration of structure variables.

For example

typedef struct student

{

int roll;

char name [20];

}st;

Here st is used as a tag for the structure student.

We can declare then

st s1, s2, s3;

The variables s1, s2, s3 are of structure type.

 

Q.7 What is the use of. operator in structures ?

Ans. :

The operator is used to access the members of the structure.

For example

s1.name or s1.roll

 

Q.8 Define structure within a structure. What is its scope?

Ans. : The structure within a structure is called nested structure. When we have to handle a collection of data which is related to another kind of collection of data then it is comfortable to use nested structures.

For example

struct bdata {

int day;

int mnth;

int year;

} dt;

struct student {

int roll.no;

Char name [10];

struct bdate dt;

} std;

One can access the birthdate of a student by following statements -

printf("\n Enter Student's birth date (mm.dd.yy)");

scanf("%d %d %d", &std.dt.month, &std.dt.day,&std.dt.year);

The scope of the nested structure is within the outer structure, and it can be accessed with the tag of outer and inner structure.

 

Q.9 Define preprocessor directives and list out a few examples. AU; Dec.-18, May-19

Ans. :

• Preprocessor directive is a kind of program which is used to preprocess the C program before compiling.

• Commands used in preprocessor are called preprocessor directives and they begin with symbol #.

Examples of Preprocessor Directives :

Macro: Using #define the constant value can be defined.

Header file inclusion: Using #include the required header file can be included in the C program.

• Conditional compilation: Using #ifdef,#endif#if,#else the conditions can be included in the program before compilation.

 

Q.10 Define pointer and initialize it. AU: May-19

Ans. : Pointer is a variable that represents the memory location of some other variable. It can be initialized as

int a = 10;

int *ptr=&a;

 

Q.11 What does #include < header_name> do? How is it possible to tell the preprocessor where to look for header files? AU: Dec.-19

Ans. : The #include is a preprocessor directive which requests the header file. It is possible to tell the preprocessor to look for the desired header file by specifying its name and path of the file. For standard header files of C program the system directories are searched.

 

C Programming and Data Structures: Unit II: C Programming – Advanced Features : Tag: : C Programming - Advanced Features | C Programming and Data Structures - Two Marks Questions with Answers