More amusing C grammer

Discussion in 'C Programming' started by Colin King, Oct 10, 2005.

  1. Colin King

    Colin King Guest

    The following code also shows some amusing C grammar features:

    /* typedef unsigned int uint_t ... */
    int typedef unsigned uint_t;

    /* defaults to int type in typedef... */
    typedef int_t;

    main()
    {
    uint_t x ;
    int_t i ;
    }
     
    Colin King, Oct 10, 2005
    #1
    1. Advertising

  2. Colin King

    Simon Biber Guest

    Colin King wrote:
    > The following code also shows some amusing C grammar features:
    >
    > /* typedef unsigned int uint_t ... */
    > int typedef unsigned uint_t;


    This is just changing the order of the qualifiers, which is quite
    acceptable. It's just like how there's no difference between `const char
    *' and `char const *'.

    > /* defaults to int type in typedef... */
    > typedef int_t;


    As far as I know, this is an example of implicit int, which was removed
    in C99. The code is no longer valid C.

    --
    Simon.
     
    Simon Biber, Oct 11, 2005
    #2
    1. Advertising

  3. Colin King

    Skarmander Guest

    Simon Biber wrote:
    > Colin King wrote:
    >
    >> The following code also shows some amusing C grammar features:
    >>
    >> /* typedef unsigned int uint_t ... */
    >> int typedef unsigned uint_t;

    >
    >
    > This is just changing the order of the qualifiers, which is quite
    > acceptable. It's just like how there's no difference between `const char
    > *' and `char const *'.
    >

    Errr... what? 'typedef' is a qualifier now?

    It's one thing to say 'typedef int unsigned uint_t' is just the same.
    'int typedef unsigned uint_t' is another matter. If the C grammar really
    considers typedef to be in the same category as 'const'... it's weird.

    Not that weirdness is weird, when it comes to C.

    S.
     
    Skarmander, Oct 11, 2005
    #3
  4. Colin King

    Simon Biber Guest

    Skarmander wrote:
    > Simon Biber wrote:
    >
    >> Colin King wrote:
    >>
    >>> The following code also shows some amusing C grammar features:
    >>>
    >>> /* typedef unsigned int uint_t ... */
    >>> int typedef unsigned uint_t;

    >>
    >>
    >>
    >> This is just changing the order of the qualifiers, which is quite
    >> acceptable. It's just like how there's no difference between `const
    >> char *' and `char const *'.
    >>

    > Errr... what? 'typedef' is a qualifier now?


    No, actually. To be more accurate, according to the C grammar, `typedef'
    is a storage-class specifier. As part of a declaration, storage-class
    specifiers can be mixed in any order with type specifiers, type
    qualifiers, or the function specifier `inline'. See C99 6.7 and 6.7.1.

    > It's one thing to say 'typedef int unsigned uint_t' is just the same.
    > 'int typedef unsigned uint_t' is another matter. If the C grammar really
    > considers typedef to be in the same category as 'const'... it's weird.
    >
    > Not that weirdness is weird, when it comes to C.


    Yes, C grammar is weird.

    --
    Simon.
     
    Simon Biber, Oct 11, 2005
    #4
  5. Colin King

    Skarmander Guest

    Simon Biber wrote:
    > Skarmander wrote:
    >
    >> Simon Biber wrote:
    >>
    >>> Colin King wrote:
    >>>
    >>>> The following code also shows some amusing C grammar features:
    >>>>
    >>>> /* typedef unsigned int uint_t ... */
    >>>> int typedef unsigned uint_t;
    >>>
    >>>
    >>>
    >>>
    >>> This is just changing the order of the qualifiers, which is quite
    >>> acceptable. It's just like how there's no difference between `const
    >>> char *' and `char const *'.
    >>>

    >> Errr... what? 'typedef' is a qualifier now?

    >
    >
    > No, actually. To be more accurate, according to the C grammar, `typedef'
    > is a storage-class specifier. As part of a declaration, storage-class
    > specifiers can be mixed in any order with type specifiers, type
    > qualifiers, or the function specifier `inline'. See C99 6.7 and 6.7.1.
    >

    Completely bizarre, yet making sense in some perverse way. That would
    mean that in 'typedef const volatile unsigned long int cvulong_t', the
    first six words can appear in any order, right?

    S.
     
    Skarmander, Oct 11, 2005
    #5
  6. On Tue, 11 Oct 2005 02:13:49 +0200, Skarmander
    <> wrote:
    <snip>
    > Completely bizarre, yet making sense in some perverse way. That would
    > mean that in 'typedef const volatile unsigned long int cvulong_t', the
    > first six words can appear in any order, right?
    >

    Yes. Placing the sc-spec (in this case typedef) anyplace other than
    first is officially 'obsolescent' (aka deprecated) since C90, but was
    not removed in C99. What may happen in C>=0x is unknown.

    There are reasonable arguments for placing the qualifiers before type
    and also reasonable arguments for placing them after, so I expect the
    Standard will forever allow both. I suppose it might (be changed to)
    allow only before or after but not mixed, but that probably doesn't
    benefit implementors and arguably doesn't help users very much.
    Moreover it 'feels' inconsistent with the feature that you can have
    qualifiers in a typedef type already or add them where it is used.

    Similarly there are reasonable arguments for different orders among
    the qualifiers, and among the type-specs (signed long vs long singed).

    - David.Thompson1 at worldnet.att.net
     
    Dave Thompson, Oct 17, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TLOlczyk

    Grammer for crappy HTML.

    TLOlczyk, Jul 2, 2004, in forum: HTML
    Replies:
    4
    Views:
    474
    Whitecrest
    Jul 2, 2004
  2. Venky
    Replies:
    0
    Views:
    557
    Venky
    Jun 27, 2005
  3. Bob Nelson

    Amusing Microsoft C++ ad

    Bob Nelson, Oct 8, 2003, in forum: C++
    Replies:
    12
    Views:
    574
    Attila Feher
    Oct 13, 2003
  4. Active8

    amusing error message

    Active8, Feb 25, 2005, in forum: C++
    Replies:
    0
    Views:
    346
    Active8
    Feb 25, 2005
  5. Ark

    Amusing C, amusing compiler

    Ark, Oct 6, 2006, in forum: C Programming
    Replies:
    29
    Views:
    742
    Elijah Cardon
    Oct 24, 2006
Loading...

Share This Page