Thread: different size of integer

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    99

    different size of integer

    Integer type come with different size such as signed, unsigned, short, long, double

    Is it possible to find out size of integer because I don't see any way.
    Code:
    #include<stdio.h>
    int main (void)
    {
      int x = 5;
      signed int y = 20;
      unsigned int z = 4;
      
      long int a = 10;
      long unsigned int b = 20;
      long signed int c = 30 ;
      
      short int k = 8;
      short unsigned int l = 12;
      short signed int m = 16;
      
      printf(" print value of x %d \n",x);
      printf(" print value of y %d \n",y);
      printf(" print value of z %d \n \n",z);
      
      printf(" print value of a %d \n",a);
      printf(" print value of b %d \n",b);
      printf(" print value of c %d \n \n",c);
      
      printf(" print value of k %d \n",k);
      printf(" print value of l %d \n",l);
      printf(" print value of m %d \n \n",m);
      
      return 0; 
    }
    print value of x 5
    print value of y 20
    print value of z 4
    print value of a 10
    print value of b 20
    print value of c 30
    print value of k 8
    print value of l 12
    print value of m 16

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Use sizeof, e.g.,
    Code:
    printf("size of int: %u\n", (unsigned int)sizeof(int));
    or if you are compiling with respect to C99 or later:
    Code:
    printf("size of int: %zu\n", sizeof(int));
    By the way, double is not an integer type.
    Last edited by laserlight; 01-23-2018 at 11:23 PM.
    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
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Also, what may be far more important is the range of a given type. There are constants for them defined in limits.h if you actually need them for programming reasons. If you just want to know, say, the largest number an integer type can hold, do some math and you will have a reasonable guess, and a guess that will always be true on two's compliment systems.

    The maximum value an integer type x can hold is 2^(sizeof(x)*CHAR_BIT) if it is unsigned. Subtract 1 from that if the type is signed.

    CHAR_BIT is the number of bits to a byte. So, the maximum of a long may be 2^(4*8) - 1, also known as 2^31, also known as 2,147,483,648.
    Last edited by whiteflags; 01-23-2018 at 11:35 PM.

  4. #4
    Registered User
    Join Date
    Apr 2012
    Posts
    99
    but compiler show same size for every types
    Code:
    #include<stdio.h>
    int main (void)
    {
      int x = 5;
      signed int y = 20;
      unsigned int z = 4;
      
      long int a = 10;
      unsigned long int b = 20;
      signed long int c = 30 ;
      
      short int k = 8;
      unsigned short int l = 12;
      signed short int m = 16;
      
      printf("size of signed int : %u \n", (signed int)sizeof(int));
      printf("size of unsigned int : %u \n", (unsigned int)sizeof(int));
      
      printf("size of long int : %ld \n", (long int)sizeof(int));
      printf("size of signed long int : %u \n", (signed long int)sizeof(int));
      printf("size of unsigned long int : %lu \n", (unsigned long int)sizeof(int));
     
      printf("size of short int : %hd \n", (short int)sizeof(int));
      printf("size of signed short int : %u \n", (signed short int)sizeof(int));
      printf("size of unsigned short int : %hu \n", (unsigned short int)sizeof(int));
      
      return 0; 
    }
    size of signed int : 4
    size of unsigned int : 4
    size of long int : 4
    size of signed long int : 4
    size of unsigned long int : 4
    size of short int : 4
    size of signed short int : 4
    size of unsigned short int : 4

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by vead
    but compiler show same size for every types
    No, compiler is showing the same result for sizeof(int) no matter how many times you use it. Obviously, this is your typo error, not the compiler's fault.
    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

  6. #6
    Registered User
    Join Date
    Apr 2012
    Posts
    99
    Quote Originally Posted by laserlight View Post
    No, compiler is showing the same result for sizeof(int) no matter how many times you use it. Obviously, this is your typo error, not the compiler's fault.
    Program
    Code:
    #include<stdio.h>
    int main (void)
    {
      int x = 5;
      signed int y = 20;
      unsigned int z = 4;
      
      long int a = 10;
      unsigned long int b = 20;
      signed long int c = 30 ;
      
      short int k = 8;
      unsigned short int l = 12;
      signed short int m = 16;
      
      printf("size of signed int : %d \n", (signed int)sizeof(signed int));
      printf("size of unsigned int : %u \n", (unsigned int)sizeof(unsigned int));
      
      printf("size of long int : %li \n", (long int)sizeof(long int));
      printf("size of signed long int : %li \n", (signed long int)sizeof(signed long int));
      printf("size of unsigned long int : %lu \n", (unsigned long int)sizeof(unsigned long int));
     
      printf("size of short int : %hi \n", (short int)sizeof(short int));
      printf("size of signed short int : %hi \n", (signed short int)sizeof(signed short int));
      printf("size of unsigned short int : %hu \n", (unsigned short int)sizeof(unsigned short int));
      
      return 0; 
    }
    size of signed int : 4 //does it means it can be store 4 bytes data

    size of unsigned int : 4
    size of long int : 4
    size of signed long int : 4
    size of unsigned long int : 4

    size of short int : 2
    size of signed short int : 2 //does it means it can be store 2 bytes data
    size of unsigned short int : 2

  7. #7
    Registered User
    Join Date
    Jun 2017
    Posts
    157
    Why don't you use the Fixed width integer types, then you know exctly the size of your types.
    Fixed width integer types (since C99) - cppreference.com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. warning: cast to pointer from integer of different size
    By DavidDobson in forum C Programming
    Replies: 6
    Last Post: 12-03-2008, 06:37 PM
  2. size of an integer pointer
    By onebrother in forum C Programming
    Replies: 5
    Last Post: 07-09-2008, 11:49 AM
  3. integer array of size 17000
    By Pearl in forum C Programming
    Replies: 2
    Last Post: 08-13-2004, 04:54 AM
  4. C integer type Size
    By [guard] in forum C Programming
    Replies: 8
    Last Post: 09-26-2002, 03:43 PM
  5. size of integer arrays
    By steve8820 in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2002, 07:31 PM

Tags for this Thread