12.19 – Pragma directive in C language

by subbu on January 1, 2014

It is a special directive to the compiler that helps to turn on/off certain features.  Pragmas are compiler dependents, different pragmas are supported by different compilers. If compiler doesn’t recognize a pragma then ignores it without showing any errors or warnings.

Now we will discourse some pragmas supported by Turbo C

#pragma startup

Generally, entry point to any C program is the main(). Pragma srartup is used to perform some task by calling a function before control enters into the main()

#pragma exit

Generally, program execution would be completed on completion of main() execution. Pragma exit is used to perform some task by calling a function immediately on completion of main() execution

Note:
Functions that are called by two pragmas startup and exit must neither accept arguments nor return any values.
These functions must be declared (proto type/signature) before pragma definitions

Example:

#include<stdio.h>
void first();
void last();
#pragma startup first
#pragma exit last
int main()
{
 printf("\nWithin main..");
 return 0;
}
void first()
{
 printf("\nWithin First..");
}
void last()
{
 printf("\nWithin Last..");
}

Output:
Within First..
Within main..
Within Last..

Example explained:
As the pragmas initiate, the compiler calls the first() before main() and last() after the main().

Errors vs. Warnings
Compiler throws errors and warnings while compiling the program if any thing in the code is against to the language specification.
There would be no other choice rather rectifying the errors by modifying the code. Where as warnings are instructions to the programmer saying that, it is a bad C practice and something may go wrong.
Build can be generated without addressing the warnings but, can’t be generated by rectifying the errors.
Hence some times, when we debug big programs, we give first priority to rectify the errors by suppressing warnings. Once we rectify the errors then we consider addressing the warnings.
Now we will see some cases where Turbo C throws a warning rather an error

#include<stdio.h>
int first();
void second(int);
int third();
int main()
{
 first();
 second(4);
 third();
 return 0;
}
int first()        /* not returning */
{
 printf("\nWithin the first");
}
void second(int x) /* x is not being used */
{
 printf("\nIn the second");
}
int third()
{
 int a=10;
 return a;
 a+=10;           /* not reachable */
}

Output:
Within the first
In the second

Warning: Parameter ‘x’ is never used in function second
Warning: Unreachable code in function third
Warning: Function should return a value in function third

#pragma warn:

It is a pragma helps to suppress warnings. There are different options used to suppress or flash warnings. Warnings are suppressed if – is used as a prefix to the options and flashed if + is used as a prefix to the warning code.
The following table list out some of warning codes of ANSI C

ANSI VIOLATIONS

1 Assigning ‘type’ to ‘enumeration’ eas
2 Bit fields must be signed or unsigned int bbf
3 Both return and return with a value used ret
4 Declare type ‘type’ prior to use in prototype dpu
5 Division by zero zdi
6 Hexadecimal value contains more than 3 digits big
7 Initializing ‘enumeration’ with ‘type’ bei
8 ‘identifier’ is declared as both external and static ext
9 Ill-formed pragma ill
10 Initialization is only partially bracketed pin
11 Redefinition of ‘macro’ is not identical dup
12 Suspicious pointer conversion sus
13 Undefined structure ‘structure’ stu
14 Void functions may not return a value voi

Frequent Errors

1 Code has no effect eff
2 Function should return a value rvl
3 Parameter ‘parameter’ is never used par
4 Possible use of ‘identifier’ before definition def
5 Possibly incorrect assignment pia
6 Unreachable code rch

Less Frequent Errors

1 Ambiguous operators need parentheses amb
2 Array variable ‘identifier’ is near ias
3 Call to function with no prototype pro
4 Call to function ‘function’ with no prototype pro
5 Condition is always false wccc
6 Condition is always true wccc
7 ‘identifier’ declared but never used use
8 ‘identifier’ is assigned a value that is never used aus
9 No declaration for function ‘function’ nod
10 Structure passed by value stv
11 Superfluous & with function amp

Portability Warnings

1 Constant is long cln
2 Constant out of range in comparison rng
3 Conversion may lose significant digits sig
4 Non portable pointer comparison cpt
5 Non portable pointer conversion rpt
6 Mixing pointers to signed and unsigned char ucp

We can suppress warnings in the above program using pragma warn and placing – as prefix to warning codes rvl (Function should return a value), par (Parameter is never used), rch (Unreachable code)

#include<stdio.h>
#pragma warn -rvl
#pragma warn -par
#pragma warn -rch
int first();
void second(int);
int third();
int main()
{
 first();
 second(4);
 third();
 return 0;
}
int first()        /* not returning */
{
 printf("\nWithin the first");
}
void second(int x) /* x is not being used */
{
 printf("\nIn the second");
}
int third()
{
 int a=10;
 return a;
 a+=10;           /* not reachable */
}

Output:
Within the first
In the second

The above program shows no warnings

Conclusion:
pragmas are not same to all the compilers, changes compiler to compiler. Refer implementation manual of a compiler in which you are compiling the program. To demonstrate the functionality of pragma, here I have used pragmas of Turbo C, don’t expect these to work on other compilers.

Previous post:

Next post: