void keyword in C Language

In this page, we are trying to understand the importance of void keyword in C Programming Language.

void means nothing in English Language and the same meaning of this word is applied in C Programming Language.

void keyword is generally used in three places inside C Programming-

  1. with function return type
  2. with function arguments
  3. with pointer which point nothing or void
Let us discuss all three above usage of void keyword with Programs one by one.

1. with function return type


To illustrate this we are creating a show() function with an argument of integer type. This function return type is void means it return nothing.

Program with and without void keyword  

--------------------------------------------------------------------------------------------------------------------------
//Program to illustrate the use of void keyword in function return type with CBhashaGyan

#include<stdio.h>

void show(int);      //function declaration for show()
int show1(int);      // function declaration for show1()
int main()     // main() function with return type integer
{
int x;
printf("Enter the number\n");
scanf("%d", &x);
show(x);
printf("\n Number return x=%d", show1(x));
return 0;
}

//show() function definition
void show(int x)    //for without return any value we use void keyword
{
printf("Number=%d", x); //this line will print the value of x
}

//show1() function definition
int show1(int x)      //for return any value we use returning data type keyword
{
return  x; //this line will return the value of x to the calling place
}

--------------------------------------------------------------------------------------------------------------------------
The show() function directly print the Number hence no need to return anything so we use void keyword at the place of return type
The show1() function return the value to calling place hence we need to use return type according to data value (in this case int) so we use int keyword (in this case) at the place of return type.

2. with function arguments


To illustrate this we are creating a show() function with an argument of integer type and show1() function without argument.

Program with and without void keyword inside function argument  

--------------------------------------------------------------------------------------------------------------------------
//Program to illustrate the use of void keyword in function Argument with CBhashaGyan

#include<stdio.h>

void show(int);      //function declaration for show() with accepting integer argument
void show1(void);      // function declaration for show1() without accepting any argument
int main()     // main() function with return type integer
{
int x;
printf("Enter the number\n");
scanf("%d", &x);
show(x);   //calling show() function having integer accepting argument
show1();   // calling show1() function without accepting any argument
return 0;
}

//show() function definition
void show(int x)    //for accepting integer argument we use int keyword
{
printf("Number=%d", x); //this line will print the value of x
}

//show1() function definition
void show1(void)      //for no accepting argument we use void in argument
{
int y;
printf("Enter the number\n");
scanf("%d", &y);
printf("\n Number=%d", y);  
}

--------------------------------------------------------------------------------------------------------------------------
The show() function accept the argument of integer value and then print it directly so we use int keyword at the place of argument list
The show1() function not accepting any value so we use void keyword at the place of argument list.

3. With Pointer which point nothing or void


To illustrate this we are creating two pointers x and y and a variable a which is pointed by pointers. But pointer x points nothing at starting and hence we pointed it to void. We can point void pointer to any data type. While other pointers can only point to specific variables. 

Program with and without void keyword using pointer   

--------------------------------------------------------------------------------------------------------------------------
//Program to illustrate the use of void keyword in pointer with CBhashaGyan

#include<stdio.h>

int main()     // main() function with return type integer
{
void *x;  //pointer point nothing and hence we create it with void keyword
int a=5;
float b=7.5;
int *y=&a;    //pointer y is of integer type and hence point only integer variables
x=&a;           //now pointer x point to integer variable

printf("The value of a using x pointer=%d\n", *((int *)x));
printf("The value of a using y pointer=%d\n", *y);

x=&b; // no error because x is void pointer and can point any data type variable
y=&b; //error generated because pointer y is of integer type and b is floating point variable
printf("The value of a using x pointer=%f\n", *((float *)x));
printf("The value of a using y pointer=%d\n", *y);

return 0;
}

--------------------------------------------------------------------------------------------------------------------------
void pointer can point any data type variable but other pointer can only point to specific data variable which is of same type as pointer. After removing yellow shaded lines of code program will work correctly.

No comments:

Post a Comment