# C++ Data Types

In this tutorial, we will learn about basic data types such as int, float, char, etc. in C++ programming with the help of examples.

In C++, data types are declarations for variables. This determines the type and size of data associated with variables. For example,

```C++
int age = 13;
```

Here, <var>age</var> is a variable of type `int`. Meaning, the variable can only store integers of either 2 or 4 bytes.

<div about="/cpp-programming/data-types" class="node node-cpp-tutorial clearfix" id="bkmrk-" typeof="sioc:Item foaf:Document"><div about="/cpp-programming/data-types" class="node node-cpp-tutorial clearfix" typeof="sioc:Item foaf:Document"><div class="content">---

</div></div></div>## C++ Fundamental Data Types

The table below shows the fundamental data types, their meaning, and their sizes (in bytes):

<div about="/cpp-programming/data-types" class="node node-cpp-tutorial clearfix" id="bkmrk-data-type-meaning-si" typeof="sioc:Item foaf:Document"><div about="/cpp-programming/data-types" class="node node-cpp-tutorial clearfix" typeof="sioc:Item foaf:Document"><div class="content"><div class="table-responsive"><table border="0"><tbody><tr><th>Data Type</th><th>Meaning</th><th>Size (in Bytes)</th></tr><tr><td><code>int</code></td><td>Integer</td><td>2 or 4</td></tr><tr><td><code>float</code></td><td>Floating-point</td><td>4</td></tr><tr><td><code>double</code></td><td>Double Floating-point</td><td>8</td></tr><tr><td><code>char</code></td><td>Character</td><td>1</td></tr><tr><td><code>wchar&#95;t</code></td><td>Wide Character</td><td>2</td></tr><tr><td><code>bool</code></td><td>Boolean</td><td>1</td></tr><tr><td><code>void</code></td><td>Empty</td><td>0</td></tr></tbody></table>

</div></div></div></div>Now, let us discuss these fundamental data types in more detail.

<div about="/cpp-programming/data-types" class="node node-cpp-tutorial clearfix" id="bkmrk--0" typeof="sioc:Item foaf:Document"><div about="/cpp-programming/data-types" class="node node-cpp-tutorial clearfix" typeof="sioc:Item foaf:Document"><div class="content">---

</div></div></div>### 1. C++ int

<div about="/cpp-programming/data-types" class="node node-cpp-tutorial clearfix" id="bkmrk-the%C2%A0int%C2%A0keyword-is-u" typeof="sioc:Item foaf:Document"><div about="/cpp-programming/data-types" class="node node-cpp-tutorial clearfix" typeof="sioc:Item foaf:Document"><div about="/cpp-programming/data-types" class="node node-cpp-tutorial clearfix" typeof="sioc:Item foaf:Document"><div class="content">

- The `int` keyword is used to indicate integers.
- Its size is usually 4 bytes. Meaning, it can store values from **-2147483648 to 2147483647**.
- For example,

</div></div></div></div>

```C++
int salary = 85000;
```

<div about="/cpp-programming/data-types" class="node node-cpp-tutorial clearfix" id="bkmrk--1" typeof="sioc:Item foaf:Document"><div about="/cpp-programming/data-types" class="node node-cpp-tutorial clearfix" typeof="sioc:Item foaf:Document"><div class="content">---

</div></div></div>### 2. C++ float and double

<div about="/cpp-programming/data-types" class="node node-cpp-tutorial clearfix" id="bkmrk-float%C2%A0and%C2%A0double%C2%A0are" typeof="sioc:Item foaf:Document"><div about="/cpp-programming/data-types" class="node node-cpp-tutorial clearfix" typeof="sioc:Item foaf:Document"><div about="/cpp-programming/data-types" class="node node-cpp-tutorial clearfix" typeof="sioc:Item foaf:Document"><div class="content">

