Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #152063

Re: longer 'char literals' meaning in c

From Keith Thompson <Keith.S.Thompson+u@gmail.com>
Newsgroups comp.lang.c
Subject Re: longer 'char literals' meaning in c
Date 2020-05-05 16:18 -0700
Organization None to speak of
Message-ID <87d07i0y4r.fsf@nosuchdomain.example.com> (permalink)
References (8 earlier) <r8qd3b$hmi$1@z-news.wcss.wroc.pl> <r8qntq$p8s$1@dont-email.me> <r8s6lq$k31$1@z-news.wcss.wroc.pl> <87h7wu15dv.fsf@nosuchdomain.example.com> <r8spca$q9$1@z-news.wcss.wroc.pl>

Show all headers | View raw


antispam@math.uni.wroc.pl writes:
> Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
>> antispam@math.uni.wroc.pl writes:
[...]
>> >                                        In particluar,
>> > it seems that compiler is allowed to reject at compile
>> > time literals which otherwise would represent legal
>> > runtime values.
>> 
>> I don't believe that's correct, but I'm not entirely sure what you
>> mean.  Can you provide an example (even a hypothetical one)?
>
> Two hypotetical possiblilities:
>
> - "broken" scanner, that could not produce all legal values.
>   I am not aware of any major compiler with such problem, but
>   some toy compilers had such problem.
>   Not likeley on normal machines, where scanner must handle
>   minimal range of long long, but possible for machine
>   with 128-bit interger type, but 64-bit literals.
> - simple code generator for 32-bit machine with 16-bit immediates.
>   Such code generator may simply embed constant literals as
>   immediates and reject any integer literal that does not fit
>   in 16 bits.  Assuming no constant folding compiler still would
>   be able to produce values of 32-bit constant expressions.
>   Point is that turning literal into expression is a bruden
>   on code generator and implementer may be tempted to say
>   that bigger literals are not implemented.  In the past
>   compiler had various crazy limits for no better reason.

Any such compiler would simply be non-conforming and buggy.
A conforming compiler is not "allowed" to reject constants that are
within the range of long long.  Equivalently, a compiler that does
so is not conforming.

>> >                  I admit that that this looks undesirable,
>> > but ATM compiler which implements INT_MAX as expression
>> > and is unable to handle it as literal value looks for
>> > me as poor quality but legal implementation.
>> 
>> INT_MAX must be a constant expression of type int.  N1570 5.2.4.2.1p1:
>> 
>>     The values given below shall be replaced by constant expressions
>>     suitable for use in #if preprocessing directives. Moreover,
>>     except for CHAR_BIT and MB_LEN_MAX, the following shall be
>>     replaced by expressions that have the same type as would an
>>     expression that is an object of the corresponding type converted
>>     according to the integer promotions.
>
> Consider
>
> #define INT_MAX (25575*41*2048 + 2047)

OK, that's a constant expression of type int, so that's a conforming but
silly way to define INT_MAX.

> or
>
> #define INT_MAX __int_max_val__
>
> where __int_max_val__ is really a variable, but compiler and
> preprocessor magic means that during translation it is treated
> as a constant.

Then it's a constant, isn't it?

>                 Neither uses literals bigger than 16-bits
> and implementation can handle each of them in way required by
> 5.2.4.2.1p1.

Again, any conforming implementation must handle the constant 2147483647
correctly.  If INT_MAX >= 2147483647 then it's of type int; otherwise
it's of type long.  There is simply no wiggle room.

