By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,515 Members | 1,339 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,515 IT Pros & Developers. It's quick & easy.

about keyword void in C language

P: 1
what is the purposes of the keyword void?where is this keyword is used?
Oct 19 '15 #1
Share this Question
Share on Google+
1 Reply


weaknessforcats
Expert Mod 5K+
P: 9,197
All void means is "no type".

So this:

Expand|Select|Wrap|Line Numbers
  1. int var = func();
  2.  
where func is:

Expand|Select|Wrap|Line Numbers
  1. void func();
  2.  
will never work because func is "no type". Therefore no value can be assigned to var.

You see, when you call a function it becomes an instance of its return type so

Expand|Select|Wrap|Line Numbers
  1. int func();
  2.  
tells the compiler that if func() is called it will result in an int which can be assigned to var. So this is safe:

Expand|Select|Wrap|Line Numbers
  1. int var = func();
  2.  
but

Expand|Select|Wrap|Line Numbers
  1. void func();
  2.  
means that func will result in "no type" so an assignment to var is invalid.

Then there's

Expand|Select|Wrap|Line Numbers
  1. int func(void* arg);
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:

Expand|Select|Wrap|Line Numbers
  1. int func(int* arg);
  2. double func(double* arg);
You would need:
Expand|Select|Wrap|Line Numbers
  1. int func-int(int* arg);
  2. double func-double(double* arg);
If there were 30 types func needs to handle you would need 30 functions. So this is the usual solution:

Expand|Select|Wrap|Line Numbers
  1. int func(void* arg, unsigned int argtype);
The argtype can be used inside the function to typecast arg to the correct type. Now you need only one function.
Oct 19 '15 #2

Post your reply

Sign in to post your reply or Sign up for a free account.