comp.lang.c
[Top] [All Lists]

Re: value of the constant expression 1<<(1?1:1) < 0x9999

Subject: Re: value of the constant expression 1<<(1?1:1) < 0x9999
From: Peter Nilsson
Date: Tue, 29 Apr 2008 15:35:06 -0700 PDT
Newsgroups: comp.lang.c

Francois Grieu wrote:
> Hello,
>
> one of my C compiler (Keil C51) evaluates the constant expression
>       1<<(1?1:1) < 0x9999
> to the value 1.

Correct...

  1 << (1?1:1) < 0x9999
  (1 << (1?1:1)) < 0x9999
  (1 << 1) < 0x9999
  2 < 0x9999
  1

> // this returns 0, much to my surprise
> unsigned char bug4_a(void)
>       {
>       return 1<<(1?1:1) < 0x9999;
>       }
>
> Can this find a satisfactory explanation under some definition of the
> C language ?

No.

> Note: I still get 0 for
>
>       return 1<<(1?1:1) < (unsigned)0x9999;
>
>       return 1<<(unsigned char)(1?1:1) < 0x9999;

Integer promotion must be applied, but it is incidental. They
should all return 1 on a conforming implementation.

--
Peter

<Prev in Thread] Current Thread [Next in Thread>
Privacy Policy