Incorrect C.  The difference is in the initializer.  In:

> const unsigned char gCompileHour=COMPILE_HOUR;
> const unsigned char gCompileMinute=COMPILE_MINUTE;
> const unsigned char gCompileSecond=COMPILE_SECOND;    

You are trying to set a global through the initializer to a variable
value.  At *compile time*, what is the value of TIME[0]?  Unknown.
Ergo, the compiler error.

In the functions:

> unsigned char GetCompileHour(void)
> {
>   unsigned char hour=COMPILE_HOUR;
>   return(hour);
> }

COMPILE_HOUR is run-time computable, and so works.


Best regards, 

Stu Bell 
DataPlay (DPHI, Inc.) 

 

> -----Original Message-----
> From: avr-gcc-list-bounces+sbell=dataplay....@nongnu.org 
> [mailto:avr-gcc-list-bounces+sbell=dataplay....@nongnu.org] 
> On Behalf Of Anton James Erasmus
> Sent: Wednesday, January 28, 2009 11:48 PM
> To: avr-gcc-list@nongnu.org
> Subject: [avr-gcc-list] Compiler bug or Incorrect C ?
> 
> Hi,
> 
> I have the following C code which gives the compiler error.
> "error: initializer element is not a constant"
> 
> #define COMPILE_HOUR (((__TIME__[0]-'0')*10) +
> (__TIME__[1]-'0'))
> #define COMPILE_MINUTE (((__TIME__[3]-'0')*10) +
> (__TIME__[4]-'0'))
> #define COMPILE_SECOND (((__TIME__[6]-'0')*10) +
> (__TIME__[7]-'0'))
> 
> const unsigned char gCompileHour=COMPILE_HOUR; const unsigned 
> char gCompileMinute=COMPILE_MINUTE;
> const unsigned char gCompileSecond=COMPILE_SECOND;    
> 
> but the following code compiles without problems and one can 
> see in the assembly generated that the macros do collapse to 
> a single constant.
> 
> #define COMPILE_HOUR (((__TIME__[0]-'0')*10) +
> (__TIME__[1]-'0'))
> #define COMPILE_MINUTE (((__TIME__[3]-'0')*10) +
> (__TIME__[4]-'0'))
> #define COMPILE_SECOND (((__TIME__[6]-'0')*10) +
> (__TIME__[7]-'0'))
> 
> 
> unsigned char GetCompileHour(void)
> {
>   unsigned char hour=COMPILE_HOUR;
>   return(hour);
> }
> 
> unsigned char GetCompileMinute(void)
> {
>   unsigned char minute=COMPILE_MINUTE;
>   return(minute);
> }
> 
> unsigned char GetCompileSecond(void)
> {
>   unsigned char second=COMPILE_SECOND;
>   return(second);
> }
> 
> 
> Generated assembly using WinAVR 2008-06-10:
> 
>         .file   "test.c"
> __SREG__ = 0x3f
> __SP_H__ = 0x3e
> __SP_L__ = 0x3d
> __CCP__  = 0x34
> __tmp_reg__ = 0
> __zero_reg__ = 1
>         .global __do_copy_data
>         .global __do_clear_bss
>         .text
> .global GetCompileHour
>         .type   GetCompileHour, @function
> GetCompileHour:
> /* prologue: function */
> /* frame size = 0 */
>         ldi r24,lo8(8)
> /* epilogue start */
>         ret
>         .size   GetCompileHour, .-GetCompileHour
> .global GetCompileMinute
>         .type   GetCompileMinute, @function
> GetCompileMinute:
> /* prologue: function */
> /* frame size = 0 */
>         ldi r24,lo8(40)
> /* epilogue start */
>         ret
>         .size   GetCompileMinute, .-GetCompileMinute
> .global GetCompileSecond
>         .type   GetCompileSecond, @function
> GetCompileSecond:
> /* prologue: function */
> /* frame size = 0 */
>         ldi r24,lo8(43)
> /* epilogue start */
>         ret
>         .size   GetCompileSecond, .-GetCompileSecond
> 
> 
> 
> So is the code that gives an error actually incorrect C, or 
> is it a compiler bug ?
> 
> Regards
>   Anton Erasmus
> 
> 
> _______________________________________________
> AVR-GCC-list mailing list
> AVR-GCC-list@nongnu.org
> http://lists.nongnu.org/mailman/listinfo/avr-gcc-list
> 


_______________________________________________
AVR-GCC-list mailing list
AVR-GCC-list@nongnu.org
http://lists.nongnu.org/mailman/listinfo/avr-gcc-list

Reply via email to