Sunday, February 14, 2010

#Pragma,#Warn,#Error

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
Explain the #pragma inline ?
Ans:
#pragma inline only tells the compiler that source code of program contain inline assembly language code .In in C we can write assembly language program with help of asm keyword.
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.
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.
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



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 will write #line 20

No comments: