Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

GCC pragma: error: expected expression before ‘#pragma’

Please consider the following minimal example:

#include <stdio.h>
// Compile with:
// gcc -g -o myprogram main.c
//#define SPECIAL
typedef struct {
 int id;
 char name[50];
 float value;
} MyStruct;
MyStruct example = {
 #ifdef SPECIAL
 .id = 43,
 #else
 #pragma message("Hello from structure")
 .id = 42,
 #endif
 .name = "Example Name",
 .value = 3.14f,
};
int main() {
 printf("ID: %d\n", example.id);
 return 0;
}

When I try to build this, I get:

$ gcc -g -o myprogram main.c && ./myprogram
main.c:18:11: error: expected expression before ‘#pragma’
 18 | #pragma message("Hello from structure")
 | ^~~~~~~

Compiler I use:

$ gcc --version | head -1
gcc (Ubuntu 11.4.0-1ubuntu1~22.04.2) 11.4.0

I always thought, the preprocessor is independent of C code, so I can put preprocessor statements wherever I want? So is the problem here that I have the #pragma message in between initializing fields of a structure? But then - there is no problem using #if etc in between fields of a structure, as the above example shows if you comment the #pragma message line ?!

So - is there any way to get this #pragma message to work at the position it is in?

Answer*

Draft saved
Draft discarded
Cancel
4
  • 2
    To be clear, by "#pragma is a statement", do you mean that GCC treats #pragmas as if they were statements? Because they definitely are not statements as far as the C language spec is concerned. All the same, GCC is free to behave that way, because #pragma directives where STDC does not immediately follow the directive name have implementation-defined behavior. That's pretty much their whole point. Commented Oct 23, 2025 at 17:34
  • I note also that for the STDC pragmas defined by the standard, all have this or a substantially equivalent statement in their descriptions: "The pragma can occur either outside external declarations or preceding all explicit declarations and statements inside a compound statement." That seems to be pretty close to what GCC requires of other pragmas, so there is consistency in that sense. If you want to consider it an inconsistency that other preprocessing directives can be used more freely then that falls on the language spec. I don't see a reason to characterize it as a bug in GCC. Commented Oct 23, 2025 at 17:49
  • 3
    Another option would be to use _Pragma("message(\"Hello from structure\")"). That may still be subject to some gotchas, though. Commented Oct 23, 2025 at 19:30
  • I think this is one of the reasons why _Pragma was added to the C standard. Commented Oct 24, 2025 at 6:41

lang-c

AltStyle によって変換されたページ (->オリジナル) /