Results 1 to 5 of 5

Thread: How to know the length of an integer in C++

  1. #1
    Join Date
    Jan 2010
    Posts
    47

    How to know the length of an integer in C++

    I recently started programming in C++ after a long time because I want to test something in C++. In my code I need to know the length of an integer. The reason for this is that I want to recheck whether I should go for "signed int" or "unsigned int" so that there does not occur any errors or warnings. I remember we could that in VB using the len function, but I didn't remember how to do that in C++? Can anyone help me to remember the correct function that I should use to get the length of integer?

  2. #2
    Join Date
    Nov 2005
    Posts
    1,323

    Re: How to know the length of an integer in C++

    In general the size of the predefined integer data type depends on the compiler. Not only compiler but even machine decides the size of short, int, long, float and double data types. Ideally in any system (may be it is 32-bit or 64-bit), the int is always of 32-bit.

  3. #3
    Join Date
    Apr 2008
    Posts
    1,948

    Re: How to know the length of an integer in C++

    The standard says that "signed int" should be of "-32767 to 32767" on 32 bit operating system and "unsigned int" should be of "0 to 65535". So depending on the input that you want from the user, you can use any of those. But if you thing the value can exceed any of those then you can even use "long" or "long long".

  4. #4
    Join Date
    Jan 2010
    Posts
    47

    Re: How to know the length of an integer in C++

    Thank you both of you but this is not what I want. I know that the size of int depends on the architecture (16 bits, 32 bits, 64 bits) and also the compiler but if I remember correctly then I think there was some method/function to know the size of any data type. I just want to know about that. Can you help me in this?

  5. #5
    Join Date
    Apr 2008
    Posts
    1,948

    Re: How to know the length of an integer in C++

    If you just want to know the size of any data type then you can use sizeof() function. It will give you the correct size of your data type based on your compiler as well as operating system. By the way, if you are not sure about the value inserted by the user but then also want to handle the situation with your code then you can use try catch block in your code. It will not stop anywhere in your code and will result based on your catch block when exception occurs.

Similar Threads

  1. Integer range of CPU
    By Udyan in forum Motherboard Processor & RAM
    Replies: 6
    Last Post: 31-07-2010, 06:07 AM
  2. Convert int to Integer type
    By Angelica Maria in forum Software Development
    Replies: 5
    Last Post: 12-03-2010, 11:59 AM
  3. integer to string conversion
    By cyber-noob in forum Software Development
    Replies: 4
    Last Post: 03-03-2010, 10:26 PM
  4. Integer length problem
    By NetworkeR in forum Software Development
    Replies: 2
    Last Post: 13-11-2009, 02:13 PM
  5. Verillo's integer and reg?
    By Fason in forum Networking & Security
    Replies: 3
    Last Post: 25-10-2008, 01:50 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,645,934.77675 seconds with 17 queries