Tuesday 23 April 2013

C/C++ Puzzles: PART - 33

C Program Showing Errors When Compiled as C++ Program 

  Write a C Program without any errors, but the same when compiled as a C++ Program should show errors? This was a question I had to answer recently. There are many different answers I suppose. The program given below is the one which comes to my mind first. 

#include<stdio.h>

void func();

int main()
{
  int x;
  x=333;
  func(x);
  return 0;
}

void func(int x)
{
    printf("\nValue is %d\n",x);
}

The program gives an error when compiled as a C++ program because in C a function with an empty parameter list can have any number of parameters including zero parameters where as in C++ it means the function can't receive any parameter.

No comments:

Post a Comment