- `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.
- For example,

</div></div></div></div>

```C++
float area = 64.74;
double volume = 134.64534;
```

As mentioned above, these two data types are also used for exponentials. For example,

```C++
double distance = 45E12    // 45E12 is equal to 45*10^12
```

<div about="/cpp-programming/data-types" class="node node-cpp-tutorial clearfix" id="bkmrk--2" typeof="sioc:Item foaf:Document"><div about="/cpp-programming/data-types" class="node node-cpp-tutorial clearfix" typeof="sioc:Item foaf:Document"><div class="content">---

</div></div></div>### 3. C++ char

<div about="/cpp-programming/data-types" class="node node-cpp-tutorial clearfix" id="bkmrk-keyword%C2%A0char%C2%A0is-used" typeof="sioc:Item foaf:Document"><div about="/cpp-programming/data-types" class="node node-cpp-tutorial clearfix" typeof="sioc:Item foaf:Document"><div about="/cpp-programming/data-types" class="node node-cpp-tutorial clearfix" typeof="sioc:Item foaf:Document"><div class="content">

- Keyword `char` is used for characters.
- Its size is 1 byte.
- Characters in C++ are enclosed inside single quotes `' '`.
- For example,

</div></div></div></div>

```C++
char test = 'h';
```

**Note:** In C++, an integer value is stored in a `char` variable rather than the character itself. To learn more, visit [C++ characters](https://www.programiz.com/cpp-programming/char-type).

<div about="/cpp-programming/data-types" class="node node-cpp-tutorial clearfix" id="bkmrk--3" typeof="sioc:Item foaf:Document"><div about="/cpp-programming/data-types" class="node node-cpp-tutorial clearfix" typeof="sioc:Item foaf:Document"><div class="content">---

</div></div></div>### 4. C++ wchar\_t

<div about="/cpp-programming/data-types" class="node node-cpp-tutorial clearfix" id="bkmrk-wide-character%C2%A0wchar" typeof="sioc:Item foaf:Document"><div about="/cpp-programming/data-types" class="node node-cpp-tutorial clearfix" typeof="sioc:Item foaf:Document"><div about="/cpp-programming/data-types" class="node node-cpp-tutorial clearfix" typeof="sioc:Item foaf:Document"><div class="content">

- 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`.
- For example,

</div></div></div></div>

```C++
wchar_t test = L'ם'  // storing Hebrew character;
```

Notice the letter L before the quotation marks.

<div about="/cpp-programming/data-types" class="node node-cpp-tutorial clearfix" id="bkmrk--5" typeof="sioc:Item foaf:Document"><div about="/cpp-programming/data-types" class="node node-cpp-tutorial clearfix" typeof="sioc:Item foaf:Document"><div class="content"><div class="block-inject block-inject-1" id="bkmrk--12"><div class="pgAdWrapper"><div id="bkmrk--13"><div id="bkmrk--14"></div></div></div></div><div class="clearfix">  
</div></div></div></div>**Note:** There are also two other fixed-size character types <code>char16&#95;t</code> and <code>char32&#95;t</code> introduced in C++11.

<div about="/cpp-programming/data-types" class="node node-cpp-tutorial clearfix" id="bkmrk--6" typeof="sioc:Item foaf:Document"><div about="/cpp-programming/data-types" class="node node-cpp-tutorial clearfix" typeof="sioc:Item foaf:Document"><div class="content">---

</div></div></div>### 5. C++ bool

<div about="/cpp-programming/data-types" class="node node-cpp-tutorial clearfix" id="bkmrk-the%C2%A0bool%C2%A0data-type-h" typeof="sioc:Item foaf:Document"><div about="/cpp-programming/data-types" class="node node-cpp-tutorial clearfix" typeof="sioc:Item foaf:Document"><div about="/cpp-programming/data-types" class="node node-cpp-tutorial clearfix" typeof="sioc:Item foaf:Document"><div class="content">

- 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).
- For example,

</div></div></div></div>

```C++
bool cond = false;
```

<div about="/cpp-programming/data-types" class="node node-cpp-tutorial clearfix" id="bkmrk--7" typeof="sioc:Item foaf:Document"><div about="/cpp-programming/data-types" class="node node-cpp-tutorial clearfix" typeof="sioc:Item foaf:Document"><div class="content"><iframe allowfullscreen="allowfullscreen" height="500px" src="https://www.youtube.com/embed/zB9RI8_wExo" width="100%"></iframe>

---

</div></div></div>### 6. C++ void

<div about="/cpp-programming/data-types" class="node node-cpp-tutorial clearfix" id="bkmrk-the%C2%A0void%C2%A0keyword-ind" typeof="sioc:Item foaf:Document"><div about="/cpp-programming/data-types" class="node node-cpp-tutorial clearfix" typeof="sioc:Item foaf:Document"><div class="content">

- 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.

</div></div></div>**Note:** We cannot declare variables of the <code>void</code> type.

