Re: typedef declares object / enum to int

From: Michael Mair (Michael.Mair_at_invalid.invalid)
Date: 02/23/05


Date: Wed, 23 Feb 2005 20:57:20 +0100

Martin wrote:
> This post asks two questions, which I've illustrated in one C source file
> (see below), which clean compiles on my GNU compiler.
>
> 1. In K&R2, Section A8.9 it says "Declarations whose storage class specifier
> is typedef do not declare object." When I compile and run my sample code,
> the value "2" is displayed. Clearly the typedef has created the enumerators
> S1, S2, and S3. This appears to contradict K&R2.

Enumeration constants are exactly that: Constants.
They are not objects.

> 2. The program below was created to emulate a Lint diagnostic I got in a
> much larger project. In that project, the equivalent line to fn(SYMBOL)
> gives the warning
>
> Converting enum '{...}' to int
>
> which I find puzzling. Enumerators are int anyway, whereas the enumeration
> has an integral value (according to K&R2, Section A4.4). A warning about an
> enumerator (int) being converted to an int seems meaningless; and a warning
> about the enumeration (integral type) being converted to int doesn't make
> sense because it's the enumerator S3 that gets
> passed to fn(). I've reproduced Lint's explanation of the warning below.

To clarify:
Every enumerated type is distinct from every other enumerated or
integer type but is compatible to either char or a signed or unsigned
integer type. IOW: While the identifier S3 gives you a constant of type
int, the underlying enumerated type is not necessarily int.

Cheers
  Michael
> /* typedef declaration; Lint warning */
> #include <stdio.h>
>
> typedef enum { S1, S2, S3 } an_enum_t;
>
> #define SYMBOL S3
>
> typedef unsigned int Word;
> void fn(Word w)
> {
> /* deliberately empty */
> }
>
> int main( void )
> {
>
> fn(SYMBOL);
> printf("%i\n", SYMBOL);
> return 0; /* success */
> }
>
> This is Lint's description of the warning:
>
> 641 Converting enum to int -- An enumeration type was used in a context that
> required a computation such as an argument to an arithmetic operator or was
> compared with an integral argument. This warning will be suppressed if you
> use the integer model of enumeration (+fie) but you will lose some valuable
> type-checking in doing so. An intermediate policy is to simply turn off this
> warning. Assignment of int to enum will still be caught.
>
> This warning is not issued for a tagless enum without variables. For example
>
> enum {false,true};
>
> This cannot be used as a separate type. PC-lint/FlexeLint recognizes this
> and treats false and true as arithmetic constants.
>

-- 
E-Mail: Mine is an   /at/ gmx /dot/ de   address.


Relevant Pages

  • Off Topic: typedef declares object / enum to int
    ... which clean compiles on my GNU compiler. ... Enumerators are int anyway, whereas the enumeration ... > and a warning about the enumeration being converted to int ...
    (comp.lang.c)
  • typedef declares object / enum to int
    ... Clearly the typedef has created the enumerators ... gives the warning ... Enumerators are int anyway, whereas the enumeration ...
    (comp.lang.c)
  • Re: Functions not matching prototypes, redux
    ... extern int foo; ... aftercompiles "knowing" that the return is really float. ... declaration can be a simple warning and not an error. ... A constraint violation means a translator has to issue a diagnostic. ...
    (comp.lang.c)
  • function declaration overloading in C
    ... behaving C compiler would do. ... and using "/W4" to get the highest warning level. ... 'test' undefined; assuming extern returning int ... Even though the body uses an inparameter of 'int' it still compiles ...
    (comp.lang.c)
  • K&R2 - section 1.5.3 , exercise 1-9
    ... i have created solution which compiles and runs without any error/ ... warning but it does not work. ... int main ... scount = scount + 1; ...
    (comp.lang.c)