The ProgrammersTalk Community
Forum Register Search Today's Posts Mark Forums Read
Register

Go Back   The ProgrammersTalk Community > General Programming > C / C++


Welcome to the The ProgrammersTalk Community forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.
Reply
 
LinkBack Thread Tools    Display Modes   
  #1 (permalink)  
Old 09-02-2007, 04:45 PM
Lee's Avatar
Lee Lee is offline
PT Staff*
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: Blackpool, UK
Posts: 616
iTrader: (0)
Lee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enough
When to use...Data type?

Hey,

There's a few data types that i don't know when i should use them, i was hoping someone could provide example cases of when to use these over the average ones string and integer.

1)short
2)long
3)float
4)double
5)long Double
6)wchar_t

If you only know the one or two then please do post them, all help is welcome,
Thanks,
Lee.
Reply With Quote
  #2 (permalink)  
Old 09-03-2007, 04:12 AM
Bench Bench is offline
Full Programmer
Join Date: Jul 2007
Location: UK
Posts: 111
iTrader: (0)
Bench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished road
float/double/long double are floating point "real" numbers. C/C++ uses double (aka. double-precision) by default for representing real values. so you should prefer double when holding real values.
float is typically smaller than double, with lesser precision. long double is known as extended precision, which is typically greater than double.

Though you'll inevitably come across compilers where one or more of these types are the same size.
The only guarantee you have is that sizeof(float) <= sizeof(double)
and sizeof(double) <= long double

Generally, float isn't used very often, it may be useful if you're economising on space, and don't need the precision (Such as sending bits over a network stream). long double is useful if you need the extra precision where its available.

wchar_t is typically used for holding unicode values (useful when support for foreign language character sets is needed)

as for long/short - These are shorthand for long int and short int. As is the case for the real types above, you've only got one guarantee for each..
sizeof(char) == 1
sizeof(char) <= sizeof(short)
sizeof(short) <= sizeof(int)
sizeof(int) <= sizeof(long int)

