Sage-Code Laboratory
index<--

C++ Data Types

C++ is a strongly type compiled language. It means we have to specify data type for every variable, parameter or constant we declare. This enable performance optimizations and argument based dispatch. We asume you know nothing about data, but maybe you do. In hardware data is ultimately a set of 0 and 1, stored in memory as electrical, optical or magnetical charge. In Computer Science data takes more abstract forms that we will explain in this page.

Declaration

To make a C++ program you must declare names or identifiers for things that can be constants, variables, functions or data types. C++ has no "define" keyword: instead, a statement beginning with the name of a type. There is no "function" keyword: instead, a function is indicated by the parentheses following the function name. In parentheses you can enumerate formal parameters.

Primitive Types:

To understand C++ code you must grasp idea of data type.  You can not declare anything without using a data type. However you can define your own types using keyword: typedef. C++ has very few basic data types enumerated below:

Type Name Description Size printf() placeholder
char character 1 byte %c
int default integer 4 bytes %d
short int short integer 2 bytes %hd
long int long integer 8 bytes %li
unsigned int unsigned integer 8 bytes %u
float single precision floating point 4 bytes %f
double double precision floating point 8 bytes %lf

To detect the real size of the data type you can use function: sizeof()

Example:

#include <stdio.h>      
int main() {
    short a;
    long b;
    double d;
    printf("size of short = %d bytes\n", sizeof(a));
    printf("size of long = %d bytes\n", sizeof(b));
    printf("size of long double= %d bytes\n", sizeof(d));
    return 0;
}

Note: You can use following modifiers for integer type:

Derived types:

You can create new data types from basic types using a derived data types. There are different methods of derivation. One method is to group multiple elements into data collection or structure. These are called improperly user defined data types.

I will present derived types in future articles.

Variables

Next, you will learn about variables and rules for naming a variable.  A variable is like a labeled container or storage area in computer memory used to hold data for a short period of time. Variable names are symbolic representation of a memory location. Content of this location can be changed.

Example:

//declare variable ch with initial value 'a'
char ch = 'a';

//alter variable ch content using operator '='
ch = 'l';

Notes:

  1. A variable name can have only Latin letters, digits and underscore,
  2. The first letter of a variable should be either a letter or an underscore,
  3. Identifier name in C++ should not be longer than 31 characters.

Constants

You can define variables that are immutable. These area actually called constants. 

Example:

const double PI = 3.14; //protected 
PI = 2.9; //Error: PI is a constant

Literals

Now is time to understand something more. C++ source code can contain numbers and characters after the symbol "=", for example: 3.14.  These elements of the language are also constants, but are different kind of constants named Literals. The literals can be changed only if you change the source code.

Literal examples:

Escape sequence:

Some characters are not printable. For representing these characters in string literals you can use escape notation. Use symbol "\" follow by a letter. This will be replaced by the compiler with the proper code point.

\b Backspace
\f Form feed
\n Newline
\r Return
\t Horizontal tab
\v Vertical tab
\\ Backslash
\’ Single quotation mark
\" Double quotation mark
\? Question mark
\0 Null character

Example:

"First line \nSecond line." 

Read next: Control Flow