(Unless you're talking about non-conforming or buggy compilers, but if
so I don't see the point.)

>> [...]
>> 
>> > 4.3 says "...correct in all other aspects, ...".  So this means
>> > that merely containing unspecified behavior is not valid reason
>> > to reject program.  But this does not imply that choice of implementation
>> > defined value have to be done in way that makes program "correct in
>> > all other aspects".  For example if implementation defined value
>> > is used in arithmetic, then implementation is _not_ forced to
>> > find values that avoid undefined behaviour (or other trouble).
>> > And for me 4.5 and 4.6 together mean that it is responsiblity
>> > of program (programmer) to avoid exceeding implementation limits.
>> > In other word when for given program implementation defined rule
>> > leads to behaviur that exceed capability of given implementation
>> > then implementation is still conforming: it does not reject
>> > program due to "unspecified behavior", but because it can
>> > not handle it...  Of course, this leads to question if
>> > implementation limit is reasonable.  Clearly saying "this
>> > implementation is unable to handle unspecified behavior"
>> > is a cheat.  But saying "this implementation can not fit
>> > 5 chars into space of 4 chars" is pretty reasonable limit.
>> 
>> Given that character constants are of type int, I'm not at all
>> convinced by an argument that the implementation can say that
>> some character constants have values that are outside the range of
>> type int.
>
> Well, 3 character constant is representable as int, but outside
> minimal limits of integer type.

Let's assume 16-bit int.  How do you conclude that the value of 'ABC' is
outside the range of int?  That's the case only if the implementation
*chooses* such a mapping.  Since the standard says nothing about what
mapping is to be used (only that it's implementation-defined), and since
it requires the value of a character constant to be of type int, my
argument is that such a mapping is non-conforming.  (It's not a 100%
firm argument.  It's possible the authors of the standard just didn't
think about that possibility one way or the other.)

>                                  If you agree with previous
> part, then implementation can say that it exceeds implementation
> limit.  Or, to be more reasonable it can say that INT_MIN
> exceeds its capability (everybody knows that correct handling
> of INT_MIN may be problematic).

Since there are no negative integer constants, defining INT_MIN is
*slightly* tricky.  With 16-bit int, this:
    #define INT_MIN (-32768)
is non-conforming.  Solutions are well known, for example:
    #define INT_MIN (-32767-1)
which meets the requirements of the standard.  What's the problem?

[...]

-- 
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips Healthcare
void Void(void) { Void(); } /* The recursive call of the void */

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

longer 'char literals' meaning in c fir <profesor.fir@gmail.com> - 2020-04-30 04:43 -0700
  Re: longer 'char literals' meaning in c Bonita Montero <Bonita.Montero@gmail.com> - 2020-04-30 13:55 +0200
    Re: longer 'char literals' meaning in c fir <profesor.fir@gmail.com> - 2020-04-30 05:17 -0700
      Re: longer 'char literals' meaning in c fir <profesor.fir@gmail.com> - 2020-04-30 05:22 -0700
        Re: longer 'char literals' meaning in c fir <profesor.fir@gmail.com> - 2020-04-30 05:24 -0700
          Re: longer 'char literals' meaning in c fir <profesor.fir@gmail.com> - 2020-04-30 05:29 -0700
            Re: longer 'char literals' meaning in c fir <profesor.fir@gmail.com> - 2020-04-30 06:16 -0700
        Re: longer 'char literals' meaning in c fir <profesor.fir@gmail.com> - 2020-04-30 05:44 -0700
      Re: longer 'char literals' meaning in c Bart <bc@freeuk.com> - 2020-04-30 14:00 +0100
        Re: longer 'char literals' meaning in c fir <profesor.fir@gmail.com> - 2020-04-30 06:13 -0700
          Re: longer 'char literals' meaning in c Bart <bc@freeuk.com> - 2020-04-30 14:41 +0100
            Re: longer 'char literals' meaning in c fir <profesor.fir@gmail.com> - 2020-04-30 07:18 -0700
              Re: longer 'char literals' meaning in c fir <profesor.fir@gmail.com> - 2020-04-30 07:31 -0700
                Re: longer 'char literals' meaning in c Spiros Bousbouras <spibou@gmail.com> - 2020-04-30 15:49 +0000
                Re: longer 'char literals' meaning in c fir <profesor.fir@gmail.com> - 2020-04-30 09:12 -0700
              Re: longer 'char literals' meaning in c Bart <bc@freeuk.com> - 2020-04-30 15:49 +0100
                Re: longer 'char literals' meaning in c fir <profesor.fir@gmail.com> - 2020-04-30 08:31 -0700
        Re: longer 'char literals' meaning in c Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-04-30 14:00 -0700
          Re: longer 'char literals' meaning in c Spiros Bousbouras <spibou@gmail.com> - 2020-04-30 21:16 +0000
            Re: longer 'char literals' meaning in c Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-04-30 15:22 -0700
            Re: longer 'char literals' meaning in c James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-04-30 18:36 -0400
          Re: longer 'char literals' meaning in c Bart <bc@freeuk.com> - 2020-04-30 22:25 +0100
            Re: longer 'char literals' meaning in c Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-04-30 15:30 -0700
              Re: longer 'char literals' meaning in c Bart <bc@freeuk.com> - 2020-05-01 00:01 +0100
                Re: longer 'char literals' meaning in c fir <profesor.fir@gmail.com> - 2020-04-30 16:15 -0700
                Re: longer 'char literals' meaning in c fir <profesor.fir@gmail.com> - 2020-04-30 16:29 -0700
                Re: longer 'char literals' meaning in c Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-04-30 17:15 -0700
                Re: longer 'char literals' meaning in c Bart <bc@freeuk.com> - 2020-05-01 01:47 +0100
                Re: longer 'char literals' meaning in c James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-04-30 18:38 -0700
                Re: longer 'char literals' meaning in c Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-04-30 18:55 -0700
                Re: longer 'char literals' meaning in c Bart <bc@freeuk.com> - 2020-05-01 11:35 +0100
                Re: longer 'char literals' meaning in c James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-05-01 05:47 -0700
                Re: longer 'char literals' meaning in c fir <profesor.fir@gmail.com> - 2020-05-01 06:15 -0700
                Re: longer 'char literals' meaning in c Bart <bc@freeuk.com> - 2020-05-01 15:53 +0100
                Re: longer 'char literals' meaning in c Richard Damon <Richard@Damon-Family.org> - 2020-05-01 12:38 -0400
                Re: longer 'char literals' meaning in c Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-05-01 09:57 -0700
                Re: longer 'char literals' meaning in c Bart <bc@freeuk.com> - 2020-05-01 20:50 +0100
                Re: longer 'char literals' meaning in c Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-05-01 13:11 -0700
                Re: longer 'char literals' meaning in c jameskuyper@stellarscience.com - 2020-05-01 12:05 -0700
                Re: longer 'char literals' meaning in c James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-05-01 19:08 -0400
                Re: longer 'char literals' meaning in c Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-05-01 09:51 -0700
                Re: longer 'char literals' meaning in c David Brown <david.brown@hesbynett.no> - 2020-05-02 15:22 +0200
                Re: longer 'char literals' meaning in c Richard Damon <Richard@Damon-Family.org> - 2020-05-02 12:13 -0400
                Re: longer 'char literals' meaning in c Les Cargill <lcargill99@comcast.com> - 2020-05-04 00:17 -0500
                Re: longer 'char literals' meaning in c James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-05-04 10:51 -0400
                Re: longer 'char literals' meaning in c James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-05-04 10:23 -0400
                Re: longer 'char literals' meaning in c David Brown <david.brown@hesbynett.no> - 2020-05-04 16:48 +0200
                Re: longer 'char literals' meaning in c James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-05-04 12:39 -0400
                Re: longer 'char literals' meaning in c David Brown <david.brown@hesbynett.no> - 2020-05-04 19:37 +0200
                Re: longer 'char literals' meaning in c James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-05-04 15:03 -0400
                Re: longer 'char literals' meaning in c David Brown <david.brown@hesbynett.no> - 2020-05-05 08:33 +0200
                Re: longer 'char literals' meaning in c James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-05-05 09:47 -0400
                Re: longer 'char literals' meaning in c David Brown <david.brown@hesbynett.no> - 2020-05-05 16:22 +0200
                Re: longer 'char literals' meaning in c Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-05-04 10:27 -0700
                Re: longer 'char literals' meaning in c antispam@math.uni.wroc.pl - 2020-05-05 00:47 +0000
                Re: longer 'char literals' meaning in c Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-05-04 20:08 -0700
                Re: longer 'char literals' meaning in c James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-05-04 23:52 -0400
                Re: longer 'char literals' meaning in c antispam@math.uni.wroc.pl - 2020-05-05 17:10 +0000
                Re: longer 'char literals' meaning in c Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-05-05 13:41 -0700
                Re: longer 'char literals' meaning in c antispam@math.uni.wroc.pl - 2020-05-05 22:29 +0000
                Re: longer 'char literals' meaning in c Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-05-05 16:18 -0700
                Re: longer 'char literals' meaning in c antispam@math.uni.wroc.pl - 2020-05-06 02:33 +0000
                Re: longer 'char literals' meaning in c Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-05-05 20:06 -0700
                Re: longer 'char literals' meaning in c antispam@math.uni.wroc.pl - 2020-05-06 18:57 +0000
                Re: longer 'char literals' meaning in c Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-05-06 13:37 -0700
                Re: longer 'char literals' meaning in c Spiros Bousbouras <spibou@gmail.com> - 2020-05-07 13:48 +0000
                Re: longer 'char literals' meaning in c Spiros Bousbouras <spibou@gmail.com> - 2020-05-07 13:58 +0000
                Re: longer 'char literals' meaning in c Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-05-24 17:36 -0700
                Re: longer 'char literals' meaning in c Spiros Bousbouras <spibou@gmail.com> - 2020-05-25 00:56 +0000
                Re: longer 'char literals' meaning in c Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-05-29 22:53 -0700
                Re: longer 'char literals' meaning in c Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-05-07 18:01 -0700
                Re: longer 'char literals' meaning in c Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-05-30 10:53 -0700
                Re: longer 'char literals' meaning in c Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-05-30 15:11 -0700
                Re: longer 'char literals' meaning in c Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-05-24 18:19 -0700
                Re: longer 'char literals' meaning in c James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-05-24 20:06 -0700
                Re: longer 'char literals' meaning in c Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-05-26 07:02 -0700
                Re: longer 'char literals' meaning in c James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-05-05 20:18 -0400
                Re: longer 'char literals' meaning in c antispam@math.uni.wroc.pl - 2020-05-06 20:01 +0000
                Re: longer 'char literals' meaning in c James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-05-06 18:20 -0400
                Re: longer 'char literals' meaning in c Siri Cruise <chine.bleu@yahoo.com> - 2020-04-30 20:13 -0700
      Re: longer 'char literals' meaning in c Bonita Montero <Bonita.Montero@gmail.com> - 2020-04-30 16:58 +0200
        Re: longer 'char literals' meaning in c Bonita Montero <Bonita.Montero@gmail.com> - 2020-04-30 16:59 +0200
        Re: longer 'char literals' meaning in c Bonita Montero <Bonita.Montero@gmail.com> - 2020-04-30 17:14 +0200
        Re: longer 'char literals' meaning in c fir <profesor.fir@gmail.com> - 2020-04-30 08:33 -0700
          Re: longer 'char literals' meaning in c Bonita Montero <Bonita.Montero@gmail.com> - 2020-04-30 18:15 +0200
      Re: longer 'char literals' meaning in c James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-04-30 08:33 -0700
        Re: longer 'char literals' meaning in c David Brown <david.brown@hesbynett.no> - 2020-04-30 17:49 +0200
          Re: longer 'char literals' meaning in c Bart <bc@freeuk.com> - 2020-04-30 17:23 +0100
            Re: longer 'char literals' meaning in c fir <profesor.fir@gmail.com> - 2020-04-30 09:38 -0700
              Re: longer 'char literals' meaning in c Bart <bc@freeuk.com> - 2020-04-30 17:56 +0100
                Re: longer 'char literals' meaning in c fir <profesor.fir@gmail.com> - 2020-04-30 10:04 -0700
                Re: longer 'char literals' meaning in c Bart <bc@freeuk.com> - 2020-04-30 18:28 +0100
                Re: longer 'char literals' meaning in c fir <profesor.fir@gmail.com> - 2020-04-30 10:39 -0700
                Re: longer 'char literals' meaning in c Bart <bc@freeuk.com> - 2020-04-30 19:16 +0100
                Re: longer 'char literals' meaning in c fir <profesor.fir@gmail.com> - 2020-04-30 11:24 -0700
                Re: longer 'char literals' meaning in c Bart <bc@freeuk.com> - 2020-04-30 19:58 +0100
                Re: longer 'char literals' meaning in c fir <profesor.fir@gmail.com> - 2020-04-30 12:08 -0700
                Re: longer 'char literals' meaning in c Bart <bc@freeuk.com> - 2020-04-30 20:16 +0100
                Re: longer 'char literals' meaning in c fir <profesor.fir@gmail.com> - 2020-04-30 12:29 -0700
                Re: longer 'char literals' meaning in c fir <profesor.fir@gmail.com> - 2020-04-30 12:10 -0700
                Re: longer 'char literals' meaning in c fir <profesor.fir@gmail.com> - 2020-04-30 11:17 -0700
                Re: longer 'char literals' meaning in c fir <profesor.fir@gmail.com> - 2020-04-30 10:31 -0700
                Re: longer 'char literals' meaning in c Bart <bc@freeuk.com> - 2020-04-30 19:20 +0100
                Re: longer 'char literals' meaning in c fir <profesor.fir@gmail.com> - 2020-04-30 11:28 -0700
          Re: longer 'char literals' meaning in c Richard Damon <Richard@Damon-Family.org> - 2020-04-30 13:48 -0400
        Re: longer 'char literals' meaning in c fir <profesor.fir@gmail.com> - 2020-04-30 09:01 -0700
      Re: longer 'char literals' meaning in c Vir Campestris <vir.campestris@invalid.invalid> - 2020-04-30 21:32 +0100
        Re: longer 'char literals' meaning in c fir <profesor.fir@gmail.com> - 2020-04-30 13:43 -0700
        Re: longer 'char literals' meaning in c Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2020-04-30 16:54 -0400
          Re: longer 'char literals' meaning in c Vir Campestris <vir.campestris@invalid.invalid> - 2020-05-03 21:23 +0100
            Re: longer 'char literals' meaning in c Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2020-05-03 16:48 -0400
  Re: longer 'char literals' meaning in c fir <profesor.fir@gmail.com> - 2020-04-30 11:38 -0700
    Re: longer 'char literals' meaning in c fir <profesor.fir@gmail.com> - 2020-04-30 11:57 -0700
      Re: longer 'char literals' meaning in c Bonita Montero <Bonita.Montero@gmail.com> - 2020-04-30 21:01 +0200
      Re: longer 'char literals' meaning in c Bart <bc@freeuk.com> - 2020-04-30 20:07 +0100
        Re: longer 'char literals' meaning in c fir <profesor.fir@gmail.com> - 2020-04-30 12:41 -0700
        Re: longer 'char literals' meaning in c Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-05-04 10:30 -0700

csiph-web