Search code examples
cpragma

Is #pragma directive compiler dependent?


I know and I've used #pragma startup and #pragma exit before but when I execute the following code it outputs only In main. Can anyone tell me what's happening here?

#include<stdio.h>
#pragma startup A 110
#pragma startup B
#pragma exit A
#pragma exit B 110

int main()
{
    printf("\nIn main");
    return 0;
}

void A()
{
    printf("\nIn A");
}

void B()
{
    printf("\nIn B");
}

Or is it compiler dependent? I am using gcc compiler.


Solution

  • All #pragma directives are compiler-dependent, and a compiler is obliged to ignore any it does not recognise (ISO-9899:2011, s6.10.6: “Any such pragma that is not recognized by the implementation is ignored.”). That's why your program compiles successfully.

    Functions A and B aren't called because... you don't call them. Apologies if you understand this perfectly well, but: a C program is executed by invoking the function main. If you want the functions A and B to be called, you have to do so within the main function.

    (In fact, recent versions of the C standard have introduced a small number of STDC pragmas which implementations are obliged to recognise, but that doesn't importantly affect the answer)