Is there any special differences should been told about "double" and "long double". I would like to know in my quest to learn C++ to its fullest![]()
Thread: Understanding Basic Data Types |
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:
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.Code:char c = 'b'; cout << int(c); // prints 98 in ASCII based systems
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
VoidCode:#include <limits> #include <iostream> int main() { std::cout << std::numeric_limits<float>::max(); }
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.
Last edited by John; 01-08-2009 at 09:41 PM.
CodeCall Blog | CodeCall Wiki
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog
Is there any special differences should been told about "double" and "long double". I would like to know in my quest to learn C++ to its fullest![]()
Hatsune Miku ~❤❤❤
初音ミク。~❤❤❤
One of the big issues with C++, as opposed to Java, is that sizes are implementation specific. This results in things about data type sizes like:
1 == sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long)
sizeof(float) <= sizeof(double) <= sizeof(long double)
In other words, a float could be the same size as a long double... or not. To know what the limits are for your implementation, you really have to go into the limits library and look at max() and min(). Often, long double provides more digits of precision and a larger exponent than double, but maybe not.
CodeCall Blog | CodeCall Wiki
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog
You can easily run a quick example program that will give you the max/min values of all the basic data types. Just take the line in the example code and change the data type in the numeric_limits<T> template.
CodeCall Blog | CodeCall Wiki
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog
I will do it then thank you![]()
Hatsune Miku ~❤❤❤
初音ミク。~❤❤❤
Very nice tutorial! This one should be added to the resource list that v0id maintains. +rep
The standard does actually declare mins for int variables. It says short has to be at least 16 bit ,int has to be at least 16 bit,long has to be at 32 bits.I was on a discussion were a lot of programmers argued over that saying that ints were fully implementation specific but someone pulled out their copy of the standard it says min values are defined.Now on some machines long or long long can be 128 bits so you can only count on min values.
Here is a link to clarify http://home.att.net/~jackklein/c/inttypes.html
Last edited by Coder Zombie; 01-11-2009 at 12:19 AM.
I'm the master of code rot
Great!!! My request is already done. Thanx!!!
@Coder Zombie: you're right about the minimums (they weren't in front of me, but I've seen them before). The specification can lead to interesting portability issues if someone isn't aware of the variability that exists.
CodeCall Blog | CodeCall Wiki
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog
There are currently 1 users browsing this thread. (0 members and 1 guests)