Write a program that produces different results in C and C++ programming


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

 Live Demo

#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

 Live Demo

#include<stdio.h>
int main(){
   printf("%d", sizeof(1!=1));
   return 0;
}

Output

C : 4
C++: 1

Updated on: 20-Apr-2020

76 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements