Help with understanding ( int, char, long, short, signed, unsigned etc.... )

This is a discussion on Help with understanding ( int, char, long, short, signed, unsigned etc.... ) within the C++ Programming forums, part of the General Programming Boards category; Most of you will answer this question with "Go read a book" or "Don't you own a c/c++ book?" k. ...

  1. #1
    0x01
    Join Date
    Sep 2001
    Posts
    88

    Help with understanding ( int, char, long, short, signed, unsigned etc.... )

    Most of you will answer this question with "Go read a book" or "Don't you own a c/c++ book?"

    k. The question is simple: When should I use a long, int, char, unsigned/signed variables?? K, thas very blunt, but this should narrow it down...

    When I declare a variable "unsigned;" what did I do it???
    Why would I delcare an integer "long" or "short" ( unsigned or signed)??

    Any examples of when things like "unsigned", "long", "short" etc... should, and shouldn't be used???



    One more thing; I noticed a lot of the code I see IN MSVC++ has variables with these "pre-fixes", for instance "Cdialog" (notice the C in front of the word Dialog) or "nResponse" ( notice the 'n' in front of Response ), or things like m_nResponse (notice the "m_n" in front of the word "Response") are these prefixes suppposed to represent things?

    I know these may seem like stupid questions, i have been programming for awhile.. theses are just things that I comme across at time.

    Help will be appreciated.
    Last edited by knave; 05-12-2003 at 05:04 PM.

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    How's this:

    When you NEED to use a short, long, or unsigned int, you'll know it.

    In the meantime, use int, concentrate on learning how to handle the logic of programming, and don't worry about those details.

    Use char if you're working with variables that won't involve arithmetic (such as names, phone numbers, etc.).

  3. #3
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    Try Google.

    Just kidding, you asked the question nicely.

    Declaring the variable unsigned means that instead of having a range from, for example, -127 to 128, it has a range that goes from 0 to 256. You can't have negative numbers with an unsigned variable, but you can with a signed one. So...depends on what you need.

    As a general rule of thumb, figure out what the maximum and minimum values for the variable will be, and then pick the smallest variable (in byte size) that you can use for that range. If you're using floats/doubles, decide how much precision you need as well.

    Code:
    Type     Meaning
    -----------------
    char	character
    int	integer
    float	single-precision floating point
    double	double-precision floating point
    Code:
        Typical Sizes
    Type		Size
    ---------------------------
    char  		 8 bits
    short int 	 8 or 16 bits
    int 		16 or 32 bits
    long int 	32 bits
    float 		32 bits
    double 		64 bits
    long double 	80 bits
    edit: Those things before the variable names are known as Hungarian notation, and for more info on that one, I will direct you to Google.
    Away.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    "Cdialog" (notice the C in front of the word Dialog) or "nResponse" ( notice the 'n' in front of Response ), or things like m_nResponse (notice the "m_n" in front of the word "Response") are these prefixes suppposed to represent things?"


    Yep.

    'C' means it's a class name.
    'n' means int type.
    'm_' means its a data member(of a class)

    So, m_nResponse means that the variable is of type int and it's a data member of a class. If you do that with your code, it makes it easier for people to understand. A very common one is using 'p' for a pointer:

    int* pnumber = 0;

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    'Hungarian Notation' I think its called.

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    126
    Here is a brief explaination of signed and unsigned:

    All variables of the same type(such as two ints or two chars) are the same number of bytes long. For example, a long variable is always 4 bytes long, no matter if it's signed or unsigned. The only difference between signed and unsigned variables is the interpretation of the bits of the variable. For example, with an unsigned int, the range of valid numbers it can hold is 0 to about 4.2 billion(2 to the 32nd power). However, the range of a signed iint is about -2 billion (negative 2 to the 31st) and 2 billion(2 to the 31st). This is because the highest bit of a signed variable is used to determine whether the variable is positive or negative. Basically, the size of the range of numbers a type can hold is the same(2 to the n power, where n is the number of bits), but with signed variables, 0 is in the middle of the range, while 0 is on the bottom of the range with unsigned numbers.

    Hmmm, I don't know if that'll make much sense to you, but it's the best I can do. Also, those little prefixes before variables are called Hungarian notation. I personally don't care for it, but Hungarian notation is all over WinAPI programming(first used by "legendary" Windows programmer Charles Simonyi, I think)

  7. #7
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Is it really necessary to find the best variable for whatever your purpose is? I mean at the level I'm at it probably isn't, but does it ever become a real necessity?
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  8. #8
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    >>> but does it ever become a real necessity?

    Of course. Assuming you use only unsigned, if you have declared 2 variables that are 8bits wide, and try to add them together and store the result in another 8bit variable, and the result is greater than 255 what result will you get?

    Would you want the same programmer working out your pay or tax?
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  9. #9
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    When making programs you need to represent the 'objects' in it in different ways, like ages with integers, names with a series of characters, prices with floating point numbers etc...
    So why are there so many sizes of the 'same type' like short, int & long you ask? Well, you could make one universal size of say 256 bytes for every type to make it fit every (read: most) possible situation, but with variables that big memory would be consumed fast and calculations would be slow. So if you don't need to represent large numbers (ie just 1 - 10) you should just use a short, or even a char. But if you need to know the amount of milliseconds since a certain date 1970 you need to use a larger variable, ie a long (actually, long is a modifier to an int so it's long int but compilers usually (always?) understands if you type just long).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 08-11-2008, 11:02 PM
  2. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  3. Replies: 2
    Last Post: 03-24-2006, 07:36 PM
  4. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  5. Can't Find Conio.h?
    By drdroid in forum C++ Programming
    Replies: 27
    Last Post: 09-26-2002, 04:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21