Every literal (constant) in C/C++ will have a type information associated with it.
In both C and C++, numeric literals (e.g. 10) will have int as their type. It means sizeof(10) and sizeof(int) will return same value.
However, character literals (e.g. ‘V’) will have different types, sizeof(‘V’) returns different values in C and C++. In C, a character literal is treated as int type where as in C++, a character literal is treated as char type (sizeof(‘V’) and sizeof(char) are same in C++ but not in C).
Result of above program:
C result – sizeof(‘V’) = 4 sizeof(char) = 1
C++ result – sizeof(‘V’) = 1 sizeof(char) = 1
Such behaviour is required in C++ to support function overloading. An example will make it more clear. Predict the output of the following C++ program.
The compiler must call
since ‘V’ type is char.
Article contribution by Venki and Geeksforgeeks team.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.
- User Defined Literals in C++
- What happen if we concatenate two string literals in C++?
- Types of Literals in C/C++ with Examples
- Data type of character constants in C and C++
- Check input character is alphabet, digit or special character
- Function overloading and return type
- Data Type Ranges and their macros in C++
- Conversion of Struct data type to Hex String and vice versa
- Comparison of boolean data type in C++ and Java
- Is there any need of "long" data type in C and C++?
- Catch block and type conversion in C++
- Type Inference in C++ (auto and decltype)
- Can a C++ class have an object of self type?
- RTTI (Run-time type Information) in C++
- Type of 'this' pointer in C++
- const_cast in C++ | Type Casting operators
- Array Type Manipulation in C++
- Bool data type in C++
- C++ set for user define data type
- What is the size_t data type in C?