Miscellaneous Directives in C

#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 offer 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 are 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 messages from the compiler.

  1. #pragma warn –rrl: Return value warnings
  2. #pragna warn –par: Parameter not used warnings
  3. #pragma warn –rch: Unreachable code warnings
Program to understand #pragma Miscellaneous Directives in C:
#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, parameters never used, and unreachable code warning messages.

Miscellaneous Directives in C with Examples
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 to understand Miscellaneous Directives in C Language:
#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:
Program to understand Miscellaneous Directives in C Language
  • In the previous program, the “abc” function is loaded first before loading the main function and the “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, the function which is having the highest priority, will be executed first and which is having the least priority, will be executed last before the main().
  • In #pragma startup, when equal priority occurred then the last specified function will be executed first.
  • In #pragma exit, the function which is having the highest priority will be executed in end and which is having the least priority 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:
Miscellaneous Directives in C
#error Miscellaneous Directives in C

By using this preprocessor, we can create user-defined error messages at the time of compilation.

Program to understand #error Miscellaneous Directives in C:
#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 preprocessor, we can create user-defined line sequences in an intermediate file. It is used to reset the line number in the code.

Program to understand #line Miscellaneous Directives in C:
#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:
Program to understand #line Miscellaneous Directives in C

When the previous code is pre-processing, the line sequence is reset to 4.

Follow Us On