Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error C2143: syntax error : missing ';' before 'type'

I am new to programming C.. please tell me what is wrong with this program, and why I am getting this error: error C2143: syntax error : missing ';' before 'type'....

extern void func();

int main(int argc, char ** argv){
    func();
    int i=1;
    for(;i<=5; i++) {
        register int number = 7;
        printf("number is %d\n", number++);
    }
    getch();
}
like image 483
eLg Avatar asked Mar 29 '13 04:03

eLg


3 Answers

Visual Studio only supports C89. That means that all of your variables must be declared before anything else at the top of a function.

EDIT: @KeithThompson prodded me to add a more technically accurate description (and really just correct where mine is not in one regard). All declarations (of variables or of anything else) must precede all statements within a block.

like image 128
Ed S. Avatar answered Oct 15 '22 12:10

Ed S.


I haven't used visual in at least 8 years, but it seems that Visual's limited C compiler support does not allow mixed code and variables. Is the line of the error on the declaration for int i=1; ?? Try moving it above the call to func();

Also, I would use extern void func(void);

like image 43
Randy Howard Avatar answered Oct 15 '22 13:10

Randy Howard


this:

int i=1;
for(;i<=5; i++) {

should be idiomatically written as:

for(int i=1; i<=5; i++) {

because there no point to declare for loop variable in the function scope.

like image 1
lenik Avatar answered Oct 15 '22 13:10

lenik