Broad Network


Integer and Float Object Types in C++

C++ Just After the Basics - Part 1

Forward: In this article I show you the different variations of the integer object and the float object types in C++.

By: Chrysanthus Date Published: 22 Aug 2012

Introduction

In C++, an object said to be of an integer type is an object that holds (or that can hold) an integer. Also, an object said to be of a float type is an object that holds (or can hold) a number that has (or can have) a decimal point. There are different variations of the integer and different variations of the float. In this article I show you the different variations of the integer object and the float object types in C++.

The Integer Object Type
In C++, the integer object type has 3 integer types (some say 4 – see below). You have the short int type, the int type and the long int type. An object said to be of the int type can hold integer values that are larger than an object said to be of the short int type. An object said to be of the long int type can hold integer values that are larger than an object said to be of the int type.

The following definitions are valid:

        short int ident1;
        int ident2;
        long int ident3;

The size (no. of bytes) of any of the above variations (objects) depend on the system the C++ program is compiled for. However, the number of bytes for a short int is normally smaller than the number of bytes for an int, which is normally smaller than the number of bytes for a long int.

With the above definitions, each of the values can be positive (e.g. 4 or +4) or negative (e.g. -4). The types as such are said to be signed types. If you want only positive numbers to be stored in the objects, then you have to precede the type specifier with the word, unsigned. The following definitions are valid:

        unsigned short int ident1;
        unsigned int ident2;
        unsigned long int ident3;

A character (e.g. ‘A’) is represented as integer, so a character is described as an integer type. Considering the character as an integer type, you have 4 integer types in C++.

The Float Object Type
In C++, the float object type has 3 float types. You have the float type, the double type and the long double type. An object said to be of the double type can hold float values that are larger than an object said to be of the float type. The value of an object said to be of the long double type is of higher number precision than the value of an object said to be of the double type.

The following definitions are valid:

        float ident1;
        double ident2;
        long double ident3;

The unsigned keyword is not used with the different float types. The different float types do not need any preceding specifier for positive numbers. A float value can be, -2.5, +2.5, or 2.5, where +2.5 and 2.5 are the same.

If a float number is too long to type, you can type it with in a kind of arithmetic standard form format. The following initialization is valid.

        double ident = 2.35e13;

The value is 2.35 times 10 raised to the power, 13. Note the use of e above. If the power is negative, then precede the power with the negative sign to have something like 2.35e-13.

The size of any of the above float types depend on the system the C++ program is compiled for.

Well, that is what I have for you as Integer and Float Object Types, in C++. We continue in the next part of the series.

Chrys

Related Courses

C++ Course
Relational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message