You are here: Home » Content » Data Types and Operators

Data Types

A data type is the specific category of information that a variable contains.
There are three basic data types used in C++: integers, floating point numbers and characters.
Integers
An integer is a positive or negative number with no decimal places.
- 259 -13 0 200
Floating Point Numbers
A floating point number contains decimal places or is written using exponential notations.
-6.16 -4.4 2.7541 10.5
Exponential notation, or scientific notation is a way of writing a very large numbers or numbers with many decimal places using a shortened format.
2.0e11 means 2*1011
C++ supports three different kinds of floating-point numbers:
  • float (i.e. single precision numbers),
  • double (i.e. double precision numbers)
  • long double.
A double precision floating-point number can contain up to 15 significant digits.
The Character Data Type
To store text, you use the character data type. To store one character in a variable, you use the char keyword and place the character in single quotation marks.
Example:
char cLetter = ‘A’;

Escape Sequence

The combination of a backlash (\) and a special character is called an escape sequence. When this character is placed directly in front of a select group of character, it tells the compiler to escape from the way these characters would normally be interpreted.
Examples:
\n : move to the next line
\t : move to the next tab
The bool Data Type
The C++ bool type can have two states expressed by the built-in constants true (which converts to an integral one) and false (which converts to an integral zero). All three names are keywords. This data type is most useful when a program must examine a specific condition and, as a result of the condition being either true or false, take a prescribed course of action.

Arithmetic Operators

Most programs perform arithmetic calculations. Arithmetic operators are used to perform mathematical calculations, such as addition, subtraction, multiplication, and division in C++.
Figure 1: Arithmetic operators
A simple arithmetic expression consists of an arithmetic operator connecting two operands in the form:
operand operator operand
Examples:
3 + 7
18 – 3
12.62 + 9.8
12.6/2.0

Example

#include <iostream.h>
int main()
{
cout << "15.0 plus 2.0 equals " << (15.0 + 2.0) << '\n'
<< "15.0 minus 2.0 equals " << (15.0 - 2.0) << '\n'
<< "15.0 times 2.0 equals " << (15.0 * 2.0) << '\n'
<< "15.0 divided by 2.0 equals " << (15.0 / 2.0) << '\n';
return 0;
}
The output of the above program:
15.0 plus 2.0 equals 17
15.0 minus 2.0 equals 13
15.0 times 2.0 equals 30
15.0 divided by 2.0 equals 7.5

Integer Division

The division of two integers yields integer result. Thus the value of 15/2 is 7.
Modulus % operator produces the remainder of an integer division.
Example:
9%4 is 1
17%3 is 2
14%2 is 0

Operator Precedence and Associativity

Expressions containing multiple operators are evaluated by the priority, or precedence, of the operators.
Operator precedence defines the order in which an expression evaluates when several different operators are present. C++ have specific rules to determine the order of evaluation. The easiest to remember is that multiplication and division happen before addition and subtraction.
The following table lists both precedence and associativity of the operators.
Figure 2: Associativity of the operators
Example: Let us use the precedence rules to evaluate an expression containing operators of different precedence, such as 8 + 5*7%2*4. Because the multiplication and modulus operators have a higher precedence than the addition operator, these two operations are evaluated first (P2), using their left-to-right associativity, before the addition is evaluated (P3). Thus, the complete expression is evaluated as:
Figure 3: Expression evaluation process

Expression Types

An expression is any combination of operators and operands that can be evaluated to yield a value. An expression that contains only integer values as operands is called an integer expression, and the result of the expression is an integer value. Similarly, an expression containing only floating-point values (single and double precision) as operands is called a floating-point expression, and the result of the expression is a floating point value (the term real expression is also used).

Comments, questions, feedback, criticisms?

Send feedback