Back to: C Tutorials For Beginners and Professionals
Miscellaneous Directives in C
In this article, I am going to discuss the Miscellaneous Directives in C with Examples. Please read our previous article, where we discussed Conditional Compilation Directives in C. At the end of this article, you will understand what Miscellaneous Directives in C are and when and how to use Miscellaneous Directives in C Program with examples.
#pragma Miscellaneous Directives in C
It is a compiler dependent pre-processor i.e. all the compilers don’t support this pre-processor. A processor directive that is not specified by ISO standard. Pragmas offers control actions of the compiler and linker. #pragma is a miscellaneous directive that is used to turn on or off certain features. It varies from compiler to compiler if the compiler is not recognized then it ignores it. #pragma start-up and #pragma exit used to specify which function should be called upon start-up (before main()) or program exit (just before the program terminates). Startup and exit functions should not receive or return any values. #pragma wanr used to suppress (ignore) specific warning message from the compiler.
- #pragma warn –rrl: Return value warnings
- #pragna warn –par: Parameter not used warnings
- #pragma warn –rch: Unreachable code warnings
Program:
#include<stdio.h> #pragma warn -rrl #pragma warn -par #pragma warn -rch int abc (int a) { print ("Hello abc"); } void main () { abc (10); return; getch (); }
When this code is passed for compilation, then we are not getting any return value, parameter never used, and unreachable code warning messages.
Program:
#include<stdio.h> void abc (); void xyz (); #pragma startup abc #pragma exit xyz void func1 () { printf ("Hello abc"); } void func2 () { printf ("Hello xyz"); } int main () { printf ("Hello main()"); return 0; }
The above code will produce the output as given below when running on GCC compilers:
Hello main()
This happens because GCC does not support the #pragma startup or exit. However, you can use the below code for a similar output on GCC compilers.
Program:
#include<stdio.h> void abc (); void xyz (); void __attribute__ ((constructor)) abc (); void __attribute__ ((destructor)) xyz (); void abc () { printf ("Hello abc \n"); } void xyz () { printf ("Hello xyz\n"); } int main () { printf ("Hello main()\n"); return 0; }
Output:
- In the previous program, abc function is loaded first before loading the main function and xyz function is loaded after loading the main function.
- Between startup and exit automatically the main function is executed.
- In implementation when we are having more than 1 startup and exit function then according to the priority, we can execute those functions.
- In #pragma startup, function which is having the highest priority, it will be executed first and which is having the least priority, it will be executed at last before main().
- In #pragma startup, when equal priority occurred then the last specified function will be executed first.
- In #pragma exit, function which is having the highest priority, it will be executed in end and which is having the least priority, it will be executed first after main() only.
- In #pragma exit, when equal priority occurred, then the last specified function will be executed first.
Program:
#include<stdio.h> void abc () { printf ("From abc \n"); } void xyz () { printf ("From xyz \n"); } void close () { printf ("From close \n"); } void end () { printf ("From end \n"); } #pragma startup abc 2 #pragma startup xyz 1 #pragma exit close 1 #pragma exit end 2 void main () { printf ("From main() \n"); }
Output:
#error Miscellaneous Directives in C
By using this prepocessor, we can create user-defined error messages at the time of compilation.
Program:
#include<stdio.h> #define NIT void main () { #ifndef NIT #error NIT MACRO NEEd TO BE DEFINE #endif #ifdef NIT printf ("Welcome\t"); printf ("NIT"); #endif }
Output: Welcome NIT
In the previous program, ifNIT MACRO is not defined, it gives the error at the time of compiling.
#line Miscellaneous Directives in C
By using this prepocessor, we can create user-defined line sequences in intermediate file. It is used to reset the line number in the code.
Program:
#include<stdio.h> void main () { printf ("A\n"); #if 5>2!=1 printf ("NIT"); printf ("B"); #endif #line 4 printf ("Welcome\t"); printf ("C"); }
Output:
When the previous code is pre-processing, the line sequence is reset to 4.
Here, in this article, I try to explain Miscellaneous Directives in C. I hope you enjoy this Miscellaneous Directive in C article. I would like to have your feedback. Please post your feedback, question, or comments about this article.