Thread: question regarding type long

  1. #1
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195

    question regarding type long

    I looked up long in the book "The C Programming Language", second edition by Kernighan and Richtie. How come on page 36 they say:

    "In addition; there are a number of qualifiers that can be applied to these basic data types. short and long apply to integers".


    But on page 211, section A8.2, they put both long and short as a
    type-specifier, not a type-qualifier.


    The sentence on page 36 seems to imply that long is being used as a type-qualifer and not a type-specifier. When using long in conjunction with long or int, does makes it a type-qualifier, but not a type-specifier?

  2. #2
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    I think you lost me but in any case a long is a type-specifier and there's no difference between long and long int.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    7
    I know for me, 'long' and 'int' are interchangable as a type-specifier, refering to a 4 byte unsigned data-type. However if I use long as a type-qualifier in front of int as a type-specifier, like 'long long int' or 'long long' (same thing), I get an 8 byte unsigned data-type. The first long being a type-qualifier and the second being the type-specifier.
    Last edited by breik; 03-16-2006 at 08:59 PM.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    I don't know about K&R C, but C89 has this:
    type specifier
    type qualifier
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    7
    Perhaps this code can help clear things up:
    Code:
    #include <stdio.h>
    
    int main()
    {
            printf("size of int: %i\n", sizeof(int));
            printf("size of long: %i\n", sizeof(long));
            printf("size of long long: %i\n", sizeof(long long));
            printf("size of long long int: %i\n", sizeof(long long int));
    
            return 0;
    }
    When run displays:
    size of int: 4
    size of long: 4
    size of long long: 8
    size of long long int: 8

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You mean: "when runs displays on my computer" ... it doesn't have to display those values for any of the types listed.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    Quote Originally Posted by Dave_Sinkula
    I don't know about K&R C, but C89 has this:
    type specifier
    type qualifier
    In the book "The C Programming Language",2nd edition by K & R, they use hyphens

    ie not type qualifier
    but
    type-qualifier

    I was just following the convention of the book.
    Last edited by cdalten; 03-16-2006 at 09:13 PM.

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by breik
    Perhaps this code can help clear things up:
    Code:
    #include <stdio.h>
    
    int main()
    {
            printf("size of int: %i\n", sizeof(int));
            printf("size of long: %i\n", sizeof(long));
            printf("size of long long: %i\n", sizeof(long long));
            printf("size of long long int: %i\n", sizeof(long long int));
    
            return 0;
    }
    When run displays:
    size of int: 4
    size of long: 4
    size of long long: 8
    size of long long int: 8
    Or it doesn't. Great, start those bad habits early and litter your code with them. Only later when your ass is bitten will you think yourself a fool.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    Dave, would this have made you happier

    Code:
    #include <stdio.h>
    
    int main(void)
    {
            printf("size of int: %i\n", sizeof(int));
            printf("size of long: %u\n", sizeof(long));
            printf("size of long long: %u\n", sizeof(long long));
            printf("size of long long int: %u\n", sizeof(long long int));
    
            return 0;
    }

  10. #10
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    Quote Originally Posted by breik
    I know for me, 'long' and 'int' are interchangable as a type-specifier, refering to a 4 byte unsigned data-type. However if I use long as a type-qualifier in front of int as a type-specifier, like 'long long int' or 'long long' (same thing), I get an 8 byte unsigned data-type. The first long being a type-qualifier and the second being the type-specifier.
    Okay, the explanation makes sense.

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by cdalten
    Dave, would this have made you happier

    Code:
    #include <stdio.h>
    
    int main(void)
    {
            printf("size of int: %i\n", sizeof(int));
            printf("size of long: %u\n", sizeof(long));
            printf("size of long long: %u\n", sizeof(long long));
            printf("size of long long int: %u\n", sizeof(long long int));
    
            return 0;
    }
    Not really (size_t with pre-C99 printf has a general workaround other than this).

    The results tell you what your setup will do. All could be 1, but that might not clarify anything. And it certainly distracts from your original question.

    Did you follow those links?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  12. #12
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    Yes I followed those links. I'm not that sure that I was able to fully comprehend it though.

  13. #13
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    These are type specifiers:
    void
    char
    short
    int
    long
    float
    double
    signed
    unsigned
    struct-or-union-specifier
    enum-specifier
    typedef-name
    These are type qualifiers:
    const
    volatile
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  14. #14
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    Okay, maybe I'm being dense, but the quote from the book

    "In addition; there are a number of qualifiers that can be applied to these basic data types. short and long apply to integers".

    Implies that long and short (in front of an int) is a type-qualifier, but not a type-specifier
    Last edited by cdalten; 03-16-2006 at 09:34 PM.

  15. #15
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Are you sure it's not saying the the following are possible?
    Code:
    const unsigned long a = 0x2345678UL;
    volatile int b;
    const volatile unsigned short c = 0x1234;
    [edit]And you may want to go by the first standard, rather than pre-standard stuff.
    Last edited by Dave_Sinkula; 03-16-2006 at 09:39 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inserting a swf file in a windows application
    By face_master in forum Windows Programming
    Replies: 12
    Last Post: 05-03-2009, 11:29 AM
  2. Can you check what is wrong with this code
    By Ron in forum C++ Programming
    Replies: 4
    Last Post: 08-01-2008, 10:59 PM
  3. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. need help
    By emperor in forum C Programming
    Replies: 1
    Last Post: 03-04-2002, 12:26 PM