4

When I try to compile using -fopenmp flag, I get the below error:

stray # in program

Below is my code:

#include<omp.h> 
int main()

{        #pragma omp parallel 
     {
     int id=0;
     printf("hello(%d) ",id);
     printf("world(%d)\n",id);
     }
}
0

1 Answer 1

10

Quoting C11, chapter §6.10/p2, "Preprocessing directives" (emphasis mine)

A preprocessing directive consists of a sequence of preprocessing tokens that satisfies the following constraints: The first token in the sequence is a # preprocessing token that (at the start of translation phase 4) is either the first character in the source file (optionally after white space containing no new-line characters) or that follows white space containing at least one new-line character. The last token in the sequence is the first newline character that follows the first token in the sequence.

So, you cannot have a # after any other token. In your code

 int main()
 {        #pragma omp parallel
 ^        ^^^

the syntax violates the constraints, hence the error. You have to put it in its own line.

3
  • @JeremyP Right, let me be explicit. Thanks Mar 14, 2017 at 10:05
  • 1
    I only mention it because I spent a couple of minutes not noticing it and thinking "what's he talking about, there is nothing wrong with the #include" :)
    – JeremyP
    Mar 14, 2017 at 10:07
  • 1
    @JeremyP You are very correct to point out, sir, what may be apparent to someone, might be very confusing to others in some context. Moreover, it never hurts to be explicit about almost anything, specifically SO answers. :) Thanks for the review and comments. :) Mar 14, 2017 at 10:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.