Is it better to use C void arguments void foo(void) or not void foo()?
void foo(void);
That is the correct way to say "no parameters" in C, and it also works in C++.
But:
void foo();
Means different things in C and C++! In C it means "could take any number of parameters of unknown types", and in C++ it means the same as foo(void)
.
Variable argument list functions are inherently un-typesafe and should be avoided where possible.
What does void mean in C, C++, and C#?
Basically it means "nothing" or "no type"
There are 3 basic ways that void is used:
Function argument:
int myFunc(void)
-- the function takes nothing.Function return value:
void myFunc(int)
-- the function returns nothingGeneric data pointer:
void* data
-- 'data' is a pointer to data of unknown type, and cannot be dereferenced
Note: the void
in a function argument is optional in C++, so int myFunc()
is exactly the same as int myFunc(void)
, and it is left out completely in C#. It is always required for a return value.
void, VOID, C and C++
Yes, as far as i know the second declaration is invalid in C++ and C89, but it is valid in C99.
From The C99 draft, TC2 (6.7.5.3/10
):
The special case of an unnamed parameter of type void as the only item in the list
specifies that the function has no parameters.
It's explicitly talking about the type "void", not the keyword.
From The C++ Standard, 8.3.5/2
:
If the parameter-declaration-clause is empty, the function takes no arguments. The parameter list
(void)
is equivalent to the empty parameter list.
That it means the actual keyword with "void", and not the general type "void" can also be seen from one of the cases where template argument deduction fails (14.8.2/2
):
- Attempting to create a function type in which a parameter has a type of void.
It's put clear by others, notable in one core language issue report here and some GCC bugreports linked to by other answers.
To recap, your GCC is right but earlier GCC versions were wrong. Thus that code might have been successfully compiled with it earlier. You should fix your code, so that it uses "void" for both functions, then it will compile also with other compilers (comeau also rejects the second declaration with that "VOID").
What is (void) with () in C
(void)
has the form of a cast operation, but casting to void
(note: not to void *
) is normally not a useful thing to do.
In this context, though, (void) myFunc();
means that myFunc
returns a value, and whoever wrote this line of code wanted to throw that value away and did not want the compiler to complain about this, and/or wanted to make it clear to future readers of the code that they were throwing the value away on purpose. In the generated code, (void) myFunc();
has exactly the same effect as myFunc();
with nothing in front.
Due to historic abuses of this notation, some compilers will warn you about not using the value of certain functions (e.g. malloc
, read
, write
) even if you put (void)
in front of them, so it's less useful than it used to be.
(void) vs void as the return type of a function in C
The cast to void is there to suppress a compiler warning about unused return value.
// at least that was most probably the intention of the genius that came up with the cast.
What is the difference between void and static void function in C?
What is the difference between void and static void function in C?
The real question should be what is the difference between static
and non-static
function? (the return type void
is irrelevant, it can be int
or anything else).
The static
keyword is somewhat over used. When it applies to function, it means that the function has internal linkage, ie its scope is limited to within a translation unit (simply as a source file).
By default, function is non-static and has external linkage. The function can be used by a different source file.
In your case, the error manifests itself because static func
cannot be used in other source file.
When should static
functions be used?
static
functions are normally used to avoid name conflicts in bigger project. If you inspect Linux kernel source, example in drivers/net
you would see many static void
functions in there. Drivers are developed by different vendors and the usage of static
functions ensure that they can name the functions the way they want without worrying of name conflicts with other non-related driver developers.
What is the purpose of (void)variable in C?
In general when one does not use an function argument, the compiler is likely to warn you about it. After all if you aren't going to use it why put it there in the first place?
In effect it is an artificial use of the argument that does nothing useful other than telling the compiler you know what you are doing, so "Shatupalready with the warning!"
(void) args works on all of the platforms that I have seen. I believe the __attribute((unused)) is a gcc specific thing.
What does (void) mean in c++?
What you see there is really just a "trick" to fake variable/parameter usage.
Without those lines, a pedantic compiler will warn you about the variables not being used.
Using a construct (void)variablename;
will result in no instructions being generated, but the compiler will consider it a valid "use" of those variables.
Related Topics
Lifetime of Object Is Over Before Destructor Is Called
What's the Difference Between Type(Myvar) and (Type)Myvar
C++ System() Function - How to Collect the Output of the Issued Command
Stoi and Std::To_String on Mingw 4.7.1
"Cannot Evaluate Function -- May Be In-Lined" Error in Gdb for Stl Template Container
Undefined Symbol on a Template Operator Overloading Function
What Is a Good Naming Convention for Vars, Methods, etc in C++
When Pass a Variable to a Function, Why the Function Only Gets a Duplicate of the Variable
How to Find the Size of All Files Located Inside a Folder
How to Safely Destruct a Qthread
Sorting Std::Strings with Numbers in Them
Memory Allocation and Deallocation Across Dll Boundaries