Thread: What's difference between int and long type ?

  1. #1
    Registered User
    Join Date
    Jul 2020
    Posts
    28

    What's difference between int and long type ?

    Hi

    I don't understand difference between int and long type ?

    Code:
    #include <stdio.h>      int main() {
      int x = 1;
      long y  = 1;
      
    
    
      printf("size of int  = %d bytes\n", sizeof(x));
      printf(" X = %d \n", x);
     
      printf("size of long  = %d bytes\n", sizeof(y));
      printf(" Y = %d \n", y);
      
      return 0;
    }
    size of int = 4 bytes
    X = 1
    size of long = 4 bytes
    Y = 1

    gcc compiler allocate 4 bytes for both type int and long and they both store decimal number

    What's difference between int and long type ?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Their guaranteed minimum ranges are different:
    INT_MAX +32767
    INT_MIN -32767
    LONG_MAX +2147483647
    LONG_MIN -2147483647

    On some implementations these days, sizeof(long) might be 8 instead of 4. And of course, even if they happen to be identical in range and bytes as given by sizeof on a given implementation, they nonetheless remain different types.

    Incidentally, you should be aware that the format specifiers for the type of the result of sizeof and for long aren't %d:
    Code:
    printf("size of int  = %zu bytes\n", sizeof(x));
    printf(" X = %d \n", x);
    
    printf("size of long  = %zu bytes\n", sizeof(y));
    printf(" Y = %ld \n", y);
    Last edited by laserlight; 11-02-2020 at 12:29 AM.
    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. Replies: 6
    Last Post: 03-05-2018, 09:59 AM
  2. Generate unsigned long long type random numbers
    By patishi in forum C Programming
    Replies: 27
    Last Post: 09-11-2013, 09:03 PM
  3. 'long long' type and printf() (on AIX 5.3)
    By alex5161 in forum C Programming
    Replies: 5
    Last Post: 12-16-2011, 04:05 PM
  4. Replies: 1
    Last Post: 10-11-2010, 01:53 AM
  5. Is long long int a valid Data Type
    By shiv_tech_quest in forum C Programming
    Replies: 2
    Last Post: 11-12-2003, 08:59 AM

Tags for this Thread