'expected expression before ; syntax error in C

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.



Solution 1:[1]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 etahae