Sunday, January 4, 2009

Preprocessor Questions with Answers(with Explanation)



(1)What is preprocessor ?
Ans: All the preprocessor are not part of c program. It is only instruction to compiler. All the preprocessor process before the staring of actual compilation and create an intermediate file. In the intermediate file all preprocessor is converted in term of actual c.
To see the intermediate file :
Step 1: First create any c file let us assume test. c which contain :
#define max 10+2
void main()
{
int a;
a=max*max;
printf(“%d”,a);
}
Step 2: go to command mode. Open run then write cmd then press enter.
Step 3: Go to the directory where test .c has been created.
Step 4: write in the command mode
cpp test. c and press enter key (to create intermediate file)
Step 5: type test. i (to see the intermediate file )

(2) Why preprocessor ?
Ans :
1. It improves the readability of program.
2. It makes easy to modify
3. portability 

(2) List all preprocessor directives.
Ans: 
(3) Explain macro substitution directive?
Ans:
Syntax: #define [ ,, …] 
Here [,,…] is optional.
When you use this in the program then in the program this is called macro and #define directive only replaces the macro by before starting of actual compilation.
e.g 
#define pie 3.14
Void main()
{
float r=3,area;
area=3*r*pie;
printf(“%f”,area);
getch();
}
Before the starting of actual compilation an intermediate is formed which is :

We can see only in place of pie ,3.14 has pasted.
If is very long or we want to write in next line ,end first line by \.
e.g 
#define word c is powerful \
language.
MACRO FUNCTION: 
(2) What is use of #line directive?
Ans: It tells the compiler that next line of source code is at the line number which has been specified by constant in #line directive i.e it transfer the program control to the line number which has been specified by #line directive.
e.g 
#line 15
void main()
{
int a=10;
a++;
clrscr();
a++;
#line 5
printf(“%d”,a);
getch();
}

If we will see its intermediate file then before the actual compilation the source code is expanded as :

In the very long c program for debugging purpose if we want to check the program after the line 300 by F7 key then we write #line 20

(3) What is use of # and ## operator in c program ?
Ans: There are two operator in preprocessor.
1. # this operator is called stringizing operator which convert any argument in the macro function in the string. So we can say pound sign # is string maker.
e.g 
#define string(s) #s
void main()
{
char str[15]=string(World is our ) ;
printf(“%s”,str);
}
Output: World is our 
Explanation : Its intermediate file is :


Argument of string macro function ‘World is our’ is converted into string by the operator # .Now the string constant “World is our” is replaced the macro call function in line number 4.
2. ## This operator is called token pasting operator. When we use a macro function with various argument then we can merge the argument with the help of ## operator.
e.g 
#define merge(p,q,r) p##q##r
Void main()
{
int merge(a,b,c)=45;
printf(“%d”,abc);
Output : 45
Explanation :
Arguments a,b,c in merge macro call function is merged in abc by ## operator .So in the intermediate file declaration statement is converted as :
int abc=45;
(4) What is #error directives ?
Ans:
Syntax : #error 
If compiler compiles this line then it shows a compiler fatal error i.e it only issue an error message and this error message includes . i.e it only issue an error message and this error message includes .
e.g :

#ifndef __MATH_H
#error First include then compile
#else
void main()
{
float a,b=25;
a=sqrt(b);
printf(“%f”,a);
}
#endif
Output: compiler error --> Error directive :First include then compile
(5) What is file inclusion directive or #include directive ?
Ans: 
Syntax: #include or #include “file name”
This directive treats has included in the current file i.e in current file also contain data of the file which has included by #include directive.
e.g 
first create a file of file name cube.h which contain :
int cube ( int a)
{
Int b;
B=(a)*(a)*(a);
Return b;
}
Now create any another c file let math.c which contain :
#include “cube.h”
void main()
{
int p=5,q;
q=cube(p);
printf(“%d”,q);
}
Output :125 
Explanation: When we write #include “cube.h” the compile includes the contain of file cube.h i.e cube function in the math.c file.So we can use the function which has defined in cube.h .We can see the intermediate file of math.c :

#include : Search the file only include directory (c:\tc\include )
#include “filename” : Search the file include directory as well as current working directory.
Note : In the above question if you will save the file cube.h at c:\tc\include directory then you can write in math.c :
#include 
(6) Describe #pragma directive in detail ?
Ans:
Pragma is implementation specific directive i.e each pragma directive has different implementation rule and use . There are many type of pragma directive and varies from one compiler to another compiler .If compiler does not recognize particular pragma the it ignore the pragma statement without showing any error or warning message and execute the whole program assuming this pragma statement is not present,
e.g 
suppose any arbitrary pragma directive is #pragma world :


#pragma world
void main()
{
printf(“C is powerful language “);
}
Output : C is powerful language
Explanation: Since #pragma world is unknown for Turbo c compiler so it ignore this directive without showing error or warning message and execute the whole program assuming #pragma world statement is not present.
List of pragma directive :
1. #pragma startup
2. #pragma exit
3. #pragma warn
4. #pragma option
5. #pragma inline
6. #pragma argsused
7. #pragma hdrfile
8. #pragma hdrstop
9. #pragma saveregs
(7) What is #pragma startup and #pragma exit ?
Ans: 
Syntax:
#pragma startup [priority]
#pragma exit [priority]
Where priority is optional integeral number.
For user priority varies from 64 to 255
For c libraries priority varies from 0 to 63
Default priority is 100.
pragma startup always execute the function before the main function 
pragma exit always execute the function after the main function.
Function declaration of must be before startup and exit pragma directives and function must not take any argument and return void i.e 
void (void)
If more than one startup directive then priority decides which will execute first.
For startup :
Lower value : higher priority i.e function will execute first.
If more than one exit directive then priority decides which will execute first
For exit:
Higher value: higher priority i.e function will execute first.
For example

void india();
void usa() ;
#pragma startup india 105
#pragma startup usa
#pragma exit usa
#pragma exit india 105
void main()
{
printf("\nI am in main");
getch();
}
void india()
{

printf("\nI am in india");
getch();
}
void usa()
{

printf("\nI am in usa");
getch();
}
Output:
I am in usa
I am in India
I am in main
I am in India
I am in usa
Explanation:
Above program there are two startup directive which will execute before the main function. 
Function name India has priority 105
Function name usa has priority 100 (default)
So usa function will execute first than India function and above program there are two exit directive which will execute after the main function. 
Function name India has priority 105
Function name usa has priority 100 (default)
So india function will execute first than usa function. 
(8) Describe #pragma warn directive ?
Ans:
In c there are many warning messages which can be on or off with help of #pragma warn.
Syntax :
#pragma warn +xxx
#pragma warn –xxx
#pragma warn .xxx
Where 
+ means on
- means off
. means on/off (toggle)
xxx is indicate particular warning code in thee alphabet
e.g
rvl is warning code which means function should return a value.

#pragma warn –rvl
Int main()
{
Printf(“It will not show any warning message”);
}
Output: It will not show any warning message
When you will execute the above program then compiler will not show the warning message function should return a value because rvl warning has offed.
List of warning code :

(9)Explain the #pragma inline ?
Ans:
#pragma inline only tells the compile that source code of program contain inline assembly language code .In in C we can write assembly language program with help of asm keyword.

No comments:

Blog List