+ Reply to Thread
Results 1 to 10 of 10

Thread: Understanding Basic Data Types

  1. #1
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,652
    Blog Entries
    57

    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
    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.
    Last edited by John; 01-08-2009 at 11:41 PM.
    CodeCall Blog | CodeCall Wiki | Shareware
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  2. #2
    Code Warrior Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n's Avatar
    Join Date
    May 2008
    Location
    4chan.org/g/
    Age
    20
    Posts
    3,835
    Blog Entries
    4

    Re: Understanding Basic Data Types

    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 ~❤❤❤
    初音ミク。~❤❤❤

  3. #3
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,652
    Blog Entries
    57

    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

  4. #4
    Code Warrior Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n's Avatar
    Join Date
    May 2008
    Location
    4chan.org/g/
    Age
    20
    Posts
    3,835
    Blog Entries
    4

    Re: Understanding Basic Data Types

    Quote Originally Posted by WingedPanther View Post
    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.
    So I can trim down for an example the max possible size of a type in C++?

    Hatsune Miku ~❤❤❤
    初音ミク。~❤❤❤

  5. #5
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,652
    Blog Entries
    57

    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

  6. #6
    Code Warrior Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n's Avatar
    Join Date
    May 2008
    Location
    4chan.org/g/
    Age
    20
    Posts
    3,835
    Blog Entries
    4

    Re: Understanding Basic Data Types

    I will do it then thank you

    Hatsune Miku ~❤❤❤
    初音ミク。~❤❤❤

  7. #7
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,556
    Blog Entries
    97

    Re: Understanding Basic Data Types

    Very nice tutorial! This one should be added to the resource list that v0id maintains. +rep

  8. #8
    Newbie Coder Zombie is an unknown quantity at this point Coder Zombie's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    28

    Re: Understanding Basic Data Types

    Quote Originally Posted by WingedPanther View Post
    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.
    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 02:19 AM.
    I'm the master of code rot

  9. #9
    Guru MathX has a spectacular aura about MathX has a spectacular aura about MathX's Avatar
    Join Date
    Oct 2008
    Location
    Kosovo
    Age
    19
    Posts
    4,006

    Re: Understanding Basic Data Types

    Great!!! My request is already done . Thanx!!!

  10. #10
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,652
    Blog Entries
    57

    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

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Abstract Data Types and Code Efficiency.
    By WingedPanther in forum C Tutorials
    Replies: 5
    Last Post: 12-25-2008, 05:56 AM
  2. Java:Tutorial - Data Types
    By John in forum Java Tutorials
    Replies: 6
    Last Post: 07-02-2007, 09:16 PM
  3. Abstract data types in C++
    By priorityone in forum C and C++
    Replies: 1
    Last Post: 01-08-2007, 11:43 AM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts