tags:

views:

545

answers:

12

this question was asked to my friend in an interview. "can you write any program that will compile in C but not in C++". is there any such program?

+12  A: 

http://en.wikipedia.org/wiki/Compatibility_of_C_and_C%2B%2B#Constructs_valid_in_C_but_not_C.2B.2B

kotlinski
This pretty much invalidates any other answer we might come up with, right?
Manuel
Not really - you can break C++ programs in more ways, by combining some differences in creative ways. But all the not-so-creative answers are in there.
MSalters
+8  A: 
int main() {
   void * v = 0;
   char * p = v;
}
anon
+13  A: 
void setValue(int new);
mouviciel
A: 

Depending on which version of C, I guess I would've chickened out and put

int main() {
  /* This is invalid in pre-C99! */
}

:)

Roel
That's the wrong way around - it compiles as C++ but not as C89.
Steve Jessop
MSalters
Argh massive fail on all sides :)
Roel
@MSalters: no it doesn't, because in C89 `main` has to return.
Steve Jessop
My mistake - I read `this` as in `this comment`.
MSalters
+5  A: 
#include <stdlib.h>

int main()
{
    char* p = malloc(10);
    return 0;
}
visitor
why will this be not compiling in c++?
Vijay Sarathi
because of the implicit void* to char* conversion that a C++ compiler won't swallow: http://codepad.org/2WFJP61O
Manuel
ah...yes...u r right +1 from me
Vijay Sarathi
In fact, this is exactly the same issue as used by my answer.
anon
+2  A: 

int main(int new, char **class) {}

Compiles in C, but not in C++ because in C++ new and class are keywords and can't be used as identifiers.

For C89, you also need return 0; in the body of the function. Using C99 was a bit of a cheat anyway, because C99 has loads of features which aren't in C++. Here I use complex types, stdint, restrict and VLAs:

#include <complex.h>
#include <stdint.h>

typedef double _Complex dc;

int funny_average(dc * restrict a, dc * restrict b, uint16_t n) {
    dc array[n+2];
    memset(array, 0, sizeof array);
    array[0] = *a;
    array[1] = *b;
    return normal_average(array, n + 2);
}

You get the idea. Some, all, or none of these might be supported as extensions by any given C++ implementation, but they aren't standard (Actually, uint16_t is optional in C99, so strictly speaking this code might not compile as C either).

Steve Jessop
+3  A: 
int main()
{
    int x[-2 + sizeof('a')];
    return 0;
}

Ooh, ooh!

#ifdef __cplusplus
   FAIL TO COMPILE HORRIFICALLY!
#else
int main() { return 0; }
#endif
Kaz Dragon
Strictly speaking, that might not compile as C either, since `sizeof(int)` might be <= 2. But it has a good chance.
Steve Jessop
Yes, sizeof('a') == sizeof(int) in C and sizeof('a') == sizeof(char) in C++, but nothing prevent sizeof(int) to be 1 or 2 in C.
AProgrammer
It would work if sizeof(int) was 2 as well, because you can have zero-sized arrays in C++ at least.But yes, failure if sizeof(int) == sizeof(char).
Kaz Dragon
+28  A: 

Why complicate things?

int main() {
    int class = 7;
}
Thomas Padron-McCarthy
+1. Clever one. `class` is a keyword in C++ and not in C.
Guru
A: 

This is a shot:

main(int argc,char**argv){
   char *p=malloc(0);
   return 0;
}

Best regards, Tom.

tommieb75
malloc(0) is actually implementation dependent in C, and I doubt that any C compilers check it at compile time, which was the question. C++ reference the C standard WRT malloc(), so I don't see how this addresses the question there either. Of course, if you are talking about void * conversions, two answers have already covered this.
anon
The use of implicit declaration of `main` as a function that returns an `int` was actually a first. +1 !
RaphaelSP
+6  A: 
int i = 42 //**/
7;

In C++, there's a missing ;

MSalters
That's also invalid in C99.
Mike Seymour
A: 
foo(x)
    int x
{
    return x * 2;
}
Charlie Somerville
A: 
int main(int argc, char * * argv)
{
  int status;
  while (argc > 0)
  {
    status = main(argc - 1, argv);
  }
  return EXIT_SUCCESS;
}

In C, the main function can be executed from inside the program.

(Critics: If this is not true, note the version of C where the specification changed.)

Thomas Matthews