Programming:C plus plus Data Types - eMyTextBooks

Search:  

Free online books - just read it!

See live article   •   Programming:C plus plus Data Types
 

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;


Also helps finding: ProgrammingCplusplusDataTypes, ProgrammingC, Cplus, plusplus, plusData, DataTypes, programing, pls, dara, programmin, plu, daa, progamming

   
 
  
Add to bookmarks
Top Articles
 
Ada Programming/Keywords/begin
Ada Programming/Keywords/renames
Ada Programming/Keywords/use
Cookbook
Cookbook:Basic foodstuffs
Cookbook:Egg
Cookbook:Milk
Cookbook:Tablespoon
Dichotomous Key
Electronics
Puzzles
SA NCS:Consumer Studies
SA NCS:Dance Studies
SA NCS:Dramatic Arts
SA NCS:Electrical Technology
SA NCS:First Additional Language
SA NCS:Mathematical Literacy
SA NCS:Tourism
SA NCS:Visual Arts
Wikibooks Pokédex:National Index
Wikiversity
Search LiveJournal blogs for Programming:C plus plus Data Types
 

Web Site Design UK  •  Free Animated Greetings  •  Credit Consolidation  •  Millsberry •  Wordpress Themes

Copyright @ 2005 eMyTextBooks.com
This article is from Wikibooks. All text is available under the terms of the GNU Free Documentation License.