What does void mean in C, C++, and C#?
Solution 1
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.
Solution 2
I have always taken it to mean absent. Here are four cases in the C language that matches to this use of absent
-
R f(void)
- Function parameters are absent -
void f(P)
- Return value is absent -
void *p
- Type of what is pointed to is absent -
(void) p
- Usage of value is absent
Other C descendants use it for other things. The D programming language uses it for cases where an initializer is absent
-
T t = void;
- initializing value is absent
Solution 3
There are two ways to use void:
void foo(void);
or
void *bar(void*);
The first indicates that no argument is being passed or that no argument is being returned.
The second tells the compiler that there is no type associated with the data effectively meaning that the you can't make use of the data pointed to until it is cast to a known type.
For example you will see void*
used a lot when you have an interface which calls a function whose parameters can't be known ahead of time.
For example, in the Linux kernel, when deferring work you will set up a function to be run at a latter time by giving it a pointer to the function to be run and a pointer to the data to be passed to the function:
struct _deferred_work {
sruct list_head mylist;
.worker_func = bar;
.data = somedata;
} deferred_work;
Then a kernel thread goes over a list of deferred work and when it gets to this node it effectively executes:
bar(somedata);
Then in bar you have:
void bar(void* mydata) {
int *data = mydata;
/* Do something with data */;
}
Solution 4
It means "no value". You use void
to indicate that a function doesn't return a value or that it has no parameters or both. Pretty much consistent with typical uses of word void in English.
Solution 5
It indicates the absence of a return value in a function.
Some languages have two sorts of subroutines: procedures and functions. Procedures are just a sequence of operations, whereas a function is a sequence of operations that return a result.
In C and its derivatives, the difference between the two is not explicit. Everything is basically a function. The void
keyword indicates that it's not an "actual" function, since it doesn't return a value.
Related videos on Youtube
Nick Katsivelos
New Media Old Timer @FamilyCTO Family CTO Site On LinkedIn
Updated on November 12, 2021Comments
-
Nick Katsivelos over 2 years
Looking to get the fundamentals on where the term "void" comes from, and why it is called void. The intention of the question is to assist someone who has no C experience, and is suddenly looking at a C-based codebase.
-
Stargateur about 6 yearsThis question mix 3 languages... that legacy question should be split into 3 different questions.
-
-
RichardOD almost 15 yearsMost people don't realise that void maps to the System.Void structure- msdn.microsoft.com/en-us/library/system.void.aspx
-
Daniel Earwicker almost 15 yearsvoid in the argument list is optional in C++, see stackoverflow.com/questions/416345/…
-
Kjetil Joergensen almost 15 yearsI'd use "no type" rather than "no size"
-
Steve Jessop almost 15 yearsNot disagreeing, but an additional use of void is in the "cast to void" trick to suppress warnings for unused values. It's a bit of a stretch since it doesn't really correspond with how the compiler interprets things, but you can interpret that to mean "I'm using this value to do nothing".
-
Gerald almost 15 yearsAnother good point. laalto more or less mentions this, so I won't pilfer his answer.
-
Sergey Mirvoda almost 15 yearsalso void is not first class citizen in c#. you could not do this for example Action<void>.
-
Daniel Earwicker almost 15 years@Sergey - nor do you need to. More irksome is that you can't do Func<void>, which is why Action has to exist.
-
michaelwb11 almost 15 yearsOnly as a return type. I can think of three more uses of void: void arguments (function takes nothing), void pointers (no pointer type specified), and void casts (discard value).
-
Adam Rosenfield almost 15 yearsIn C++, void in the argument list is optional. However, in C, it is NOT optional: foo() means it takes any number of parameters of any type, whereas foo(void) means it takes zero parameters.
-
Mark Embling almost 15 yearsJust a note in regards to your last sentence - bear in mind that is only true of statically typed languages. Dynamically typed languages do not need void since their methods may just simply not return anything - the "void" is implied by the lack of anything else being returned.
-
Mark Embling almost 15 yearsCorrection to my last comment - I meant to say last paragraph (not sentence).
-
Markus Joschko over 14 yearsCould you also elaborate in your answer what it means when void(0) is used? For example, assert() in Release mode is usually defined as void(0).
-
Yashas over 6 yearsWhat does
(void)p
do? I did not quite get what you meant by "usage of value is absent." -
Arnie97 almost 5 years@Yashas If anyone is also confused about the
(void) var;
statement, I've found detailed answers at stackoverflow.com/q/21045615. -
Peter Mortensen over 2 yearsPerhaps update for Rust (if that is relevant)? - as the new contender.