Understanding Basic Data Types

Status
Not open for further replies.

Baljeet

Member
Jan 31, 2011
76
0
There are several basic types in C++, each of which has various modifications available. These are: booleans, characters, integers, floating-points, enumerations, and void. Pointers, arrays, references, data structures and classes are built around the basic types and are discussed in other tutorials.

Logical Types
There is only one logical data type, the bool. It can only accept two values: true or false. This is the return type of all logical operators, such as ==, !=, etc., and is used to control loops and if statements. Integer types can be implicitly converted to and from bools. 0 converts to false, non-zero values convert to true, false converts to 0, and true converts to 1.

Character Types
There are two basic character types, with a few variations. The purpose of these is to store a single character in the system's character set.
char: this generally stores an 8-bit character in a standard character set, such as ASCII or EBCDIC. This means it can store 256 characters on most systems. char values can be cast to an int to return the numeric value of the encoded character in the system's character set. for example:
Code:
  char c = 'b';
  cout << int(c); // prints 98 in ASCII based systems
For high values (128-255), the integer value may be positive or negative. This means the typical range of a char could be either -128-127 or 0-255. C++ allows the implementation to choose. If you need to specify how these high values will be handled, you can specify the type as "unsigned char" or "signed char". Because of the limited range of values, however, chars are not good for storing integer values.

wchar_t: C encountered a problem with char: it was too small for Unicode and other large character sets. As a result, C created a typedef: wchar_t. C++ carries it over as a built-in data type.

char literals are in the form 'a'. wchar_t literals are in the form L'ab'. The number of characters a wchar_t requires depend on the implementation. Special characters, such as newline or horizontal tab, are escaped with a backslash: '\n' or '\t' in this case.

In C, strings were stored as arrays of chars, where the last character is '\0'. In order to support most C programs in C++, this form of representing strings is supported. This style of string is enclosed in double-quotes instead of single-quotes when using literals. "A string" would be stored as 'A',' ','s','t','r','i','n','g','\0'. The extra character for the null termination is required. C++ offers a more robust method for handling strings in the string class, with far more flexibility for working with strings.

Integer Types

Along with the character types, there are several other types that store integers. These are: short int, int, and long int. Each of these can be plain, signed, or unsigned. Note that these are NOT guaranteed to be different sizes. There may be limitations on the implementation that cause all of them, including char, to be the same size. A plain int is ALWAYS signed. Integer literals can be decimal, octal, or hexadecimal. If the leading two characters are 0x, it is hexadecimal. If the leading character is 0 not followed by x, it is octal. So 15, 0xf, and 017 are all the same value. Suffixes can further specify types. 15U is an unsigned int, and 15L is an unsigned long.

Floating-Point Types
There are three floating point types: float, double, and long double. Generally, they are increasing in capacity, but may be equal. double is generally good. Literals must have no spaces and must include a decimal point. In general, think of them as standard decimals or decimals in scientific notation. Examples are 1. , 1.24 , 1.23e-4 , .23 A suffix f or F indicates it is a float. A suffice of L or l indicates it is a long double. The default is double.

How big is it?
The sizeof() operator returns the size of a data type in multiples of a char. sizeof(char) will always return 1. sizeof(int) is frequently 4, but could be 1. To find the upper and lower limits of values, include the limits header and use the provided functionality
Code:
#include <limits>
#include <iostream>

int main()
{
  std::cout << std::numeric_limits<float>::max();
}

Void
void can be used as the return type of a function to indicate it does not return a value, or as the type of a pointer if the pointer points to unknown types.

Enumerations
You can create a custom type called an enumeration. It creates a list of variables and associates each value with an integer value. These start at 0 by default, but can be specified in the declaration. The range of values that can be assigned to an enum are either 0 to 2^n - 1 (if there are no negative values, 2^n - 1 uses the minimum n to include all required values) or -2^n to 2^n - 1 (if there are negative values, use the minimum n to include all required values).

sizeof() is not guaranteed to provide meaningful information for an enum.

All Credits goes to one who really made this...
 
Status
Not open for further replies.

Users who are viewing this thread

Top