In this video tutorial lets look at using #pragma preprocessor directive.
#pragma Directive
#pragma is a special purpose directive which can be used to turn on and off certain features.
Note: #pragma commands or keywords vary from one compiler to another. So make sure to check the keywords and/or syntax for the compiler you are using.
Video Tutorial: #pragma Preprocessor Directive: C Program
[youtube https://www.youtube.com/watch?v=3jsqavQ5faU]
Source Code: #pragma Directive: C Program
#include<stdio.h>
void init();
void end();
#pragma startup init
#pragma exit end
int main()
{
printf("We're inside main method\n");
return 0;
}
void init()
{
printf("We're inside init method\n");
}
void end()
{
printf("We're inside end method\n");
}
Output:
We’re inside init method
We’re inside main method
We’re inside end method
#pragma startup and #pragma exit are keywords.
#pragma startup followed by a function name: this makes the function name specified to be executed before main method.
#pragma exit followed by a function name: this makes the function name specified to be executed just before program termination.
Note:
#pragma startup and #pragma exit doesn’t work in GCC compiler. So the equivalent behavior can be obtained by using below syntax:
Source Code: For GCC Compiler: C Program
#include<stdio.h>
void __attribute__((constructor)) init();
void __attribute__((destructor)) end();
int main()
{
printf("We're inside main method\n");
return 0;
}
void init()
{
printf("We're inside init method\n");
}
void end()
{
printf("We're inside end method\n");
}
Output:
We’re inside init method
We’re inside main method
We’re inside end method
__attribute__((constructor)) acts like #pragma startup and __attribute__((destructor)) acts like #pragma exit.
#include<stdio.h>
void init();
void end();
int main()
{
printf("We're inside main method\n");
return 0;
}
void __attribute__((constructor)) init()
{
printf("We're inside init method\n");
}
void __attribute__((destructor)) end()
{
printf("We're inside end method\n");
}
Output:
We’re inside init method
We’re inside main method
We’re inside end method
We could even use __attribute__((constructor)) and __attribute__((destructor)) in function definition, as shown in above source code.
Source Code: Suppressing warnings using #pragma Directive: C Program
#include<stdio.h>
#pragma warn -rvl /* No Return Value */#pragma warn -par /* Parameter Not Used */#pragma warn -rch /* Unreachable Code */
int main()
{
int count = 1;
printf("%d\n", count);
return 0;
count++;
}
int total()
{
printf("Sum of a and b is c\n");
}
void end(int x)
{
printf("We're inside end method\n");
}
Output:
1
Here function total has a return type of integer, but it doesn’t return any value from inside, so it must throw “no return value” warning. But that warning is suppressed by #pragma warn -rvl .
Function end takes a integer argument, but the value is nowhere used inside the function definition. Compiler should throw “Parameter Not Used” warning, but it’s suppressed by #pragma warn -par.
Inside main method, there is count++ after return. This code is never executed. So the compiler should throw “Unreachable Code” warning, which is suppressed by #pragma warn -rch.
Note: -(minus or subtraction) symbol before rvl, par and rch means compiler removes the warning. + sign indicated the warning will be shown in the editor after compilation of the program.
Also note that,
#pragma warn -rvl /* No Return Value */#pragma warn -par /* Parameter Not Used */#pragma warn -rch /* Unreachable Code */
doesn’t work on GCC compiler.
Important Note: The functions init() and end() should not receive any arguments and should not return any value, if we want to handle their invocation via #pragma.
For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List
For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert