about keyword void in C language

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • akshatha kini
    New Member
    • Oct 2015
    • 1

    #1

    about keyword void in C language

    what is the purposes of the keyword void?where is this keyword is used?
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    All void means is "no type".

    So this:

    Code:
    int var = func();
    where func is:

    Code:
    void func();
    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

    Code:
    int func();
    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 var = func();
    but

    Code:
    void func();
    means that func will result in "no type" so an assignment to var is invalid.

    Then there's

    Code:
    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:

    Code:
    int func(int* arg);
    double func(double* arg);
    You would need:
    Code:
    int func-int(int* arg);
    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:

    Code:
    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.

    Comment

    Working...