How Does Void Differ In C And C++ - C++ Programming Tutorial
C++ Course / Miscellaneous / How Does Void Differ In C And C++

How Does Void Differ In C And C++

BLUF: Mastering How Does Void Differ In C And C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: How Does Void Differ In C And C++

C++ is renowned for its efficiency. Learn how How Does Void Differ In C And C++ enables low-level control and high-performance computing in the tutorial below.

Before delving into the key distinction between void functions in C and C++ programming languages, let's explore some examples and gain a thorough comprehension of void functions, their applications, and the scenarios where they are utilized.

The void keyword, true to its name, signifies a lack of obligation to any specific entity within programming. A void function, upon invocation, does not yield any output to the calling location; instead, it directs control back to the point in the program where the function was invoked after completing its designated task, whether that involves computation, recursion, or displaying information on the screen. Next, we will delve into the intricacies of pointers and explore the distinctions between 'void*' 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 permits a void* pointer to be assigned to any data type.

Casting a pointer type is not required in C, unlike in C++. In C++, it is necessary to explicitly typecast a void* pointer.

Let's examine the example provided below; the following code is acceptable in C but not in 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 ensure the aforementioned code compiles in C++ too, explicit type casting must be employed, as illustrated beneath,

Example

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

Input Required

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

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience