Thread: Variable size and range

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    110

    Variable size and range

    I am having trouble to understanding this combination to store variable. I do not understand which one should be use for specific reason
    let's suppose if I want to store integer number then I have 11 ways to store integer value'

    Code:
    int N;
    Code:
    signed int N;
    Code:
    unsigned int N;
    Code:
    short int N;
    Code:
    long int N;
    Code:
    short  int N;
    Code:
    short signed  int N;
    Code:
    short unsigned  int N;
    Code:
    long  int N;
    Code:
    long signed  int N;
    Code:
    long unsigned  int N;
    I know the difference between signed and unsigned int. if I want to store positive number I use unsigned int and if I don't know number is positive or negative then i use only int.

    I think each statement has its own important. I do not understand what to use and when to use it ?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,493
    You repeated short int and long int, and the signed keyword really only matters for signed char since by default the integer types are signed. Then you omitted long long. So I'd put the list as:
    Code:
    signed char
    unsigned char
    short
    unsigned short
    int
    unsigned int
    long
    unsigned long
    long long
    unsigned long long
    and then there are width-specific types from <inttypes.h> that are aliases for these types above, along with other aliases like size_t and ptrdiff_t. I omitted char because it's not a good idea to use it as an integer type because whether it is signed or unsigned is implementation defined.

    As for what to use: it depends on your requirements, of course. If you are unsure, default to int (or unsigned int if you know you need an unsigned integer) and perhaps use your own typedef so you can easily change it later. If you're working with sizes, consider using size_t.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2017
    Posts
    110
    Quote Originally Posted by laserlight View Post
    You repeated short int and long int, and the signed keyword really only matters for signed char since by default the integer types are signed. Then you omitted long long. So I'd put the list as:
    .
    That information given on this page C syntax - Wikipedia



    1. Size qualifiers – short, long
    2. Sign qualifiers – signed, unsigned
    3. Data types - int, char, float, double

    <Sign qualifiers > <Data types>
    <Size qualifiers> <Data types>
    <Size qualifiers> <
    Sign qualifiers > <Data types>

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,493
    Quote Originally Posted by abhi143 View Post
    That information given on this page C syntax - Wikipedia



    1. Size qualifiers – short, long
    2. Sign qualifiers – signed, unsigned
    3. Data types - int, char, float, double

    [FONT="]<[/FONT][FONT="]Sign qualifiers > <[/FONT]Data types>
    [FONT="]<Size qualifiers> [/FONT][FONT="]<[/FONT]Data types>[FONT="]
    <Size qualifiers> <[/FONT]
    Sign qualifiers > <Data types>
    Can't find that on that page. What I did find, under Data structures -> Primitive data types -> Integer types, was the statement that "In many cases, there are multiple equivalent ways to designate the type; for example, signed short int and short are synonymous." and a "Specifications for standard integer types" table that is identical to mine except that it includes _Bool and char.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2017
    Posts
    110
    Quote Originally Posted by laserlight View Post
    Can't find that on that page. What I did find, under Data structures -> Primitive data types -> Integer types,ard integer types" table mine except that it includes _Bool and char.
    What you said in post 2 is given in page integer types" table

    suppose we want to store number -32768 to 32767

    Code:
    #include<stdio.h> int main(void)
    {    
      short a = 32767;
      printf("max number = %d \n", a);  
      printf ("size of a = %d", sizeof (a));     
      return 0; 
    }
    max number = 32767
    size of a = 2

    in my example, I am just using qualifier to store data but I haven't used data type

    I still don't know what to use and when it should use ?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,493
    Quote Originally Posted by abhi143
    in my example, I am just using qualifier to store data but I haven't used data type
    The data type is named short. It is an alias for short int and for signed short int. If you cannot get your head around this, then use my table as the sole truth and forget that you ever heard about short int (unless you do see it in the wild, then just aggressively delete the "int" and move on).

    Quote Originally Posted by abhi143
    I still don't know what to use and when it should use ?
    I thought you were talking about selecting relevant data types. If your concern is just about the type names and their aliases, then as I said, use my table from post #2 as your sole source of truth concerning the integer type names. If Wikipedia tells you something else that you think is contradictory, forget it as I'm more authoritative than Wikipedia (seeing that I can change Wikipedia by citing the standard).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to compute the variable type range
    By webofunni in forum C Programming
    Replies: 1
    Last Post: 08-04-2009, 01:28 AM
  2. performing range checks on variable values
    By broli86 in forum C Programming
    Replies: 12
    Last Post: 07-02-2008, 11:21 AM
  3. Replies: 2
    Last Post: 02-23-2008, 03:59 PM
  4. Replies: 5
    Last Post: 08-09-2004, 08:25 PM
  5. Variable Size
    By Gr3g in forum C++ Programming
    Replies: 13
    Last Post: 04-17-2002, 11:09 AM

Tags for this Thread