Search code examples
cparallel-processingopenmppragmapreprocessor-directive

I get stray '#' in program error when I try to compile this program


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);
     }
}

Solution

  • 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.