What happens if the following program is executed in C and C++? [code] #include <stdio.h> void func() { printf("Hello"); } void main()

Nguyễn Trà

New member
What happens if the following program is executed in C and C++?
Code:
#include <stdio.h> 
void func()
{
	printf("Hello");
}
void main() 
{ 
	func();
	func(2);
}
A. Error in both C and C++
B. Outputs Hello twice in both C and C++
C. Error in C and Outputs Hello twice in C++
D. Error in C++ and Outputs Hello twice in C
 
Answer: d
Explanation: In C++ whenever a function without argument is declared it is equivalent to function with void arguments i.e. func() == func(void) whereas in C a function without argument is equivalent to func(…) i.e. it can take any number of arguments so func(2) call is also valid in C but not valid in C++. Hence it gives error in C++ whereas no error in C.
 

Tìm kiếm

Back
Top