-4

my first time really working with #pragma and for some reason I don't get the same output as the ones posted online, the functions don't print out, I use GCC v5.3 and clang v. 3.7. Here's the code

#include<stdio.h>

void School();
void College() ;

#pragma startup School 105
#pragma startup College
#pragma exit College
#pragma exit School 105

void main(){
    printf("I am in main\n");
}

void School(){
    printf("I am in School\n");
}

void College(){
    printf("I am in College\n");
}

and I compile with "gcc file.c" and "clang file.c". The output I get is "I am in main"

MikeCAT
  • 73,922
  • 11
  • 45
  • 70
JSamson
  • 31
  • 6
  • 4
    Where did you find `#pragma startup` and `#pragma exit` in the [GCC documentation](https://gcc.gnu.org/onlinedocs/gcc-4.9.2/gcc/Pragmas.html)? – Andrew Henle Mar 25 '16 at 01:30
  • 2
    Pragmas are compiler-dependent, the [GCC online documentation about pragmas](https://gcc.gnu.org/onlinedocs/gcc/Pragmas.html) doesn't list these ones, so it's likely Clang (which aims for GCC compatibility) doesn't have them either. The [Visual C compiler doesn't have these pragmas either](https://msdn.microsoft.com/en-us/library/d9x1s805.aspx). A quick search seems to indicate they are specific to [Embarcadero C++ Builder](https://www.embarcadero.com/products/cbuilder). – Some programmer dude Mar 25 '16 at 01:31
  • http://stackoverflow.com/q/29462376/971127 – BLUEPIXY Mar 25 '16 at 01:39

1 Answers1

2

#pragma is not consistent across compilers. It is only meant to be used in odd situations with specific compilers/platforms. For a general program like this it should never be used.

A better way to accomplish this is with #define and #if. For example:

#include<stdio.h>

#define SCHOOL 1
#define COLLEGE 2

#define EDUCATION_LEVEL COLLEGE

void None();
void School();
void College();

void main(){
    #if EDUCATION_LEVEL == SCHOOL
        School();
    #elif EDUCATION_LEVEL == COLLEGE
        College();
    #else
        None();
    #endif
}

void None(){
    printf("I am in neither school nor college\n");
}

void School(){
    printf("I am in School\n");
}

void College(){
    printf("I am in College\n");
}
smead
  • 1,768
  • 15
  • 23