C++ Variables and Data Types

By , last updated November 8, 2017

What is data type?
In programming languages, data type is a classification that defines which type of value a variable can contain and what type of operations (mathematical, relational or logical operations) can be applied to it without causing any error or unwanted side effects.

Data types in C++ are classified into following two categories:

  • Primitive or Fundamental Data Types
  • Abstract or User Defined Data Types

Primitive Data types

These are built-in data types and can be directly used to declare variables. They are basic or fundamental data types that are not carved out of any other data types and can represent one single value only. Following are C++ Built-in Data Types.

Data Type Signedness Modifier Size Modifier Size *)
Integer signed
unsigned
short
int
long
long long
2 bytes
2-4 bytes
4-8 bytes
8 bytes
Character signed
unsigned
char
wchar_t
char16_t
char32_t
1 byte
2-4 bytes
2 bytes
4 bytes
Floating point float
double
long double
4 bytes
8 bytes
10 bytes
Boolean bool 1 byte
Void empty

*) The size of the data type may differ on 32 and 64-bit systems. The sizes listed here are the most used sizes. The only requirement is that short is smaller or equal to int, int is smaller or equal to long, and long is smaller or equal to long long.

Integer

Keyword for integer data types is int, this data type is used to store integer values. Integers typically require 4 bytes of memory space and the range of values it can contain is -2147483648 to 2147483647.

There are three size modifiers that may go along with an integer: short, long and long long. The keyword “int” may be omitted if any of the size modifiers are used.

Here are some examples of integer variables in a simple C++ program:

#include <iostream>
using namespace std;
int main()
{
	int a = 123456789;
	int b = 123456789;
	int c = a + b;

	cout << "a + b = " << c;
	cout << "\n\n"; //2 new lines

	short d = 1234;
	short e = 1234;
	short f = d + e;

	cout << "d + e = " << f;
	cout << "\n\n";

	short d2 = 123456789;
	short e2 = 123456789;
	short f2 = d2 + e2;

	cout << "d2 + e2 = " << f2 << " <- wrong value, integer too big";
	cout << "\n\n";

	long g = 123456789;
	long h = 123456789;
	long i = g + h;

	cout << "g + h = " << i;
	cout << "\n\n";

	long g2 = 123456789123456789;
	long h2 = 123456789123456789;
	long i2 = g2 + h2;

	cout << "g2 + h2 = " << i2 << " <- wrong value, integer too big";
	cout << "\n\n";

	long long j = 123456789123456789;
	long long k = 123456789123456789;
	long long l = j + k;

	cout << "j + k = " << l;
	cout << "\n\n";
}

The output shows that operations with integers where the data type is chosen correct in comparison to the integer size will produce the correct result. Integers where the value of a variable is too big for the chosen data type will produce totally incorrect results. Integer with a long size modifier can’t hold 18 digits. Integer with a short size modifier can’t hold 9 digits.

Character

This data type is used for storing characters, which may be any single character. It may also include a letter, a digit, a punctuation mark, or space. The keyword for this data type is char. It typically requires 1 byte of memory space and ranges from -128 to 127 or 0 to 255.

Wide Character is also used to store a character, but it has a larger size than char data type. A wide character is specified by the keyword wchar_t and has a size of 2 or 4 bytes.

Floating point

This data type is used for storing single precision floating point values or decimal values. Its Keyword is float and requires from 4 and up to 10 bytes of memory space for extended precision floating point types like long double.

Floating point types support special values like #INF, #IND, #NaN and negative zeros. These are tricky to work with and one should be aware of certain error conditions that may arise from using floating points.

double floating point is used for storing double precision floating point values or decimal values. It typically requires 64-bits of memory storage. Not all compilers support long double will give you the same precision as double.

Here’s a simple C++ program that stores and prints the value of the number PI with many digits in different floating point variables:

#include <iostream>
#include <iostream>

using namespace std;

# define M_PI 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651 

int main()
{
	float pi  = M_PI;
	double dpi = M_PI;
	long double ldpi = M_PI;
	cout.precision(1000);
	cout << pi << "\n";
	cout << dpi << "\n";
	cout << ldpi << "\n";
}

Run this sizeof code to check if your computer supports long double:

printf("%d\n", sizeof(double)); // some compilers print 8
printf("%d\n", sizeof(long double)); // some compilers print 16

Ours didn’t support long doubles and the print was the following:

Boolean

This is used for storing Boolean or logical values. Any variable with this data type can store either true or false. Its Keyword is bool. The size of the Boolean data type is 1 byte.

Here’s a simple C++ bool type example:

#include <iostream>
using namespace std;

int main()
{
	bool isTrue = true;
	bool isFalse = false;

	if (isTrue) {
		cout << "True";
	}
	else if (isFalse) {
		cout << "False";
	}
}

The program will print “True”.

Valueless

Specified by Void keyword, these data types are without any value. This data type is used with functions which does not return any value.

Here’s a C++ example, that prints a string “Hello, World!” from a void function that doesn’t return anything:

#include <iostream>
#include <string>
using namespace std;

void printValue(string val) {
	cout << val;
}

int main()
{
	printValue("Hello, World!");

	return 0;
}

The output will be “Hello, World!”.

Abstract or User Defined Data Types

C++ also allows programmers to create data types of their own choice, such data types fall in the category of Abstract or user defined data type. Example of this data type is defining a class or structure.

Keyword typedef is used to create our own user defined data type with the following structure:

typedef <existing type with modifiers> <name>; 

Here’s an example of creating a user defined data type using keyword “typedef”:

typedef char value_type;
typedef char& reference;
typedef char* pointer;
typedef std::ptrdiff_t difference_type;

Typedef is a useful feature when creating templates. Take a look at this Variadic Template Example where we create a template class wrapper around a structure.

If you are not stuck with an older compiler, using will do the same as typedef, and some more.

using have the arguments in opposite order of typedef.

The above example will be:

using value_type = value_type;
using reference = char&;
using pointer = char*;
using difference_type = std::ptrdiff_t;

Data Type Modifiers

A modifier is used to alter the meaning of built in data types and expand them to a much larger set. Following are four data type modifiers in C++. They are divided in signedness modifiers and size modifiers.

All modifiers can be mixed in any order in a variable, but only one of each group can be present in a type name.

Here’s an example of the same Integer variable:

unsigned short int foo;
unsigned short foo;
short unsigned int foo;
short unsigned foo;

Finding Size of Data Types

There are few data types whose values vary from system to system. You can find the size of a data type on your machine using the ‘sizeof()’ operator.

#include <iostream>

int main()
{
	std::cout << "Size of int = " << sizeof(int) << "\n";
	std::cout << "Size of long = " << sizeof(long) << "\n";
	std::cout << "Size of char = " << sizeof(char) << "\n";
	std::cout << "Size of float = " << sizeof(float) << "\n";
	std::cout << "Size of double = " << sizeof(double) << "\n";
	std::cout << "Size of long double = " << sizeof(long double) << "\n";
	std::cout << "Size of boolean = " << sizeof(bool) << "\n";

	return 0;
}

Senior Software Engineer developing all kinds of stuff.

Comments

Be the first to comment.

Leave a Reply


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*