[Courses] Absolute Beginning C: void?

Kelly Martin kelly.lynn.martin at gmail.com
Mon Feb 6 05:04:13 UTC 2012


On Sun, Feb 5, 2012 at 10:48 PM, techno curmudgeon <
technocurmudgeon at gmail.com> wrote:

> In looking around (extracurricular reading?  I never even did that in
> university!) a lot of the 'hello world' examples start with 'void' in
> front of main().
>
> Researching "void" almost always says words to the effect "is the type
> for a function does not return a value"...OK, got that.
>
> Then they launch into wildly gobbledegooky jargon about other uses.
> Including "A C prototype taking no arguments, e.g. void f() above, has
> been deprecated however in C99" (from a footnote in
> http://en.wikipedia.org/wiki/Void_type).
>
> I don't want to get too off track here.  Since hello world worked, a
> void keyword is not required (right?).  And since it didn't generate a
> warning, it's not even bad form not to have one, right?
>
> I guess that's my question.  While not needed, is it good practice to
> use the 'void' keyword?
>
> Thanks!
> _______________________________________________
> Courses mailing list
> Courses at linuxchix.org
> http://mailman.linuxchix.org/mailman/listinfo/courses
>

I'm sure someone will correct me if I'm wrong, but what I recall being
deprecated is not the use of void as the return type, but the use of an
empty argument list (that is, the "()" in "void f()") in function
prototypes.

Technically, what an empty argument list means is "arguments are
unspecified", and the resulting function prototype yields a function which
may permissibly be called with any number of arguments.  The C99 standard
specifies that a function that takes no arguments shall be prototyped as
"<returntype> f(void)", where the "(void)" indicates that the function
explicitly takes no arguments.  Calling a function with an explicitly void
formal argument list with any number of actual arguments other than zero is
an compile-time error.  Calling a function prototyped with an unspecified
formal argument list (or with no prototype at all) with any number of
arguments other than zero generates, at most, a warning.

This has nothing to do with specifying a return type of void, which one
should always declare for functions that do not have a return value.

Kelly


More information about the Courses mailing list