C++ Programming

C++ Programming

Data Types and Variables


In any language, there are some fundamentals you need to know before you can write even the most elementary programs. This module introduces two such fundamentals; Data Types and Variables.

C++ provides a predefined set of data types for handling its users. The data types can either be fundamental or derived.

Fundamental Data Types

There are five fundamental data types:

  • char
  • int
  • float
  • double
  • void

Note : C++ is case sensitive which means that “Y” and “y” will be treated differently by the C++ compiler and these data types should be written in lowercase characters

Integer Data Type

Integers are whole numbers with no fractional parts and are represented in C++ by int data type. Integers can be positive or negative values. Syntax to declare an integer data type:

int variable_name= value; 

Ex: int a= -5; 

Character Data Type

Characters are represented in C++ by char data type. It is used to store characters but there’s a twist. The characters are stored in the ASCⅡ format. Syntax to declare a character data type:

char variable_name= ‘character’; 

Ex: char a='X'; 

Before we move ahead, let’s discuss about ASCⅡ code.

ASCⅡ Code abbreviated from American Standard Code for Information Interchange, is a widely used alphanumeric code to represent text in computers and other electronic devices. The ASCⅡ Code pronounced as “askee” is used to express alphabets, digits and other special characters in the form of numbers. Which then is converted into a 7-bit binary code ( in the form of 1s and 0s ) to be understood by the C++ compiler. Here’s an ASCⅡ code table as a reference

ASCII Code 1ASCII Code 2ASCII Code 3

No need to get overwhelmed by the enormous table. You don’t have to study the entire table, this table should be used as a reference only. Moreover, a char data type is often said to be an integer type because of the fact that characters are stored an ASCⅡ value which are basically integers. 

Decimal Data Type

Decimals are represented in C++ by float data type. An identifier declared as float becomes a floating-point variable and can hold floating-point numbers. Syntax to declare a decimal data type:

float variable_name= decimal value; 

Ex: float a= 7.4; 

Double Decimal Data Type

Double decimals are represented in C++ by double data type. It is used to handle floating-point numbers but is treated as a distinct data type because it occupies twice as much memory as float data type and stores floating-point numbers with a much larger range and precision. This data type is an alternative when the float is too small or insufficiently precise to be used. Syntax to declare a double decimal data type:

double variable_name= large decimal value

Ex: double pi= 3.14159; 

Null Data Type

Null data type are represented in C++ by void data type. It is used as the return type for functions that do not return a value. This is the reason why we use void with the main( ) function. The uses of void will be discussed in the subsequent modules.

Derived Data Types

Derived data types are the data types that are derived from the fundamental data types. C++ has the following derived data types:

  • Arrays
  • Functions
  • Pointers
  • Reference
  • Constants

These data types are broad topics and also require pre-linguistic knowledge. So I’m going to create a whole chapter based on those topics in the near future.


A variable is a name given to a memory location to store data, whose values can be manipulated during program run. Syntax to declare a variable:

data type variable_name=value; 

The Weekly Program

Now let’s create a program to display the ASCⅡ code of a character and vice versa.

void main( )
char ch='A';
int num=ch;                                                                    //ASCII value of ‘A’ is assigned to num
cout<<"Let's learn ASCII Code\n";
cout<<"The ASCII code for "<<ch<<" is "<<num<<"\n";

Execution of the Program

Now Compile the program. If the compiler shows errors then check my program and resolve it. Then Run the program. The output of this program is quite self-explanatory. Here’s the output:

Now it’s time to do it yourself and feel free to mess around with the program by changing “A” into other characters in the table. Then verify the answer with the table. Don’t forget to save your program.

That’s it for today, see you next week!!!