C program that is not a C++ program

created by ariels
(idea) by ariels (1.8 d) (print)   (I like it!) 1 C! Mon Sep 25 2000 at 20:54:45

#include <stdlib.h>
#include <stdio.h>

int main()
{
  int *new = malloc(10*sizeof(*new));

  printf("The address of new is %p\n", (void*)new);

  return EXIT_SUCCESS;
}

Please compile this on your C compiler, with the maximal level of warnings (gcc -pedantic -Wall might be a good place to start). This program is strictly conforming standard ISO C.

Now please try to compile it with your C++ compiler, with the maximal level of warnings (g++ -pedantic -Wall is a nice try, although it will accept both includes, which strictly speaking are deprecated in ISO standard C++, either). I count 3 corrections required to make it into a horrible but conforming C++ program.

C != C++

(no, that's not legal C OR C++...)

The noder known as ISO C. Gorgonzola points out it is legal C and legal C++. Just that its value is undefined by both ISO C and ISO C++.

(idea) by stupot (9 mon) (print)   (I like it!) Fri Jan 18 2002 at 23:06:06

The following program will print a different value for a depending on whether it is compiled in C (pre C99 standard) or C++

#include <stdio.h>
void main()
{
   int a;
   a=4//**/2;
   ;
   printf("%d\n",a);
}

The reason being that traditional C reads the line as a=4/2; with a comment in the middle. C++ (and C99) comments out the whole line after the 4, and uses the following semicolon to end the line.

Y'know, if you log in, you can write something here, or contact authors directly on the site. Create a New User if you don't already have an account.