C++ Data Types
C++ Data Types
- In C++, data types are declarations for variables. This determines the type and size of data associated with variables. For example. int age=13;
- Here, age is a variable of type int. Meaning, the variable can only store integers of either 2 or 4 bytes.
C++ Fundamental Data Types
Data Type Meaning Size (in Bytes)
int Integer 2 or 4
float Floating-point 4
double Double Floating Point 8
char Character 1
wchar_t Wide Character 2
bool Boolean 1
void Empty 0
C++ int
- The int keyword is used to indicate integers.
- Its size is usually 4 bytes. Meaning, it can store values from -2147483648 to 2147483647.
- int salary = 85000;
C++ float and double
- float and double are used to store floating-point numbers (decimals and exponentials).
- The size of float is 4 bytes and the size of double is 8 bytes. Hence, double has two times the precision of float. To learn more, visit C++ float and double.
- float area = 64.74;
- double volume = 134.64354;
C++ Char
- Keyword char is used for characters.
- Its size is 1 byte.
- Characters in C++ are enclosed inside single quotes ' '.
- char test = 'h';
C++ wchar_t
- Wide character wchar_t is similar to the char data type, except its size is 2 bytes instead of 1.
- It is used to represent characters that require more memory to represent them than a single char.
- wchar_t test = L'ם'
C++ Bool
- The bool data type has one of two possible values: true or false.
- Booleans are used in conditional statements and loops (which we will learn in later chapters).
- bool cond = false;
C++ Void
- The void keyword indicates an absence of data. It means "nothing" or "no value".
- We will use void when we learn about functions and pointers.
C++ Types of Modifiers
- We can further modify some of the fundamental data types by using type modifiers. There are 4 type modifiers in C++. They are:
- signed
- unsigned
- short
- long
- We can modify the following data types with the above modifiers:
- int
- double
- char
long b = 4523232;
long int c = 2345342;
long double d = 233434.56343;
short d = 3434233; // Error! out of range
unsigned int a = -5; // Error! can only store positive numbers or 0
Comments
Post a Comment