How Does Void Differ In C And C++

Before learning the differentiating factor of void functions in C and C++ programming language, let us see a few examples and deeply understand the void function where it is used, use cases we can draw etc.

Void funThe void, as the name suggests, there is nothing it owes to any entity in programming. The void function, when called, will not return anything to the primary position; the control jumps to where the process is called after finishing the operation allowed in the process, which can either be calculation, recursion or printing anything to the display screens and then we will look at the play of pointers and examine How does 'void*' differ in C and C++?

C++ void function-1

Example

// C++ code to demonstrate void()
// returning void()
// here we are writing down the C++ programming language to demonstrate the
// concept of C++ code to demonstrate void() and returning the function
// void() function as well
#include <iostream>
using namespace std;

// the below small code snippet demonstrates the void function. It's the syntax
void work()
{
	cout << "As we are expecting it to perform the void function has returned the void to the main control";
}

// Driver void() returning void work()
// this below line (driver void code) returns the 
//void function to the function call from where it has been called in the code
void test()
{
	// this below line returns the void function to the function call 
	// from where it has been called
	return work();
}

// the main driver code functionality starts from here 
int main()
{
	// this below line simple calls the void function, which we have 
	// created just above our int main() function
 	test();
	return 0;
}

C++ void function-2

Example

// C++ code to demonstrate void()
// returning void()
// here we are writing down the C++ programming language to demonstrate the
// concept of C++ code to demonstrate void() and returning the function
// void() function as well
#include <iostream>
using namespace std;

// the below small code snippet demonstrates the void function. It's the syntax
void test()
{
    // we are writing the C code display text from here
	cout << "As we are expecting it to perform the void function has returned the void to the main control";

	// this below line returns the void function to the function call 
	// from where it has been called
	return (void)"Nope, it doesn't print anything!";
}

// the main driver code functionality starts from here 
int main()
{
    // this below line simple calls the void function, which we have 
	// created just above our int main() function
	test();
	return 0;
}

Output:

Output

//output_is_for_both_C++_codes_written_above
As we expect it to perform, the void function has returned the void to the central control.

C void Function

Example

// C code to demonstrate void()
// returning void()
// here we are writing down the C programming language to demonstrate the
// concept of C code to demonstrate void() and returning the function
// void() function as well
#include<stdio.h> 
 void display_function(void); 
 // the main driver code functionality starts from here 
void main() { 
    // this below line simple calls the void function, which we have 
	// created just above our int main() function
 display_function(); 
} //end of the void main function 
void display_function() { // Definition of Display 
 printf("C void function print statement 1"); 
 printf("C void function print statement 1 perfection in programming in C.\"\n"); 
}

Output:

Output

C void function print statement 1C void function print statement 1 perfection in programming in C."

How does 'void*' differ in C and C++?

A c programming language allows a void* pointer to be assigned to any

pointer type without a cast, whereas C++ does not. We have to typecast the void* pointer in C++ explicitly.

Let us look at the example below; the following is valid in C but not C++:

Example

void* pointer;
int *i = pointer; // this is an example of the Implicit conversion from void* to int*

Similarly,

Example

int *j = malloc(sizeof(int) * 5);  // this is an example of the Implicit conversion from void* to int*

To make the above code compile in C++ as well, we have to use explicit casting, as shown below,

Example

void* pointer;
int *i = (int *) pointer;
int *j = (int *) malloc(sizeof(int) * 5);

Input Required

This code uses input(). Please provide values below: