Error in c code: expected identifier or '(' before '{' token

Short review of the program (3 problems with the body):

#include <stdlib.h> #include <stdio.h> #include <math.h> double ax, ay, t; double dt; /* other declarations including file output, N and 6 command line arguments */ ... int main(int argc, char *argv[]) { int validinput; ... /* input validation */ output = fopen("..", "w"); ... /* output validation */ for(i=0; i<=N; i++) { t = t + dt; vx = ... x = ... vy = ... y = ... fprintf(output, "%lf %lf %lf\n", t, x, y); } fclose (output); } /* ext function to find ax, ay at different ranges of x and y */ { declarations if(x < 1) { ax = ... } else if(x==1) { ax = ... } ... else { ... } if(y<0) { ... } ... } 

I get an error in the string '{/ * ext function to find ax, ay in different ranges x and y * /, saying "error: expected identifier or '(' before '{' token"

I think this may be due to an incorrect call or the creation of an external function.

+4
source share
2 answers

Your function needs a name! A block of code outside of any function in C makes no sense.

There are several syntax / conceptual errors in your example. Please clear it and clarify your question - I will try to answer better when you do this.

+6
source

Now let's look at the following example.

 #include <stdlib.h> #include <stdio.h> int main(int argc, char *argv[]) { printf("hello world \n"); return 0; } { printf("do you see this?!\n"); } 

If you compile the above program, it will give you the following error:

 $ gcc qc qc:10:1: error: expected identifier or '(' before '{' token $ 

This is because the gcc compiler expects identifier to { . Therefore, we need to update the above program as follows

 #include <stdlib.h> #include <stdio.h> int main(int argc, char *argv[]) { printf("hello world \n"); return 0; } void function() { printf("do you see this?!\n"); return; } 

It will work fine.

 $ gcc qc $ ./a.out hello world $ 

Hope this helps!

+5
source

Source: https://habr.com/ru/post/1393083/


All Articles