
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
How to write a short literal in C++?
Here we will see how will be the short literal in C++. In C or C++, different types of data have different literals. These are listed below.
Sr.No | Datatypes & Literals |
---|---|
1 | int 5 |
2 | unsigned int 5U |
3 | Long 5L |
4 | long long 5LL |
5 | float 5.0f |
6 | double 5.0 |
7 | char ‘\5’ |
Now, there are int, long float, double etc, but no short is present. So we cannot use any literals for short type data. But we can solve this problem by explicit typecasting.
If we use the line like below, then it will be converted into short.
int x; x = (short) 5; //converted into short type data.
Example
#include <iostream> using namespace std; main() { int x; x = 65700; cout << "x is (as integer):" << x << endl; x = (short)65700; //will be rounded after 2-bytes cout << "x is (as short):" << x << endl; }
Output
x is (as integer):65700 x is (as short):164
- Related Articles
- Write a short note on short circuiting?
- How to define multiline String Literal in C#?
- Simple Ways to Write a Short Essay
- Write a short note about motion
- What is 0 (zero) - A decimal literal or An octal literal in C++
- What is a string literal in C++?
- Literal number suffixes in C#
- Raw string literal in C++
- Write a short note on nitrogen fixation?
- Write a short note on rainwater harvesting.
- C++ Program to Create a Dictionary with a Dictionary Literal
- Raw string literal in C++ program
- Integer literal in C/C++ (Prefixes and Suffixes)
- How to create a Struct Instance Using a Struct Literal in Golang?
- How to display a short quotation in HTML?

Advertisements