- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Write a program that produces different results in C and C++
Here we will see some program that will return different results if they are compiled in C or C++ compilers. We can find many such programs, but here we are discussing about some of them.
In C and C++, the character literals are treated as different manner. In C, they are treated as int but in C++, they are treated as characters. So if we check the size using sizeof() operator, it will return 4 in C, and 1 in C++.
Live Demo For C.
Example
#include<stdio.h> int main() { printf("The character: %c, size(%d)", 'a', sizeof('a')); }
Output(C)
The character: a, size(4)
Live Demo For C.
Example
#include<stdio.h> int main() { printf("The character: %c, size(%d)", 'a', sizeof('a')); }
Output(C++)
The character: a, size(1)
In C if we use struct, then we have to use struct tag when we are using it until some typedef is used. But in C++, we do not need to struct tag to use structures.
Live Demo For C.
Example
#include<stdio.h> struct MyStruct { int x; char y; }; int main() { struct MyStruct st; //struct tag is present st.x = 10; st.y = 'd'; printf("Struct (%d|%c)", st.x, st.y); }
Output(C)
Struct (10|d)
Live Demo For C++.
Example
#include<stdio.h> struct MyStruct{ int x; char y; }; int main() { MyStruct st; //struct tag is not present st.x = 10; st.y = 'd'; printf("Struct (%d|%c)", st.x, st.y); }
Output(C++)
Struct (10|d)
The size of the Boolean type data are different in C and C++.
Live Demo For C.
Example
#include<stdio.h> int main() { printf("Bool size: %d", sizeof(1 == 1)); }
Output(C)
Bool size: 4
Live Demo For C++.
Example
#include<stdio.h> int main() { printf("Bool size: %d", sizeof(1 == 1)); }
Output(C++)
Bool size: 1
- Related Articles
- Write a program that produces different results in C and C++ programming
- Write a C program that won’t compile in C++
- Different substrings in a string that start and end with given strings in C++ Program
- Write a program that does not terminate when Ctrl+C is pressed in C
- Write a C program that does not terminate when Ctrl+C is pressed
- Write program to shutdown a system in C/C++
- Write a C# program to find GCD and LCM?
- Write a C++ Program without Semicolons?
- C program to print characters and strings in different formats.
- Write a C program to print all files and folders.
- C++ program to check xor game results 0 or not
- Write a program in C++ to count the Number of substrings that starts with ‘1’ and ends with ‘1’
- Write a C program to reverse array
- C program that won’t compile in C++
- C# Program to write a number in hexadecimal format

Advertisements