what is the purposes of the keyword void?where is this keyword is used?
about keyword void in C language
Collapse
X
-
Tags: None
-
All void means is "no type".
So this:
where func is:Code:int var = func();
will never work because func is "no type". Therefore no value can be assigned to var.Code:void func();
You see, when you call a function it becomes an instance of its return type so
tells the compiler that if func() is called it will result in an int which can be assigned to var. So this is safe:Code:int func();
butCode:int var = func();
means that func will result in "no type" so an assignment to var is invalid.Code:void func();
Then there's
where func can take an argument of anything and the function will work. In C functions must have unique names. If arg can be a pointer to a double or an int you can't have:Code:int func(void* arg);
You would need:Code:int func(int* arg); double func(double* arg);
If there were 30 types func needs to handle you would need 30 functions. So this is the usual solution:Code:int func-int(int* arg); double func-double(double* arg);
The argtype can be used inside the function to typecast arg to the correct type. Now you need only one function.Code:int func(void* arg, unsigned int argtype);
Comment