initialize for a 2-D dynamic array.

E

Ersek, Laszlo

An "integer-constant" is a decimal, octal, or hexadecimal constant,
optionally followed by a suffix such as "UL"; it's a single token.
Even "-1" isn't an integer constant; it's a "-" token followed by
an integer constant.

Hm... Supposing INT_MIN is -32768 and INT_MAX is 32767, how is the value
and type of the integer constant expression described by the token
sequence "-", "32768" determined? If "-32768" is not a single integer
constant, then it is the expression

(-(32768))

However, 32768 has type "long" (as per C99 6.4.4.1p5 and the INT_MAX
above), so -32768 (INT_MIN) is a long. Umm, what? :) I guess I'm
misunderstanding something...

Thanks,
lacos
 
E

Ersek, Laszlo

Hm... Supposing INT_MIN is -32768 and INT_MAX is 32767, how is the value
and type of the integer constant expression described by the token
sequence "-", "32768" determined? If "-32768" is not a single integer
constant, then it is the expression

(-(32768))

However, 32768 has type "long" (as per C99 6.4.4.1p5 and the INT_MAX
above), so -32768 (INT_MIN) is a long. Umm, what? :) I guess I'm
misunderstanding something...

And just to complete the circle, C99 5.2.4.2.1p1 says INT_MIN is an
expression of type int, because the "INT" in INT_MIN denotes "int", and
"int" is not affected by the integer promotions.

Or *must* INT_MIN be defined as ((int)-32768) on this hypothetical
platform then, and is -32768 really a long?

Thanks,
lacos
 
K

Keith Thompson

Keith Thompson said:
C99 6.5.3.4p2 has the same problem; it says:

If the type of the operand is a variable length array type, the
operand is evaluated; otherwise, the operand is not evaluated
and the result is an integer constant.

But the phrase "integer constant" cannot refer to the
syntactic category "integer-constant" defined in C99 6.4.4.1.
An "integer-constant" is a decimal, octal, or hexadecimal constant,
optionally followed by a suffix such as "UL"; it's a single token.
Even "-1" isn't an integer constant; it's a "-" token followed by
an integer constant. Clearly anything that includes the "sizeof"
keyword cannot be an "integer-constant".

What it probably should say is that "sizeof ..." is an "integer
constant expression" as defined in C99 6.6p6. The point is that the
result is constant (evaluated at compile time). The tricky part
is saying that without misusing the word "constant" as defined in
C99 6.4.4.

Thinking about this a bit more, I don't think that saying it's an
integer constant expression quite captures the same intent (though
it's logically equivalent). What it's trying to say, of course, is
that the result is evaluated at compile time. The term "constant",
which is used now, has a different meaning, referring to a kind
of lexical token. There needs to be a term for "what you get when
you evaluate a constant expression". "Constant value" would be good.
 
K

Keith Thompson

I think you're running into a contradiction because you're thinking
of ``-32768'' both as a mathematical value (which is the value of
INT_MIN) and as a C expression (which cannot be the expansion of
the macro INT_MIN).
And just to complete the circle, C99 5.2.4.2.1p1 says INT_MIN is an
expression of type int, because the "INT" in INT_MIN denotes "int", and
"int" is not affected by the integer promotions.

Or *must* INT_MIN be defined as ((int)-32768) on this hypothetical
platform then, and is -32768 really a long?

Yes, 32768 on such a system is of type long, so -32768 is also of type
long; therefore, such a system's <limits.h> cannot have
#define INT_MIN (-32768)
If it did, then ``sizeof INT_MIN'' would yield the size of long, not
the size of int.

#define INT_MIN ((int)-32768)
is one possible solution, but not the only one.

On my system (with 32-bit int), <limits.h> has:

# define INT_MIN (-INT_MAX - 1)
# define INT_MAX 2147483647
 
E

Ersek, Laszlo

(e-mail address removed) (Ersek, Laszlo) writes:

Yes, 32768 on such a system is of type long, so -32768 is also of type
long; therefore, such a system's <limits.h> cannot have
#define INT_MIN (-32768)
If it did, then ``sizeof INT_MIN'' would yield the size of long, not
the size of int.

#define INT_MIN ((int)-32768)
is one possible solution, but not the only one.

On my system (with 32-bit int), <limits.h> has:

# define INT_MIN (-INT_MAX - 1)
# define INT_MAX 2147483647


Thank you.
lacos
 
T

Tim Rentsch

C90 6.3.3.4 The sizeof operator

----v----
[...]

The result is an integer constant.

[...]
----^----

In my interpretation all these permit size_t to be "short unsigned".

Oh no! After looking up C90 6.1.3.2 "Integer constants", I realize
nothing with a lower conversion rank than that of "int" can be size_t.
And thus size_t is not affected by the integer promotions.

The "integer constant" phrase here is a red herring. In this
text it means only that the result has an integer value and is
fixed at compile time. In particular it does not refer to the
'integer-constant' syntactic category.
 
T

Tim Rentsch

Keith Thompson said:
C90 6.3.3.4 The sizeof operator

----v----
[...]

The result is an integer constant.

[...]
----^----

In my interpretation all these permit size_t to be "short unsigned".

Oh no! After looking up C90 6.1.3.2 "Integer constants", I realize
nothing with a lower conversion rank than that of "int" can be size_t.
And thus size_t is not affected by the integer promotions.

Sorry :)

I think you've been led astray by some incorrect wording.

C99 6.5.3.4p2 has the same problem; it says:

If the type of the operand is a variable length array type, the
operand is evaluated; otherwise, the operand is not evaluated
and the result is an integer constant.

But the phrase "integer constant" cannot refer to the
syntactic category "integer-constant" defined in C99 6.4.4.1.
An "integer-constant" is a decimal, octal, or hexadecimal constant,
optionally followed by a suffix such as "UL"; it's a single token.
Even "-1" isn't an integer constant; it's a "-" token followed by
an integer constant. Clearly anything that includes the "sizeof"
keyword cannot be an "integer-constant".

What it probably should say is that "sizeof ..." is an "integer
constant expression" as defined in C99 6.6p6. The point is that the
result is constant (evaluated at compile time). The tricky part
is saying that without misusing the word "constant" as defined in
C99 6.4.4.

I expect these comments are based on a presumption that
the appearance of "integer constant" in a passage in the
Standard must necessarily refer to the 'ingeger-constant'
syntactic category. I believe that presumption doesn't
hold in general (and more specifically doesn't hold in
this case, but my main point is about the general case).
Note the wording used in 6.1p1:

When syntactic categories are referred to in the main
text, [they are not italicized and words are separated by
spaces instead of hyphens.]

It says 'when'. It doesn't say that any such phrase refers
to a syntactic category. And they don't, at least not
always. It's only when a word or phrase is used _to
indicate a syntactic category_ that the word or phrase
is as defined in the syntax. Presumably that distinction
is supplied by other context around each use.

Returning to the specific passage, it seems clear that
"integer constant" in this case is meant to be taken
just as an ordinary English phrase, and does not refer
to the syntactic category 'integer-constant'.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top