Programming:C plus plus/Data Types (Redirected from Programming:C plus plus Data Types)
Data Types
Primitive Data Types
The primitive data types are simple data types that store a single value. The primitive data types include: char, int, float, and double. For more information on the size of the different data types, see C++ Data Types.
There are several modifiers that can be applied to data types to change the range of numbers they can represent. These modifiers include short, long, unsigned, and signed.
short
The short modifier can be applied to the int data type. It can decrease the number of bytes used by the variable, which decreases the range of numbers that the variable can represent. Typically, a short int is half the size of a regular int -- but this will be different depending on the compiler and the system that you use. When you use the short modifier, the int type is implicit. For example:
short a;
is equivalent to:
short int a;
Typically short variables are not used in C++ programs today. Although they take up less memory, they can be slower than regular int types on some systems. Because most machines have plenty of memory today, it is rare that using a short int is advantageous.
long
The long modifier can be applied to the int and double data types. It can increase the number of bytes used by the variable, which increases the range of numbers that the variable can represent. A long int is typically twice the size of an int, and a long double can represent larger numbers more precisely. When you use long by itself, the int type is implied. For example:
long a;
is equivalent to:
long int a;
Use the long modifier when you need to store larger numbers in your variables. Be aware, however, that on some compilers and systems the long modifier may not increase the size of a variable.
unsigned
The unsigned modifier makes a variable only represent positive numbers. It can be applied to the char and int data types. For example, if an int typically holds values from -32768 to 32767, an unsigned int will hold values from 0 to 65536. You can use this modifier when you know that your variable will never need to be negative. For example, if you declared a variable 'myHeight' to hold your height, you could make it unsigned because you know that you would never be negative inches tall.
signed
The signed modifier makes a variable represent both positive and negative numbers. It can be applied to the char and int data types. The signed modifier is applied by default, so you typically will never use it in your code.
Floating Point
The float and double primitive data types are called 'floating point' types and are used to represent real numbers (numbers with decimal places, like 1.435324 and 853.562). Floating point numbers and floating point arithmetic can be very tricky, due to the nature of how a computer calculates floating point numbers.
Constants
Floating point constants should always have a '.' (decimal point) somewhere in them. Any number that does not have a decimal point is interpreted as an integer, which then must be converted to a floating point value before it is used. For example:
double a = 5 / 2;
will not set a to 2.5 because 5 and 2 are integers and integer arithmetic will apply for the division, cutting off the fractional part.
A correct way to do this would be:
double a = 5.0 / 2.0;
You can also declare floating point values using scientific notation. The constant .05 in scientific notation would be 5 * 10^-2. The syntax for this is the base, followed by an e, followed by the exponent. For example, to use .05 as a scientific notation constant:
double a = 5e-2;
|