Question:
is " void" a datatype in any computer language?
anuragLucknow
2006-02-05 03:17:36 UTC
we cant declare any variable as void datatype..but for a function if we r not gonna return a value .we hav to declare it as void function.does it mean void a datatype?
Three answers:
brunt
2006-02-05 05:56:00 UTC
In C, "void" has three distinct uses.



1) Return values for functions. If a function returns literally NOTHING, then it is declared as returning void. This tells the compiler not to push/pop any return value on/off the stack on return. It also tells the compiler that the result of the function cannot be assigned or compared to any variable.



2) Generic pointers. When you are dealing with generic pointers, such as returns from "malloc", parameters to "free", "memcpy" and such, these functions need pointers, but they really don't care what data type the pointers actually point to. Rather than declaring a copy of these functions that returns every possible atomic type (char, short, int, long, float, double and such), they simply use a pointer to type void (void*) instead.



3) Ignoring return values. If you are using a particularly picky compiler, or even a program such as Lint that produces warnings when you ignore the return value of a function that returns something other than void, you can cast the function call to type void to make the warning go away if you really don't need to know the return value.



For instance, "printf" returns an "int" value. So if you use the following line in your program:



printf ("Hello world");



it could actually generate a warning, since you ignore the return value. If instead you use the line



(void) printf ("Hello world");



you are telling the compiler that you are purposefully ignoring the return value, so it will not issue a warning in this case. This is of particular use if you have a policy of zero warnings (as all programmers should).



"Void" kind of lives in limbo just outside of datatypes - it is not a modifier such as "static", "const" or "volatile", but it is not possible to declare a variable of type "void". It is kind of in its own little world there.
Tof
2006-02-05 11:19:50 UTC
Yeah void is a datatype that doesn't apply to variable. But for variable you can use void* to say a pointer on any datatype...



It's only used in C, other languages just allow you not to declare any retrurn type for your function...
Tanaeem
2006-02-05 15:41:19 UTC
void is also a data Type in Java but you can't have any variable of it. There is also a Void object to box void data type. You can't derectly use this object too. It is needed when work with reflection is done.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...