Data Types in C 1

Data Types in C++

As mentioned earlier, a variable can only store data of a specific type. This means the value assigned to a variable must match its data type. But what exactly is a type? A type defines what kind of data a variable can hold. For example:

int cubes;

Here, cubes is the variable name, the semicolon (;) marks the end of the statement, and int is the data type of the variable.

Below is a table that provides information about the fundamental C++ data types.

C++ Variable Types

Data TypeMinimum ValueMaximum ValueMemory (Bytes)
char-1281271
short -32,76832,7672
int -2147483648 21474836474 (or 2 in older compilers)
long-9223372036854775808 92233720368547758078
float1.2 × 10⁻³⁸3.4 × 10³⁸4
double2.2 × 10⁻³⁰⁸1.8 × 10³⁰⁸8
long doubleVaries by compiler (e.g., 80-bit precision on x86 architectures)Varies by compiler (e.g., 80-bit precision on x86 architectures)8, 10, 12, or 16

These data types allow you to store both positive (+) and negative (-) values. Therefore, they are called signed data types. However, if you want your variables to store only positive values, you can use Unsigned Data Types, which also modify the range of values they can hold by effectively doubling the upper limit of their corresponding signed types. For example, unsigned short can store values from 0 to 65,535, whereas a signed short can store -32,768 to 32,767.”

Below is a table for Unsigned Data Types.

C++ Unsigned Data Types

Data TypeMinimum ValueMaximum ValueMemory (Bytes)
unsigned char02551
unsigned short065,5352
unsigned int04,294,967,2954
unsigned long0184467440737095516158

With these data types, you can assign the desired value to a variable.

Note:  Sizes and ranges of data types are defined by the C++ standard (e.g., C++11, C++14, etc.) and may vary slightly across different implementations.

Let us examine each data type separately.

Integer Data Type:

An integer is a whole number, such as 11, -7, etc. Now, let’s look at the different integer data types available in C++.

Common Integer Data Types:

  • short (identical to short int)
  • int
  • long (identical to long int)

They are all integer data types; just range of minimum and maximum value and sizes are different, as mentioned above. Whenever you need to store an integer value in memory, you can use any of these data types.

Below are examples of how to declare variables using these integer types:

short small_number;
int  number;
long int large_number;

You can also write them as given below. It will compile without an error. But it is not recommended. Because in this case, int is simply redundant.

short int small_number;
int number;
long int large_number;

Character Data Type in C++:

In C++, the char data type is used to store characters. Each character value must be enclosed in single quotes ('A'). This distinguishes it from strings and ensures it occupies 1 byte of memory.

In C++, char is an integer type because it stores the ASCII value of a character in memory. This means that a char variable can store both characters (e.g., 'A') and their corresponding ASCII values (e.g., 65 for 'A'). For example:

char d = 65;
char b = 'A';

Here:

  • char d = 65; stores the numeric value to memory as it is. And when you print this char, compiler displays it as A.
  • char b = 'A'; The compiler converts it into its ASCII equivalent value (65) and stores this ASCII value in 1 byte of memory. And when you print this char, compiler displays it as A.
#include 

using namespace std;
int main() {
  char variable = 'Z';
  char a = 41;
  cout << "Output is:" << a << endl << "and " << variable;
  return 0;
}

Explanation:

  • The character variable variable stores 'Z'.
  • The character variable a stores 41, which is interpreted as an ASCII code.
  • The ASCII code of 41 corresponds to ) in the ASCII table.
  • The output will display the ASCII representation of 41 along with the character 'Z'.

Output:

Output is:)
and Z

This example demonstrates how char variables can hold both characters and numeric ASCII values.

Floating-Point Data Types:

Some values cannot be stored in integer types, such as numbers containing decimal points (e.g., 3.14, 11.561). These values require floating-point data types, such as:

  • float
  • double

The difference between float and double is that float uses less memory but has lower precision, while double provides higher precision at the cost of more memory.

Let’s see an example:

#include 
using namespace std; 

int main() {

    // Floating point variables
    float a = 3.14;
    double d = 5.69;
    double result;

    // Performing multiplication
    result = a * d;

    cout << "Answer is: " << result;
    return 0;
  
}

Output:

Answer is: 17.8666 

Note: You cannot use short or long as in the code below with a char or float. Because short and long are different data types from char and float data types and cannot be combined like this.

short char var = 'A'; //Will cause error:
short float = 0.6;

But you can combine long with double. And you cannot combine short with double.

long double = 0.6;   //Will compile without an error
short double = 0.6;   //Will cause error

No Comments

Leave a Reply