Beginning C++: Primitive Types and Identifiers

Home / Beginning C++: Primitive Types and Identifiers

Beginning C++: Primitive Types and Identifiers

December 3, 2015 | Article | No Comments

What is Identifiers?

Literally, identifiers are tokens / symbols used to identify objects declared in program. Compiler knows object through identifier. Identifier can be a variable name, constants, functions, classes, template, even namespace.

In this article we will discuss identifiers in the smaller scope: variables and constants.

Identifier act as variable (or constant) is a value-container. Both of them can save some values but they are differ in the term of how they save value. A constant can save value but it can only assigned once and the value cannot be modified at runtime. While a variable can save value and the value can be modified or changed at runtime.

Let’s save it up for later and see this little analogy:

Imagine variable and constant as a bottle. We know that a bottle is a container which can contain / save something (especially liquid). Now, the bottle has many types / kinds, such as: soysauce bottle, water bottle, kerosene bottle, gas bottle, etc. Each bottle can save specific type liquid. For example a water bottle can only save water. Imagine there is a mineral-water bottle but it contains kerosene. It’s not suitable and maybe it can bring harm later (if someone think it as a plain water and drink them, you can imagine what will come next).

From the same perspective, variables and constant do so. A variables can save a value. The values can vary but the variable can only hold one specific type of value. We can only set what kind of value the bottle can hold only at declaration. A variable can be “refilled” with another value as long as the value has same data type while a constant is cannot be refilled with another value.

Primitive Data Types

As discussed, either variables or constant can save value. But they can only save value with the same type as they are declared before. The set of values of data which variable can sabe is referred as data types. There are two types of data types: a primitive data types and user-defined data types. For this article we will focus on primitive data types.

What is primitive data type? It is a built in / native data type on C++ programming language. So far, there are four primitive data types supported on C++: integer number, real number, character, boolean. Advancing to next level, we can create a new data type using these primitive data types (let say the primitive data types are the building block to build more complex one) through enumeration process, struct, and class.

Now, how can we declare a variable?

A syntax to declare a variable is as follow:

data_type variable_name;

A data_type is a data type. It can be user-defined data type or primitive data types. While the variable_name is a variable name, an identifier.

How about a constant?

Constant declaration is similiar to variable declaration, except at the declaration the constant must be initialized with some value and preceded by keyword const.

const data_type variable_name = value;

Now let’s write the actual code with variable and constant declaration

#include <iostream>
using namespace std;

int main() {
	// declaring variables
	int i;
	float f;
	char c;
	bool b;

	// declaring constants
	const int j = 10;
	const float k = 3.14;
	const char l = 'A';
	const bool m = true;

	return 0;

From codes above, we have four variables: an integer variable named i, a real-value variable name f, a character variable name c, a boolean variable name b. We also have four constant: an integer constant namej, a real-value constant name k, a character constant name l, a bool constant name m.

We can’t write identifier arbitrary. There are some rules to naming an identifier. Here are the rules for naming identifier:

  • Identifier can’t be chosen from C++ reserved keyword. C++ has reserved keywords such as: for, do, while, int, float, return, etc and pick them for identifier name is strictly forbidden.
  • Identifier name is case sensitive. When you say aVariable and AVARIABLE, C++ distinguish them because they have different case although they are consist of same word.
  • Identifier name must be unique. You cannot declare two variable with totally same name even if the identifier has different data type.
  • Identifier name consists of alphabet, number, or underscore. Any character beside that is considered as invalid name. No symbolic character such as: #, @, ?, !, $, as identifier.
  • Identifier name cannot be preceded by number. Identifier can only preceded by underscore (_) and alphabets. Number can be used as identifier name, but it cannot be placed as first character.
  • Identifier cannot has space.

The Identifier Scope

The lifetime of an identifier, or in some terms the scope where an identifier can be resolved. There are two scope: global and local. This is applied to both variables and constants.

A global variable or constant, is a variable or constant declared outside of any blocks of code. Simply we can say a global variable is placed outside of any function. A global identifier is recognized in whole source code.

A Local variable or constant, is a variable or constant declared inside of a block of code. Simply we can say a local variable is place inside of a function, branching code, looping code, etc. This identifier is recognized only at declared blocks.

Outside from two identifier types, a variable can also be classified as static variable. A static variable is a variable which occupy computer memory permanently (until the program is unloaded). It means the last variable value will be kept. To declare a static variable we can write as:

static data_type identifier_name;

Similiar to normal variable, static variable can be categorized as local-static variable and global-static variable.

Another variable type we have are register variable. A register variable will be stored on CPU register. Register variable provide faster way to store a value. No need to access memory. To declare a register variable, one must do:

register data_type variable_name;

More into Data Types

We have discussed a little thing about data type at above. Now Let’s dive deeper into data types. As stated before, C++ has four built-in primitive data types: integer number, real number, character, boolean. We will cover each category. We will refer to x86 machine.

Integer Numbers

Integer numbers, as hinted by the name, is a set of number of integers. An integer is a whole number (not a fractional number) that can be positive, negative, or zero. Data types for this category are:

Data Type Size (bits) Range
int 16 or 32 -32,768 to -32,767 or
-2,147,483,648, to 2,147,483,647
unsigned int 16 or 32 0 to 65,535 or
0 to 4,294,967,295
signed int 16 or 32 similiar to int
short int 16 -32,768 to 32,767
unsigned short int 16 0 to 65,535
signed short int 16 similiar to short int
long int 32 -2,147,483,648, to 2,147,483,647
signed long int 32 similiar to long int
unsigned long int 32 0 to 4,294,967,295

Real Numbers

Real numbers are number for real-value number. Data types for this category are:

Data Type Size (bits) Range Precision
float 32 1.2E-38 to 3.4E+38 6 digits
double 64 2.3E-308 to 1.7E+308 15 digits
long double 80 3.4E-4932 to 1.1E+4932 19 digits

Character (and string)

Character are data type used for representing a character on computer. Character is based on ASCII. Inside a computer, a character is stored as number representation but it would be presented as a character. A series of character can form a string. A string is defined as sequence of characters and terminated by NULL character. This category has following data types:

Data Type Size (bits) Range
char 8 -128 to 127 or 0 to 255
unsigned char 8 0 to 255
signed char 8 -128 to 127

A string in other hand is formed by series of character. For example, if a single character is ‘A’, a string can be something like “Hello world” or “Learning C++ with Xathrya”.


Often said as logic data type. It has only one data type, bool which has 2 different value: false (can be represented as 0) and true (can be represented as any number except 0). This data type mainly represent condition of comparison either by operator equality, inequality, greater, less.


About Author

about author


A man who is obsessed to low level technology.

Leave a Reply

Your email address will not be published. Required fields are marked *

Social media & sharing icons powered by UltimatelySocial