Thursday 5 February 2015

'undef' and 'pragma' directives - 'C' Programming

Welcome to Logically Proven blog.

This post teaches you the C preprocessor directives which are not very commonly used. They are

1. #undef
2. #pragma

To understand these directives, you must know what is a macro and ifdef directives. If you don't know please have a look into the previous post in this blog.

#undef directive

At certain times, it may necessary to cause the defined name to become "undefined". This can be accomplished by using the directive "#undef". 

In order to undefine a macro that has been earlier defined.

Syntax: #undef macro template

Example: 
#include <stdio.h>
#define INTEL
int main()
{
#undef INTEL //undefined the INTEL directive
#ifdef INTEL
 printf("INTEL code\n");
#else
 printf("Other than INTEL\n");
#endif
 
printf("Common code");
 
return 0;
}
 
/* Output: Other than INTEL
           Common code
*/
In this example, even though you defined the INTEL directive before main function, the compiler executes the statement in the else "Other than INTEL" because we are undefined the INTEL directive. The #undef directive cause the definition of INTEL to be removed from the system.

All subsequent #ifdef INTEL statements evaluate to FALSE

#pragma directive

This directive is another special-purpose directive that you can use to turn on or off certain features. For example you can use this directive often when you want to run some statements before the main function, before terminating the program and also to format, suppress warnings etc.

Pragmas vary from one compiler to another. 

In case of  Microsoft C compiler that deal with formatting source listings and placing comments in the object file generated by the compiler. 

Turbo C/C++ compiler has got a pragma that allows you yo suppress warnings generated by a compiler.

We will see these pragmas and how we can make use of.

(a) #pragma startup and #pragma exit
         These directives allow us to specify functions that are called before main() or just before the program terminates. 

Example:
#include <stdio.h>
 
void Intialize();
void CleanUp();
 
#pragma startup Intialize
#pragma exit CleanUp
 
int main()
{
 printf("\nInside main");
}
 
void Intialize()
{
 printf("\nBefore Main");
}
 
void CleanUp()
{
 printf("\nBefore Exit");
}
 
/* Output:  Before Main
     Inside main
     Before Exit
*/
Note: The functions Initialize() and CleanUp() should neither receive nor return any value. 

(b) Prioritized Pragmas:
         If you want two functions to get executed at start-up, then their pragmas should be defined in the reverse order or you can mention the priorities of the directives to decide which one should run first.

Syntax:
#pragma startup [priority]
#pragma exit [priority]
Where priority is optional integral number.
For user, priority varies from 64 to 255.
For C libraries, priority varies from 0 to 63.
Default priority is 100.

startup:
Lower value - Higher priority (executes first).
Higher value - Lower Priority (executes last).

exit:
Higher value - Higher priority (executes first).
Lower value - Lower priority (executes last).

Example:
#include <stdio.h>
 
void Function1();
void Function2();
#pragma startup Function1 123
#pragma startup Function2       //default priority - 100
#pragma exit Function2          //default priority - 100
#pragma exit Function1 123
 
int main()
{
 printf("\nInside main");
 return 0;
}
 
void Function1()
{
 printf("\nInside Function1");
}
 
void Function2()
{
 printf("\nInside Function2");
}
 
/* Output:
 Inside Function2
 Inside Function1
 Inside main
 Inside Function1
 Inside Function2
*/
(c) #pragma warn
     This directive tells the the compiler whether to suppress a specific warning or not.
Some warn pragmas -
#pragma warn -rvl //return value
#pragma warn -par   //parameter not used
#pragma warn -rch   //unreachable code
The minus sign (-) is to suppress the warnings and plus sign (+) is to flash the warnings on compilation.

Example -
int function1() //no return value
{
 int a = 5;
}
 
void function2(int x) //parameter not used
{
 printf("\nInside f2");
}
 
int function3() //unreachable code
{
 int x = 6;
 return x;
 x++;
}
 
int main()
{
 function1();
 function2(7);
 function3();
 return 0;
}
If you add these pragmas with minus sign, all these warnings will be suppressed on compilation. 
It is a bad practice to suppress warnings but at times it becomes useful to suppress them.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Logically Proven
learn, teach, share

Karthik Byggari

Author & Editor

Computer Science graduate, Techie, Founder of logicallyproven, Love to Share and Read About pprogramming related things.

0 comments:

Post a Comment

 
biz.