Syntax error in C in If-else

I programmed the apache module. In the middle of programming, I opened the file, but during compilation I got an error.

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 this function, I checked that it is a problem, and I think that the problem lies in the if-else statement, the code is written in C, not 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 
+6
source share
2 answers

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

It seems you are compiling with the Micrososft Visual Studio Compiler in C mode. Note that William H. Gates III decided to completely ignore C99 and refuses to update the C implementation for the third millennium. :-)

+13
source

Share the solution to your problem. This will help others understand this faster.

Improve your knowledge of operators and comments.

You write too many unnecessary { and } in statements. For example, your code:

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

More simple / understandable code:

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

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

Your code (original) may look like this ( 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; } 
-1
source

All Articles