What is the differnece between long and int?

This is a discussion on What is the differnece between long and int? within the C Programming forums, part of the General Programming Boards category; What is the differnece between long and int?...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    61

    What is the differnece between long and int?

    What is the differnece between long and int?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Sometimes long is bigger than int - are you doing a test or something?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    long is always 4 bytes.
    int is 2 bytes on 16-bit systems, 4-bytes on 32-bit systems, and I'm guessing it's 8 bytes on 64-bit systems.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by cpjust View Post
    long is always 4 bytes.
    int is 2 bytes on 16-bit systems, 4-bytes on 32-bit systems, and I'm guessing it's 8 bytes on 64-bit systems.
    There's no guarantee that Long is any particular size. It should be at least as long as an int. Most 64-bit systems have 64-bit longs and 32-bit ints, for example. long that is shorter than int is not allowed by the C standard.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,823
    The C standard specifies what the minimum requirements are for each data type.
    Your compiler's limits.h file tells you what you've really got in terms of data type ranges.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    61

    Why it doesn't give compilation error?

    inser
    Code:
    int p=10;
    int p;
    int main()
    {
     printf("%d",p)
     getch();
      return 0;
    }
    inser
    The output is 10. Actually p is declared twice in a file. Hence, it should give compile time error. Then, why we get the output 10?

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,010
    What compiler?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Hence, it should give compile time error.
    What makes you think that? C allows an object to be declared multiple times, and one of those declarations will be used as a definition. So in the end, there's really only one object. If you try to initialize more than one of the declarations, that constitutes a multiple definition, and you would get an error, but the following is perfectly legal C:
    Code:
    #include <stdio.h>
    
    int x; int x; int x;
    int x; int x; int x;
    int x; int x; int x;
    int x; int x; int x;
    int x; int x; int x;
    int x; int x; int x;
    int x; int x; int x;
    int x; int x; int x;
    
    int main ( void )
    {
      x = 12345;
    
      printf ( "%d\n", x );
    
      return 0;
    }
    So is this:
    Code:
    #include <stdio.h>
    
    int x; int x; int x;
    int x; int x; int x;
    int x; int x; int x;
    int x; int x = 12345; int x;
    int x; int x; int x;
    int x; int x; int x;
    int x; int x; int x;
    int x; int x; int x;
    
    int main ( void )
    {
      printf ( "%d\n", x );
    
      return 0;
    }
    But this is not:
    Code:
    #include <stdio.h>
    
    int x = 12345;
    int x = 12345;
    
    int main ( void )
    {
      printf ( "%d\n", x );
    
      return 0;
    }
    There's no benign redefinition rule, so even if the initialization values are identical, it's still seen as multiple definitions. Only the third example will give you an error.
    My best code is written with the delete key.

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Why would you exhume a 4 month old thread to ask a different question?

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I think cpjust has a point - could one of the moderators dig out the other thread with exactly the same question, and perhaps merge the content of this thread to here.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 04:45 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 10:42 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 12:58 PM
  5. A Simple (?) Problem
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 10-12-2001, 05:28 AM

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