<div about="/cpp-programming/data-types" class="node node-cpp-tutorial clearfix" id="bkmrk--8" typeof="sioc:Item foaf:Document"><div about="/cpp-programming/data-types" class="node node-cpp-tutorial clearfix" typeof="sioc:Item foaf:Document"><div class="content">---

</div></div></div>#### Putting it all together

Run the following repl to see its results. Looking at the `main.cpp` file, a range of variables of different data types are declared at the top.

`cout` is used with the `+` operator to output the sum of two integers. `cout` is also used to display a message that includes the contents of the string variables, and lastly an if statement is used to present a message based on which letter is stored in the char variable.

##### Try doing each of the following, running the between each change:

1. Change the string variables to reflect your name
2. Change the values of the integers
3. Change the operator used in the output to `*`
4. Change the letter stored in the char variable to a grade you would like to receive
5. Change the integer variables to float, and assign decimal values.

<iframe frameborder="0" height="500px" src="https://replit.com/@ChesterWhitwell/C-variables-and-operators?lite=true" width="100%"></iframe>

<div class="content" id="bkmrk--10">---

</div>## C++ Type Modifiers

We can further modify some of the fundamental data types by using type modifiers. There are 4 type modifiers in C++. They are:

<div about="/cpp-programming/data-types" class="node node-cpp-tutorial clearfix" id="bkmrk-signed-unsigned-shor" typeof="sioc:Item foaf:Document"><div about="/cpp-programming/data-types" class="node node-cpp-tutorial clearfix" typeof="sioc:Item foaf:Document"><div class="content">

1. `signed`
2. `unsigned`
3. `short`
4. `long`

</div></div></div>We can modify the following data types with the above modifiers:

<div about="/cpp-programming/data-types" class="node node-cpp-tutorial clearfix" id="bkmrk-int-double-char" typeof="sioc:Item foaf:Document"><div about="/cpp-programming/data-types" class="node node-cpp-tutorial clearfix" typeof="sioc:Item foaf:Document"><div class="content">

- `int`
- `double`
- `char`

---

</div></div></div>### C++ Modified Data Types List

<div about="/cpp-programming/data-types" class="node node-cpp-tutorial clearfix" id="bkmrk-data-type-size-%28in-b" typeof="sioc:Item foaf:Document"><div about="/cpp-programming/data-types" class="node node-cpp-tutorial clearfix" typeof="sioc:Item foaf:Document"><div class="content"><div class="table-responsive"><table border="0"><tbody><tr><th>Data Type</th><th>Size (in Bytes)</th><th>Meaning</th></tr><tr><td><code>signed int</code></td><td>4</td><td>used for integers (equivalent to <code>int</code>)</td></tr><tr><td><code>unsigned int</code></td><td>4</td><td>can only store positive integers</td></tr><tr><td><code>short</code></td><td>2</td><td>used for small integers (range **-32768 to 32767**)</td></tr><tr><td><code>unsigned short</code></td><td>2</td><td>used for small positive integers (range **0 to 65,535**)</td></tr><tr><td><code>long</code></td><td>at least 4</td><td>used for large integers (equivalent to <code>long int</code>)</td></tr><tr><td><code>unsigned long</code></td><td>4</td><td>used for large positive integers or 0 (equivalent to <code>unsigned</code> <code>long int</code>)</td></tr><tr><td><code>long long</code></td><td>8</td><td>used for very large integers (equivalent to <code>long long int</code>).</td></tr><tr><td><code>unsigned long long</code></td><td>8</td><td>used for very large positive integers or 0 (equivalent to <code>unsigned long long int</code>)</td></tr><tr><td><code>long double</code></td><td>12</td><td>used for large floating-point numbers</td></tr><tr><td><code>signed char</code></td><td>1</td><td>used for characters (guaranteed range **-127 to 127**)</td></tr><tr><td><code>unsigned char</code></td><td>1</td><td>used for characters (range **0 to 255**)</td></tr></tbody></table>

</div></div></div></div>Let's see a few examples.

```C++
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
```

<div about="/cpp-programming/data-types" class="node node-cpp-tutorial clearfix" id="bkmrk--11" typeof="sioc:Item foaf:Document"><div about="/cpp-programming/data-types" class="node node-cpp-tutorial clearfix" typeof="sioc:Item foaf:Document"><div class="content">---

</div></div></div>## Derived Data Types

Data types that are derived from fundamental data types are derived types. For example: arrays, pointers, function types, structures, etc.

We will learn about these derived data types in later tutorials.