in general, use int. which is the default integral type in C/C++. If you need a bit more range than int can provide, then use long, be aware that long may be the same size as int. (if that's not enough, use a "Big Number" library)
If you're economising on space, you might feel the need to use short, although in practice, its not used all that commonly. Again, being aware that short can be the same size as int.


If you're not sure what range or precision you need, start with the default ones - char for characters, int for integrals and double for real numbers. If it transpires later that one of the others are needed, then you can change it.

Although swapping char for wchar_t is a little trickier, it has seperate string and I/O facilities to plain char. generally these are prefixed with 'w' - eg, wcout, wcin, wstring.

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!

Last edited by Bench : 09-03-2007 at 04:18 AM.
Reply With Quote
The Following User Says Thank You to Bench For This Useful Post:
Lee (09-03-2007)
  #3 (permalink)  
Old 09-03-2007, 05:12 AM
Lee's Avatar
Lee Lee is offline
PT Staff*
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: Blackpool, UK
Posts: 616
iTrader: (0)
Lee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enough
Wow, i wasn't expecting that answer, that's awesome and loads of help, couple of small thing though, i always use int for numbers,
Does that mean i should really think about using double?
If so when should i use int over double or vice versa?
What do you mean by real values?

Thanks,
Lee.
Reply With Quote
  #4 (permalink)  
Old 09-03-2007, 06:05 AM
Bench Bench is offline
Full Programmer
Join Date: Jul 2007
Location: UK
Posts: 111
iTrader: (0)
Bench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished road
by "real" and "floating point" numbers, I mean non-integers that represent data with a fractional part.

floating point types can't be used for anything which requires an integer - Things like array indexes, enums, pointer-offsets, character representation, etc all need an integral value
You'll probably find that double will be useful in real-world calculations, such as trigonometry, currency, scientific values, etc.

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!

Last edited by Bench : 09-03-2007 at 06:15 AM.
Reply With Quote
The Following User Says Thank You to Bench For This Useful Post:
Lee (09-03-2007)
  #5 (permalink)  
Old 09-03-2007, 06:53 AM
Lee's Avatar
Lee Lee is offline
PT Staff*
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: Blackpool, UK
Posts: 616
iTrader: (0)
Lee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enough
Thanks again, thats cleared everything about them i can think of for now
Reply With Quote
  #6 (permalink)  
Old 09-03-2007, 10:00 AM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,111
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
I personally still don't get of why do you ask of "When to use one variables over another?" Because I'd say that their functionalities are different. Well, there are some of them those are similar, though, they have different sizes. So, it's either on how do you want to use the variable for, OR if you need more size for whatever information to be stored in a variable

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #7 (permalink)  
Old 09-03-2007, 12:17 PM
TeraTask's Avatar
TeraTask TeraTask is offline
PT Staff*
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 3
Join Date: Jun 2007
Location: Reno, NV
Posts: 428
iTrader: (0)
TeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enough
I'm afraid I take a far more simplistic approach. In whatever language I'm working with, I go to the documentation and print out the minimum and maximum values. Then, when choosing a type, I choose the type that uses the smallest number of bytes and covers the entire range of possible values.

__________________
Jeremy Miller
Content Farmer - Optimized Automated Blog Posting

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #8 (permalink)  
Old 09-04-2007, 07:27 PM
rpgfan3233 rpgfan3233 is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jul 2007
Posts: 118
iTrader: (0)
rpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura about
Quote:
Originally Posted by HelloWorld View Post
I personally still don't get of why do you ask of "When to use one variables over another?" Because I'd say that their functionalities are different. Well, there are some of them those are similar, though, they have different sizes. So, it's either on how do you want to use the variable for, OR if you need more size for whatever information to be stored in a variable
In Java, yes, that is guaranteed. A quick comparison of what is comparable on my machine (format: Java = C/C++) -
boolean = _Bool/bool
byte = char
char = wchar_t
short = short int
int = long int (note that on my machine, a C/C++ int = a C/C++ long int, but a C/C++ int = a C/C++ short int on some other machines)
long = long long (C99 only, not valid with C++98)

As Bench wrote, C++ only guarantees that a char is at least 1 byte in size and each integral data type is at least the same size. The same is true for the real/floating-point types. Honestly, a char in C++ could be the same size as a long int, meaning that your long int would only be able to hold -128 to 127 (signed) or 0 to 255 (unsigned). Also, a char is often an int with a restricted range. As such, int is often preferred because using char causes conversion to take place at runtime, which can affect performance somewhat, depending on the efficiency of the instructions used for the conversion and the machine's abilities itself.

Quote:
Originally Posted by TeraTask View Post
I'm afraid I take a far more simplistic approach. In whatever language I'm working with, I go to the documentation and print out the minimum and maximum values. Then, when choosing a type, I choose the type that uses the smallest number of bytes and covers the entire range of possible values.
Yeah, that's nice to do, but in some languages, that can cause performance issues, as I said previously. The only reason they exist is for convenience IMHO. That way, a programmer doesn't need to "reinvent the wheel" by doing range checking herself/himself. In some languages however, no performance issues are noticed. For example, one can calculate 65536^256 in Python rather quickly to EXACT PRECISION. Often a program in certain languages such as Common Lisp, Perl and Python are amazingly fast compared to the equivalent program in the simpler, more machine-based C or C++ program. Of course, they were designed to be that way.

__________________
"C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off."
-- Bjarne Stroustrup, creator of what is now known as C++
For more quotes by Bjarne Stroustrup, check out http://www.research.att.com/~bs/bs_faq.html#really-say-that.
Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following User Says Thank You to rpgfan3233 For This Useful Post:
HelloWorld (09-04-2007)
Reply


Thread Tools
Display Modes

   Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 11:48 AM. Powered by vBulletin
Copyright © 2000 - 2007, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO © 2007 ProgrammersTalk Sedo - Buy and Sell Domain Names and Websites project info: programmerstalk.net Statistics for project programmerstalk.net etracker® web controlling instead of log file analysis


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50