Write a program that compiler and runs both in c and c++ and produces different results.
There are multiple types of programs that give different results when compiled in c and c++.
i. Using character literals− c and c++ both treat characters differently. In C, they are treated as integer literals whereas, in C++, they are treated as characters.
Example
#include<stdio.h> int main(){ printf("%d", sizeof('a')); return 0; }
Output
C : 4 C++: 1
ii. Use of binary number − binary values are not considered as binary in c, instead treat it as integer. But in c++, they are treated as binary.
Example
#include<stdio.h> int main(){ printf("%d", sizeof(1!=1)); return 0; }
Output
C : 4 C++: 1