|
||||||
| C Tutorials All C Tutorials and Code |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
||||
|
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: 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 Code:
#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.
__________________
CodeCall Blog | CodeCall Wiki | Shareware Programming is a branch of mathematics. My CodeCall Blog | My Personal Blog Last edited by John; 01-09-2009 at 12:41 AM.. |
|
||||
|
Re: Understanding Basic Data Types
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 | Shareware Programming is a branch of mathematics. My CodeCall Blog | My Personal Blog |
|
||||
|
Re: Understanding Basic Data Types
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 | Shareware Programming is a branch of mathematics. My CodeCall Blog | My Personal Blog |
|
||||
|
Re: Understanding Basic Data Types
Very nice tutorial! This one should be added to the resource list that v0id maintains. +rep
__________________
Questions and Answers | Online News and Social Bookmarking | Code and Text Collaboration General Chat Forum |
|
||||
|
Re: Understanding Basic Data Types
Quote:
Here is a link to clarify http://home.att.net/~jackklein/c/inttypes.html
__________________
I'm the master of code rot Last edited by Coder Zombie; 01-11-2009 at 03:19 AM.. |
|
||||
|
Re: Understanding Basic Data Types
@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 | Shareware Programming is a branch of mathematics. My CodeCall Blog | My Personal Blog |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| C++ Abstract Data Types and Code Efficiency. | WingedPanther | C Tutorials | 5 | 12-25-2008 06:56 AM |
| Java:Tutorial - Data Types | John | Java Tutorials | 6 | 07-02-2007 10:16 PM |
| Abstract data types in C++ | priorityone | C and C++ | 1 | 01-08-2007 12:43 PM |
All times are GMT -5. The time now is 08:16 AM.
Amrosama.cc
Arekbulski.cc
Debtboy.cc
Guest.cc
Jaan.cc
James.cc
Mathx.cc
Tsz.cc
Vswe.cc