Small Details That Matters a Lot #1: Difference in the fun() and fun(void) in C and C++ ??

I always thought that defining no argument while declaring a function is the same as defining the argument as void, lets take an example:

TRY to guess the answer of the following question:

What happens if the following program is executed in C and C++?

#include <stdio.h> 
void funtime(void)
{
printf("Hi Binod");
}
void main()
{
funtime();
funtime(3);
}

Most of you have guessed till now, the answer will be an error on both C and C++, as while executing funtime(3); integer argument is passed but there is no function funtime having an argument as an integer, so what happen is funtime( ) has no problem with funtime(void) but funtime(3) sure has so it will give error in both C++ and C compiler.

Now let's Take Another Example:

What happens if the following program is executed in C and C++?

#include <stdio.h> 
void funtime() //here no argument(void) is defined
{
printf("Hi Binod");
}
void main()
{
funtime();
funtime(3);
}

Now This time there will be Error in C++ and Output Hi Binod Hi Binod (two time )in C compiler will be there because in C compiler funtime() is treated as funtime(…) that is any no of argument can be passed of any type so C compiler will run printf statement two times without any problem

but In C++ funtime() is treated as funtime(void) so it will give error for funtime(3) as it will not get any related function.

Small Details That Matters a Lot #1: function() and function(void) is treated differently in the C compiler.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store