Thread: C data types

  1. #1
    back? dbaryl's Avatar
    Join Date
    Oct 2001
    Posts
    597

    C data types

    Can someone point me in the right direction where I can learn more about C data types... specifically the variations of int (short, unsigned) and double (float, long).

    Also, I've been taught to use int, char and double, the rest of the types were not even mentioned. How important are they to implement (ex.: when would I use a short unsigned int instead of an int)
    This is my signature. Remind me to change it.

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    there's only a few data types. each one can be signed or unsigned (although it won't make a difference with floats and doubles.)

    an int is an integer. nowadays, a long is the same thing. they take up 4 bytes of memory and range about +/- 2 billion.
    a short int is an older type of integer. it takes up 2 bytes, and ranges about +/- 64 thousand.

    a char is a byte. it can range from -128 to 127 (or -127 to 128... i can't remember). an unsigned char ranges from 0 to 255.

    a float is a floating-point number that takes up 4 bytes. it has about 7 decimal points of precision. a double takes 8 bytes, and therefore has more precision, 15 decimal places.

    there are others, some undefined in common c, but mostly you will only need to use the ones i've listed.

    remember that unsigned numbers have the same range as signed numbers. they just start at zero and go up, rather than start at zero and go both ways half as much.
    Last edited by ygfperson; 07-20-2002 at 09:58 PM.

  3. #3
    back? dbaryl's Avatar
    Join Date
    Oct 2001
    Posts
    597
    So, in general, which would you use for an integer and which for a decimal? Does it make that much a difference (like using a char for a loop counter)?

    Is there really any need to use a double over an float? Rarely do we ever use something over 7 decimal spaces... when would you bother to use the unsigned int over an int?
    This is my signature. Remind me to change it.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by dbaryl
    So, in general, which would you use for an integer and which for a decimal? Does it make that much a difference (like using a char for a loop counter)?

    Is there really any need to use a double over an float? Rarely do we ever use something over 7 decimal spaces... when would you bother to use the unsigned int over an int?
    It's always best to actually say what kind of variable you want, instead of letting the compiler do it for you. If you wan a long int, say long, don't just say int. If you want a short int, naturally, you'll specify short. Don't just use 'int' by itself. (I actually do for most stuff here, just because I'm lazy.)

    What variable you use all depends on what your need is. Do you only need a number from 0 to 32000? Use an unsigned short int then. Do you need a negative number? Will it be more / less than X?

    For example, were I creating a game where each object had N number of statistics, none of which would ever go above 100, and none of which would go below 0, then I'd simply use an unsigned char. There is no point to use a long or short int.

    If you aren't sure what you need, do something like:

    #define DATATYPE long int

    Then in your program:

    DATATYPE x, *ptr;

    So later, when you need change from a char to a long int, or vice versa, or you decide you want a floating point number instead, you only have to change it in one spot.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extending basic data types.
    By nempo in forum C++ Programming
    Replies: 23
    Last Post: 09-25-2007, 03:28 PM
  2. Replies: 4
    Last Post: 06-14-2005, 05:45 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM
  5. Using enumerated data types
    By SXO in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2001, 06:26 PM