Spamdark
Spamdark

Reputation: 1391

Weird Syntax Error In C in If-else

I was programming an apache module. In the middle of the programming, I was opening a file, but I got an error while compiling.

32. static int wqb_handler(request_rec* req){
33. // Open and read our requested file
34. const char* p_file = req->filename;
35. 
36. FILE* req_file;
37. if((req_file = fopen(p_file,"r"))==NULL){
38.    return HTTP_NOT_FOUND;
39. }else{
40.     fclose(req_file);
41. }
42. // Required variables
43. const char* content_type_a = "text/html";
44.
45. // Set Headers
46. ap_set_content_type(req,content_type_a);
47. if(req->header_only){
48.    return OK;
49. }
50. 
51. 
52. return OK;
53. }

The problem is in that function, I was checking that was the problem, and I think the problem is the if-else statement, the code is written in C, not in C++.

These are the errors:

C:/wqb/wqb1_apache2.c(43) : error C2143: syntax error : missing ';' in front of 'const'
C:/wqb/wqb1_apache2.c(46) : error C2065: 'content_type_a' : undeclarated identifier

Upvotes: 6

Views: 1864

Answers (2)

yulian
yulian

Reputation: 1627

Share with the solution of your problem. It will help the others to understand it more quickly.

Improve your knowledge about operators and comments.

You're writing too many unnecessary { and } in the operators. For example, your code:

for( i = 0; i < N; i++ )
{
   printf("Hello");
}    

More simple/clear code:

for( i = 0; i < N; i++ )
   printf("Hello");

...........................................................................

Your code (original) may look so (It's easier to read and understand.):

static int wqb_handler(request_rec* req)
{
    /* Open and read our requested file */
    const char* p_file = req -> filename;

    FILE* req_file;
    if((req_file = fopen(p_file,"r"))==NULL)
       return HTTP_NOT_FOUND;

    else
       fclose(req_file);

    /* Required variables */
    const char* content_type_a = "text/html";

    /* Set Headers */
    ap_set_content_type(req,content_type_a);

    if(req->header_only)
       return 0;

return OK;
}

Upvotes: -2

Jens
Jens

Reputation: 72746

If this is C, and you're not compiling in C99 mode (i.e. with a C89 compiler), remember that all declarations must be directly following the start of a block. Mixing declarations and code is a C99 feature imported from C++.

It appears you are compiling with a Micrososft Visual Studio Compiler in C mode. Note that William H. Gates III chose to ignore C99 entirely and refuses to update the C implemenation for the third millennium. :-)

Upvotes: 14

Related Questions