"int main (vooid)"? How does that work?

CSyntaxMain

C Problem Overview


I recently had to type in a small C test program and, in the process, I made a spelling mistake in the main function by accidentally using vooid instead of void.

And yet it still worked.

Reducing it down to its smallest complete version, I ended up with:

int main (vooid) {
    return 42;
}

This does indeed compile (gcc -Wall -o myprog myprog.c) and, when run, it returns 42.

How exactly is this valid code?


Here's a transcript cut and pasted from my bash shell to show what I'm doing:

pax$ cat qq.c
int main (vooid) {
    return 42;
}

pax$ rm qq ; gcc -Wall -o qq qq.c ; ./qq

pax$ echo $?
42

C Solutions


Solution 1 - C

It's simply using the "old-style" function-declaration syntax; you're implicitly declaring an int parameter called vooid.

Solution 2 - C

It's valid code, because myprog.c contains:

int main (vooid) // vooid is of type int, allowed, and an alias for argc
{     
  return 42; // The answer to the Ultimate Question
} 

vooid contains one plus the number of arguments passed (i.e., argc). So, in effect all you've done is to rename argc to vooid.

Solution 3 - C

In C, the default type for a function argument is int. So, your program is treating the word vooid as int main(int vooid), which is perfectly valid code.

Solution 4 - C

It is only gcc -std=c89 -Wall -o qq qq.c and gcc -std=gnu89 -Wall -o qq qq.c don't emit a warning. All the other standards emit a warning about implicit type int for vooid.

int main(chart) behaves the same way as does int main (vooid).

return vooid; returns the number of command line arguments.

I tested with gcc 4.4.5 on Debian testing system.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpaxdiabloView Question on Stackoverflow
Solution 1 - COliver CharlesworthView Answer on Stackoverflow
Solution 2 - CMichael GoldshteynView Answer on Stackoverflow
Solution 3 - CChinmay KanchiView Answer on Stackoverflow
Solution 4 - Cvpit3833View Answer on Stackoverflow