- C++ Programming Basics
- C++ Tutorial
- C++ Environment Setup
- C++ Character Set
- C++ Keywords
- C++ Identifiers
- C++ Constants
- C++ Punctuators
- C++ Program Structure
- C++ Basic Syntax
- C++ Comments
- C++ Basic Programs
- C++ Input Output Operator
- C++ Input Output Stream
- C++ Type & Variable
- C++ Data Types
- C++ Data Type Modifiers
- C++ Variables
- C++ Variable Types
- C++ Variable Scope
- C++ Storage Classes
- C++ Formatting Output
- C++ Operators
- C++ Operators
- C++ Type Conversion
- C++ Numbers
- C++ Assignment Operator
- C++ Shorthand
- C++ Flow of Control
- C++ Statements
- C++ Flow Control
- C++ Decision Making
- C++ if if-else if-else-if switch
- C++ Loops
- C++ for while do-while Loop
- C++ break continue goto
- C++ Standard Library
- C++ Standard Library
- C++ Header Files
- C++ Character String
- C++ Mathematical Functions
- C++ Functions
- C++ Functions
- C++ Function Types
- C++ Function Prototype
- C++ Function Call
- C++ Function Return
- C++ Friend Function
- C++ Scope Rules
- C++ Arrays & Strings
- C++ Arrays
- C++ One Dimensional Arrays
- C++ Two Dimensional Arrays
- C++ Strings
- C++ Data Structure
- C++ Data Structure
- C++ Access Structure Member
- C++ Nested Data Structure
- C++ Structure Array
- C++ Pass Structure to Function
- C++ typedef
- C++ #define
- C++ Programming Pointers
- C++ Pointers
- C++ Memory Map
- C++ Free Store
- C++ Declare Initialize Pointers
- C++ Dynamic Memory Allocation
- C++ Pointers & Arrays
- C++ Pointers & Const
- C++ Pointers & Functions
- C++ Pointers & Structures
- C++ Objects as Function Arguments
- C++ Pointers & Objects
- C++ References
- C++ File Handling
- C++ File Handling
- C++ File Streams
- C++ Data Files
- C++ Opening & Closing Files
- C++ Steps to Process Files
- C++ Change Stream Behaviour
- C++ Sequential I/O Operations
- C++ Detecting EOF
- C++ File Pointers Random Access
- C++ Binary Files Operations
- C++ Error Handling
- C++ Object Oriented
- C++ Object Oriented
- C++ Function Overloading
- C++ Classes & Objects
- C++ Constructors & Destructors
- C++ Inheritance
- C++ Encapsulation
- C++ Polymorphism
- C++ Data Abstraction
- C++ Interfaces
- C++ Programming Advance
- C++ Linked Lists
- C++ Stacks
- C++ Queues
- C++ Date Time
- C++ Preprocessors
- C++ Exception Handling
- C++ Namespaces
- C++ Dynamic Memory
- C++ Multithreading
- C++ Templates
- C++ Signal Handling
- C++ Web Programming
- C++ Programming Examples
- C++ Programming Examples
- C++ Programming Test
- C++ Programming Test
- Give Online Test
- All Test List
C++ Data Type Modifiers
Except type void, the basic data types may have various modifiers preceding them. You use a modifier to alter the meaning of the base type to fit various situations more precisely. The list of modifiers are as follows :
- signed
- unsigned
- long
- short
You can apply the modifiers signed, unsigned, short, and long to character and integer base types. However, you may also apply long to double.
Note - ANSI standard eliminates long float because it is nothing but double.
Computer memory is organized into units called bytes. A byte consists of 8-bits, where a bit is the smallest unit of memory. 1 bit can represent 2 unique combinations of binary numbers (0 and 1) ie., 21, 2 bits can represent 4 unique combinations of binary number (00, 01, 10, 11) i.e., 22 and so on. Thus, a byte (8 bits) can represent 28 = 256 unique number of combinations Two-byte unit can represent 65536 (216) different values, and a four-byte unit can represent 4,294,672,296 (232) different values.
Integer Type Modifiers
By using different number of bytes to store values, C++ offers three types of integers : short, int, and long that can represent up to three different integer
sizes. Each comes in both signed and unsigned versions. That gives you a choice of six different integer types. The sizes of these integer types can vary
across different computer systems, however, C++ offers a flexible standard with some guaranteed minimum sizes, which are as follows:
A short integer is at least two bytes.
An int integer is at least as big as short.
A long is at least four bytes and at least as big as int.
Note - short is short name for short int and long is short name for long int.
The prefix unsigned makes the integer type not to hold negative values. For instance, if short (2-byte long) represents the range -32768 to +32767, then the unsigned version can represent the range 0 to 65535. This has the advantage of increasing the largest value the variables can hold. Unsigned type are used for quantities that are never negative such as populations, sports scores, inventory counts etc.
The following table summarizes different integer types, their sizes and minimal range they can hold :
Type | Approximate Size (in byte) |
Minimal Range |
---|---|---|
short | 2 | -32768 to 32767 |
unsigned short | 2 | 0 to 65,535 |
signed short | 2 | same as short |
int | 2 | -32768 to 32767 |
unsigned int | 2 | 0 to 65,535 |
signed int | 2 | same as int |
long | 4 | -2,147,483,648 to 2,147,483,647 |
unsigned long | 4 | 0 to 4,294,967,295 |
signed long | 4 | same as long |
As obvious from the above table that short, int, and long are signed by default.
Character Type Modifiers
The char type is really another integer type (as inside memory it actually holds numbers i.e., equivalent codes of characters/symbols). It is guaranteed to be large enough to represent the entire range of basic symbols - all the letters, digits, punctuation and the like - for the target computer system. A single byte can represent the whole range of 256 known characters.
The char type can also be signed or unsigned. Unlike int, char is neither signed nor unsigned by default. The choice is left to the C++ implementation in order to allow the implementer to best fit the type to the hardware properties.
However, these distinctions are particularly important if you are using char as a numeric type. The unsigned char represents the range 0 to 256 and signed char represents the range -128 to 127.
The following table summarizes the character types and their minimal ranges :
Type | Approximate Size (in bytes) |
Minimal Range |
---|---|---|
char | 1 | -128 to 127 |
unsigned char | 1 | 0 to 255 |
signed char | 1 | same as char |
Floating-point Type Modifiers
C++ has three floating-point types: float, double and long double.
Note - Double is another floating-point type it is none other than long float.
These types are described in terms of the number of significant features they can represent and the minimum allowable range of exponents. The type float occupies 4 bytes of memory. Type double occupies 8 bytes, twice as much memory as type float and stores floating-point numbers with much larger range and precision. Floating-point type long double occupies 10 bytes and has only slightly greater range and precision than type double.
Following table summarises floating-point types and the minimal range of values they can hold :
Type | Approximate Size (in bytes) |
Minimal Range | Digits of Precision |
---|---|---|---|
float | 4 | 3.4 × 10-38 to 3.4 × 1038 - 1 | 7 |
double | 8 | 1.7 × 10-308 to 1.7 × 10308 - 1 | 15 |
long double | 10 | 3.4 × 10-4932 to 3.4 × 104932 - 1 | 19 |
Note - Some implementation may allow you to apply unsigned to the floating-point types (as in unsigned double). However, this reduces the portability of your code and is generally not recommended.
Note - One thing that you must ensure is not to put commas in numeric values.
Type Qualifiers in C++
The type qualifiers provide additional information about the variables they precede.
Qualifier | Meaning |
---|---|
const | Objects of type const cannot be changed by your program during execution |
restrict | A pointer qualified by restrict is initially the only means by which the object it points to can be accessed. Only C99 adds a new type qualifier called restrict. |
volatile | The modifier volatile tells the compiler that a variable's value may be changed in ways not explicitly specified by the program. |
Examples
Here are some C++ example programs listed, that you can go for:
- Print Table of Number
- Print Prime Numbers
- Add n Numbers
- Interchange Numbers
- Reverse Numbers
- Swap two Numbers
« Previous Tutorial Next Tutorial »