Thread: Variable Types

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    3
    Rep Power
    0

    Variable Types


    Hey guys,

    This might seem like a stupid question but it's been puzzling me for a while whilst reading documentation and watching YouTube tutorials on working with variables for beginners. They always seem to use intergers for storing numbers regardless of the size of the number or the number range. Now as everyone knows an integer number range is -2,147,483,648 to 2,147,483,648 which is a lot of space especially if you're going to be working with simple calculations.

    Wouldn't it be better if I used the correct variable types depending on the size of the number I'm working with. Such as if I'm going to be doing calculations using time for example;

    byte
    - Seconds 60
    - Minutes 60
    - Hours 24
    - Days 7
    - Months 12

    ushort
    - Milliseconds 1000
    - Years 365

    Please let me know if this is madness or if I should stick to only using integers and no other variable types. When do you only use integers and when do you switch and start using other variable types or does it matter at all?

    Thanks from Cubies.
  2. #2
  3. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,117
    Rep Power
    1803
    Originally Posted by Cubies
    Now as everyone knows an integer number range is -2,147,483,648 to 2,147,483,648 which is a lot of space especially if you're going to be working with simple calculations.
    Not really - its is 32bits or 4 bytes. On a 32 bit architecture that is the natural word size for the machine and can sometimes be more efficiently addressed than smaller widths.

    Originally Posted by Cubies
    Wouldn't it be better if I used the correct variable types depending on the size of the number I'm working with.
    Not significant better unless you are storing very-large arrays of values or on severely memory constrained systems where the reduced memory usage may be significant.

    Originally Posted by Cubies
    Please let me know if this is madness or if I should stick to only using integers and no other variable types. When do you only use integers and when do you switch and start using other variable types or does it matter at all?
    I advise that if the value is an "arithmetic" object, i.e. a variable upon which you will perform any kind of arithmetic operation, and a plain int has sufficient range (even if excessive) then you use plain int. C and C++ has all sorts of arcane rules on type promotion and conversion when integers of mixed size or sign are used in an expression that can yield unexpected results for the unwary; most of these problems are avoided by sticking to a plain int.

    You might use specific types in a number of circumstances, for example:
    • Type agreement with a third-party or existing library,
    • To conform to a specific communication protocol or file format,
    • To match the size and type of a hardware register in a device driver.
  4. #3
  5. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,648
    Rep Power
    4248
    Interesting points:

    Originally Posted by Cubies
    Now as everyone knows an integer number range is -2,147,483,648 to 2,147,483,648 which is a lot of space especially if you're going to be working with simple calculations.
    Actually, the integer number range depends on the compiler. For instance, most 16-bit compilers have ints going from -32768 to 32767. Also, the range from -2,147,483,648 to 2,147,483,648 only takes up 4 bytes of memory, so it isn't that much memory. However, your point is taken.

    Originally Posted by Cubies
    Wouldn't it be better if I used the correct variable types depending on the size of the number I'm working with. Such as if I'm going to be doing calculations using time for example;

    byte
    - Seconds 60
    - Minutes 60
    - Hours 24
    - Days 7
    - Months 12
    C doesn't support ranges like that, but some other languages do (e.g. Pascal).
    Code:
       type
           Seconds = 0 .. 59;
           Minutes = 0 .. 59;
           Hours = 0 .. 23;
           Days = (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday);
       var
            secs: Seconds;
            min: Minutes;
            day: Days;
    begin
       secs := 0; // Set the seconds value to 0
       inc(secs);  // and increment it to the next value.
       day := Monday;
       inc(day); // Now set the day to tuesday
    end;
    Here, the type section declares the ranges for various types and the var section defines variables of the different types. Note how the increment is done using the inc() function, rather than secs := secs + 1; or day := day + 1;. This is because the type is not necessarily an integer (for example, the Days type isn't), so you have to use inc() instead. A feature like this isn't much of a memory saver, but it could help with logical bugs (e.g. you can't assign secs := 61 because it is outside the range of that type.)

    Of course, such features come with a price -- the generated code has to do range checking at runtime for every assignment and inc()/dec() operation, which slows the code down. And, depending on the underlying compiler implementation, you may only be able to use N items in a type declaration (e.g. the original Pascal implementation used bit fields for sets and could only support a max. of 36 items in a set type, since it ran on a machine with 36-bit bytes)
    Last edited by Scorpions4ever; July 29th, 2013 at 02:22 PM.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo

IMN logo majestic logo threadwatch logo seochat tools logo