Home > Enterprise >  expected expression before ; syntax error in C
expected expression before ; syntax error in C

Time:04-04

why I get compliation error

error: expected expression before ‘;’ token

Why (void) can not be an expression?

int main()
{
    (void);
    return 0;
}

After I added 1 after (void) it works but I don't know why void() alone does not.

CodePudding user response:

"(type)" is to cast variables, i don't know what you're trying to do but if you want to have a function that does nothing, just return 0.

CodePudding user response:

What you are trying to do there is called typecasting, it allows us to change the data type of a variable during runtime. The right way to do it is like this :

int main()
{
    float fvar = 14.2;
    int   ivar = (int)fvar;
    return 0;
}

What I did here is that i assigned a float variable to an int by casting fvar to the same data type as ivar.

And when you wrote void(), you were just trying to call a function named void that doesn't exist, which causes an error.

  • Related