Home / code / cpp

C++ variables

Before you can use any variable you have to define the variable. This means : giving the variable a name and assign it a type (int, float, ...).This is usually done at the start of the program.
If you give the variable a value it is called initialization.

If you give a name to a variable, you have to follow 2 rules

  • A name can exists of small letters, capital letters, numbers and underscore (_)
  • A name can never start with a number

You can make a variable constant, so it can not be changed in the program. You have to but const if front when you initialize the variable. Example:

const double pi=3.14159265358979

INT variables

In this variable you can hide whole numbers (like -17, +23 and 0). int is short for integer.
The value of a int variable must be between -32 768 and +32 767. This is a very small range this comes because a integer must fit in 2 bytes.

You can define them one by one.

int a;
int b;
or all at once (same type) int a, b;

Unsigned int

A unsigned int can hold numbers between 0 and 65 535. Like integers they exists of 2 bytes.

long int

A long int (short long)can hold numbers between -2 147 483 648 and + 2 147 483 647.
So why shouldn't you always use the type long? First of all a long int is 4 bytes, second, there are many standard-function that work with int and not long int.

unsigned long int

This type can hold numbers between + and +4 294 967 295. It also occupies 4 bytes.

Float

Specially created for floating point numbers. In these variables you can store numbers between 3.4 x 10-38 and 3.4 x 10+38 (positive and negative). A float variable is 4 bytes big.
A float is written with a precision of 7 figures, this means that too many figures after the decimal point would disappear by rounding off, and that possibly the so called scientific notation, with the symbol e for the power of 10 is used.

C++ will use as long as possible the normal notation with cout, but if the number gets to small or to great it uses the scientific notation.

Normal decimal notation Scientific notation with the power of 10 Scientific notation with e
3400 3.4x103 3.4e+03
3400000 3.4x106 3.4e+06
34000000 3.4x107 3.4e+07
0.56 5.6x10-1 5.6e-01
0.000000056 5.6x10-8 5.6e-08
0.000000000056 5.6x10-11 5.6e-11

Double

The float numbers can be pretty big but the precision is limited to 7 figures. Sometime there are more then 7 figures displayed but the last figures are unreliable.
So it is safer to use the type double. These variables have a precision of 15 figures and a range between 1.7x10-308 to 1.7x10+308 (positive and negative).

Long Double

If you want even more you have long double. These variables have a range between 3.4x10-4932 and 3.4x10+4932 (positive and negative) and a precision of 19 figures.
C++ doesn't automatic shows the 19 figures, you have to make him show them with manipulators

Char

In a variable of the type char you can store 1 character, never more then 1. You have to but the characters between ''. (example variable='a'). The reason for this is that you have to make it clear to the compiler that it is a value and not a variable with the name a.

Type conversion

You can convert 1 type to the other. Sometimes this happens automatically:

#include <iostream.h>

void main()
{
  int number=3;
  float prize=2.75;
  double total=;

  total=number*prize;

  cout << "Total: f " << total;
  cin.get();
}
This code uses 3 different types: int, float and double. The int value of number is converted to float, before multiplying it with the float value of prize. The result is a float that is converted to a double value to be stored in total.

You can sort the number types from high to low priority:

intLow
long 
float 
double 
long doubleHigh
The rule is simple: the lower type is upgraded to the higher type.

The CAST

Sometimes the conversion doesn't happen automatically. You can force a conversion with the so called cast.
Example : if you divide with 2 int numbers.

3/4 gives 0 and not 0.75
If you still want 0.75, you can do this with a cast

#include <iostream.h>

void main()
{
  int up=3, down=4;
  double result1, result2;

  result1=up/down;
  result2=double( up )/down;

  cout << "Without cast" <> result1 << endl;
  cout << "With cast" <> result2 << endl;
  cin.get();
}

 

TOP

Latest script:

 

Books: