10

For example, if I made the variable:

void helloWorld;

What could I do with it? Since it represents nothing, at first I thought that nothing could be done with it. I am aware that functions use void for no return value, but do the variables have a purpose?

-- EDIT --

I have found out the answer. I am now aware that void variables are illegal in programming languages such as Java, C++ and C. Thanks for all of your answers!

Taking1n1
  • 145
  • 1
  • 7
  • 1
    That will cause a compilation error in `java`. Unless you meant `Void`. – Sotirios Delimanolis Sep 09 '13 at 15:53
  • Speaking strictly about Java, `void` is a keyword that's not usable with variables. Still, `Void` is the uninstantiable object supporting the `void` keyword and, since it's an object, it could actually be usable as a type. – Fritz Sep 09 '13 at 15:55
  • 4
    I'm fairly sure you can't define a `void` variable in any of the three languages you're asking about; which makes the question rather pointless. – Mike Seymour Sep 09 '13 at 16:00
  • You are right that it doesn't make sense which is why you can't do that. – Peter Lawrey Sep 09 '13 at 16:05
  • _void *_ are mostly used as arguments in function prototypes where that argument is designed to accept a pointer to any other type of variable. – ryyker Sep 09 '13 at 16:26
  • I wonder that OP is only 14 years old! Your question deserves for +1. – haccks Sep 09 '13 at 16:49
  • @ryyker Yes, but the OP didn't ask about `void*`, he asked about `void`. – Andre Kostur Sep 09 '13 at 23:04

5 Answers5

11

void variables are invalid in C/C++ because the compiler can not determine their size. void is only valid as function argument list (takes no arguments) or return types (returns nothing).

There is void * which just means "any type pointer" and is a generic pointer type, but you are not allowed to dereference it.

Sergey L.
  • 21,822
  • 5
  • 49
  • 75
6

C99 6.2.6 paragraph 19 says:

The void type comprises an empty set of values; it is an incomplete type that cannot be completed.

Also, void in C, is a type that has no size. Thus, if you were to declare a variable of type void, the compiler would not know how much memory to allocate for it.
So, you can't declare a void variable because it is of incomplete type.

void is useful just when we're talking about pointers (void*) since it allows you to declare a generic pointer without specifying the type.

Read this answer given by Keith Thompson for more detailed explanation.

Community
  • 1
  • 1
haccks
  • 104,019
  • 25
  • 176
  • 264
  • So it is _possible_ (but useless) to declare a `void helloWorld` variable in C? A reference to the standards document which you apparently used would be nice. – MarnixKlooster ReinstateMonica Sep 09 '13 at 16:01
  • 2
    That answer does not declare a void variable: it evaluates a non-void variable expression and casts it to void. I just tried to get `gcc` to accept a variable declaration `void helloWorld` (as opposed to the expression `(void) helloWorld` where `helloWorld` is declared as a non-`void`), and failed. – MarnixKlooster ReinstateMonica Sep 09 '13 at 16:09
  • @MarnixKlooster; I read this in this [tutorial](http://www.crasseux.com/books/ctutorial/void.html). – haccks Sep 09 '13 at 16:12
  • @MarnixKlooster; Sorry! I misunderstood. Although it is syntactically correct, it is useless and that's why you are getting error (code will not compile). – haccks Sep 09 '13 at 16:19
  • 3
    @MarnixKlooster It is invalid because 1) void is an incomplete type (C99 6.2.5§19) and 2) an identifier declared with no linkage must have a complete type (C99 6.7§7). On the other hand, you can have a (completely useless) declaration such as `extern void x;` – Virgile Sep 09 '13 at 16:36
  • 3
    @Virgile: `extern void x;` does have potential (non-portable) uses: `&x` can be useful for taking the address of asm labels in inline assembly or external asm files. This in turn can be useful for determining, from a signal handler, whether the saved program counter is between two particular asm labels. – R.. GitHub STOP HELPING ICE Sep 09 '13 at 16:41
  • @R..GitHubSTOPHELPINGICE "The operand of the unary & operator shall be either ... or an **lvalue** that designates an object" and "An **lvalue** is an expression (with an object type **other than void**) that potentially designates an object". – pmor Feb 02 '22 at 02:16
3

In Java, you cannot use void as a variable type.

You can however use Void

The Void class is an uninstantiable placeholder class to hold a reference to the Class object representing the Java keyword void.

You won't be able to do anything with it, except get the Class object, but that is a static field anyway so you don't need an object reference.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
1

In C you can cast function parameters to void

(void)myarg;

this is only useful for dealing with unused parameter warnings however.

James Morris
  • 4,867
  • 3
  • 32
  • 51
0

i think you don't know what 'void' is??? do you..

small and quick info about void

void is not a data-type like int or char that you use to declare variables.

void is a return-type

void return type is used to say that we are returning nothing from this function.

if you don't return anything then you have to write void as return-type for a function (including main function). alternatively you can say ....

return 0;

at the end of function's definition.

i hope this might helped you to understand what is void.

nikhil
  • 203
  • 1
  • 10
  • this is the same case with any programming language (c,c++,java,c#, vb.net, js, motif, scripting languages, pascal, cobal)... almost everywhere – nikhil Sep 10 '13 at 03:30
  • 1
    I know what the void type is, I'm just asking if it can be used in a variable. I've pretty much got the answer now, so thanks for your answer. I will announce that I have gotten my answer in the above post.... – Taking1n1 Sep 10 '13 at 14:53