Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different output for different compiler - C and C++ [duplicate]

Tags:

c++

c

compilation

Can you think of 'a program' which gives 'different outputs for a C and a C++ compilers' (yet gives consistent output under the same language)?

like image 606
Aditya369 Avatar asked Mar 29 '11 03:03

Aditya369


2 Answers

This program produces 12 in C++ or C99, and 6 in C89:

#include <stdio.h>

int main()
{
    int a = 12//**/2;
    ;

    printf("%d\n", a);
    return 0;
}
like image 71
caf Avatar answered Oct 06 '22 00:10

caf


Incompatibilities between ISO C and ISO C++

A common example is sizeof('A'), which is usually 4 in C but always 1 in C++, because character constants like 'A' have the type int in C but the type char in C++:

#include <stdio.h>

int main(void)
{
    printf("%d\n", sizeof('A'));
}
like image 27
Adam Rosenfield Avatar answered Oct 05 '22 23:10

Adam Rosenfield