Go Back   CodeCall Programming Forum > Software Development > Tutorials > C Tutorials
Register Blogs Search Today's Posts Mark Forums Read

C Tutorials All C Tutorials and Code

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-08-2009, 09:28 PM
WingedPanther's Avatar
Super Moderator
 
Join Date: Jul 2006
Age: 36
Posts: 11,435
WingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud of
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.
__________________
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..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 01-09-2009, 12:48 PM
Turk4n's Avatar
Code Warrior
 
Join Date: May 2008
Location: 4chan.org/g/
Age: 20
Posts: 3,822
Turk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud of
Send a message via MSN to Turk4n Send a message via Skype™ to Turk4n
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 ~❤❤❤
初音ミク。~❤❤❤
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 01-09-2009, 01:27 PM
WingedPanther's Avatar
Super Moderator
 
Join Date: Jul 2006
Age: 36
Posts: 11,435
WingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud of
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 01-09-2009, 01:41 PM
Turk4n's Avatar
Code Warrior
 
Join Date: May 2008
Location: 4chan.org/g/
Age: 20
Posts: 3,822
Turk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud of
Send a message via MSN to Turk4n Send a message via Skype™ to Turk4n
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 ~❤❤❤
初音ミク。~❤❤❤
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 01-09-2009, 03:57 PM
WingedPanther's Avatar
Super Moderator
 
Join Date: Jul 2006
Age: 36
Posts: 11,435
WingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud of
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 01-09-2009, 04:15 PM
Turk4n's Avatar
Code Warrior
 
Join Date: May 2008
Location: 4chan.org/g/
Age: 20
Posts: 3,822
Turk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud of
Send a message via MSN to Turk4n Send a message via Skype™ to Turk4n
Re: Understanding Basic Data Types

I will do it then thank you
__________________

Hatsune Miku ~❤❤❤
初音ミク。~❤❤❤
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 01-09-2009, 06:02 PM
Jordan's Avatar
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Posts: 24,556
Jordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to all
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan Send a message via Yahoo to Jordan
Re: Understanding Basic Data Types

Very nice tutorial! This one should be added to the resource list that v0id maintains. +rep
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 01-11-2009, 12:10 AM
Coder Zombie's Avatar
Newbie
 
Join Date: Jan 2009
Location: USA
Posts: 28
Coder Zombie is an unknown quantity at this point
Send a message via AIM to Coder Zombie
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
__________________
I'm the master of code rot

Last edited by Coder Zombie; 01-11-2009 at 03:19 AM..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 01-11-2009, 06:45 AM
MathX's Avatar
Guru
 
Join Date: Oct 2008
Location: Kosovo
Age: 19
Posts: 3,994
MathX has a spectacular aura aboutMathX has a spectacular aura about
Send a message via MSN to MathX
Re: Understanding Basic Data Types

Great!!! My request is already done . Thanx!!!
__________________
My Blog
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 01-11-2009, 11:08 AM
WingedPanther's Avatar
Super Moderator
 
Join Date: Jul 2006
Age: 36
Posts: 11,435
WingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud of
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
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.


vBulletin v3.8.0 ©2010, Jelsoft Enterprises Ltd.


no new posts

LinkBacks Enabled by vBSEO 3.1.0