1. Home
  2. Computing & Technology
  3. C / C++ / C#

C++ Tutorial - Lesson One Learning How to Compile With Visual C++ 2005

By David Bolton, About.com

4 of 8

Numbers in C++

In C++ there are two main types of numbers. Ints and floats. There are also variants of these types that hold bigger numbers, or only unsigned numbers but they are still just an int or float.

An int is a whole number like 47 and has no decimal point. It's used for whole things- You can't have 4.5 babies or loop 32.9 times. You can have $25.76, or a temperature of 97.4 degrees if you use a float. So when writing programs, you must decide which type to use. Generally you'll use int for most things and float (or double - just a float that holds more bits) the odd time.

Why Decide?

Why not just store everything as floats, as some scripting languages do? Because it's inefficient, floats take up more memory and are generally slower than ints. Also you cannot easily compare two floats to see if they are equal like you can with ints.

To manipulate numbers you have to store them in memory. Because the value can be easily changed, it's called a variable. The compiler that reads your program and converts it into machine code needs to know what type it is, ie whether it's an int or a float, so before your program uses a variable, you must declare it.

Here's an example.

int Counter =0;
float BasicSalary;
You'll notice that the Counter variable is set to 0. This is an optional initialization. It's a very good practice to initialize variables when you declare them. If you don't initialize and then use them in code without having set a value then you will end up with a random value that will most likely 'break' your code. The value will be whatever was in memory when the program was loaded.

On the next page : Learn more about ints.

Explore C / C++ / C#
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. C / C++ / C#
  4. C++
  5. Learn C++ Programming
  6. How Many Ways Can I Count in C++?

©2009 About.com, a part of The New York Times Company.

All rights reserved.