char type size

K

Keith Thompson

Francis Moreau said:
Ok.

All of this means that the (source|execution) characters set is
undefined or rather a local-specific defined.

The *basic* character sets are defined by the standard. The
*extended* character sets are implementation-defined and
locale-specific.
So using '@' character, for example, is valid as long as the local
conventions define it but can be no more valid if the source is used
where the local conventions don't define '@'. So it's basically not
portable but the behaviour is undefined.

As I said, if you transferred the source file to a system that doesn't
have an encoding for the '@' character (apparently some versions of
EBCDIC are like this), then you simply can't have a source file
containing an '@' character. The behavior isn't merely undefined;
there is no behavior, because there can be no such source file.

But yes, if the OS allows '@' characters but the C implementation
doesn't treat '@' as part of its (source|execution) character set,
then the behavior is undefined.
But I still don't see the point of the part of 5.2.1.{3}, which says:

... (except in an identifier, a character constant,
a string literal, a header name, a comment, or a
preprocessing token that is never converted to a
token) ...

I don't see why all of them are exceptions.

I think the idea is that an implementation isn't allowed to quietly
ignore other characters in those contexts, which would be allowed if
the behavior were undefined. For example, an implementation may
permit additional implementation-defined characters in identifiers
('$' is not uncommon); if it does so, it must treat the identifiers xy
and x$y as distinct. Similarly, if you have an '@' character in a
string literal, the compiler must either accept it (after the
source-to-execution conversion in translation phase 5) or issue a
diagnostic.
 
F

Francis Moreau

The *basic* character sets are defined by the standard.  The
*extended* character sets are implementation-defined and
locale-specific.

So the (source|execution) characters set is local-specific since
char set = basic char set + extended char set.

[...]
I think the idea is that an implementation isn't allowed to quietly
ignore other characters in those contexts, which would be allowed if
the behavior were undefined.  For example, an implementation may
permit additional implementation-defined characters in identifiers
('$' is not uncommon); if it does so, it must treat the identifiers xy
and x$y as distinct.  Similarly, if you have an '@' character in a
string literal, the compiler must either accept it (after the
source-to-execution conversion in translation phase 5) or issue a
diagnostic.

Ok that makes sense now.

Thanks !
 
F

Francis Moreau

[...]
I think the idea is that an implementation isn't allowed to quietly
ignore other characters in those contexts, which would be allowed if
the behavior were undefined.  For example, an implementation may
permit additional implementation-defined characters in identifiers
('$' is not uncommon); if it does so, it must treat the identifiers xy
and x$y as distinct.  Similarly, if you have an '@' character in a
string literal, the compiler must either accept it (after the
source-to-execution conversion in translation phase 5) or issue a
diagnostic.

Hmm, I just tried to use '$' in both an identifier declaration and a
character constant and gcc didn't complain.

But I also tried with '@' character and now gcc accept if only for
character constant. For identifier declaration it said:

main.c:3: error: stray ‘@’ in program

confusing...
 
B

Ben Bacarisse

Francis Moreau said:
[...]
I think the idea is that an implementation isn't allowed to quietly
ignore other characters in those contexts, which would be allowed if
the behavior were undefined.  For example, an implementation may
permit additional implementation-defined characters in identifiers
('$' is not uncommon); if it does so, it must treat the identifiers xy
and x$y as distinct.  Similarly, if you have an '@' character in a
string literal, the compiler must either accept it (after the
source-to-execution conversion in translation phase 5) or issue a
diagnostic.

Hmm, I just tried to use '$' in both an identifier declaration and a
character constant and gcc didn't complain.

Just a data point: gcc 4.3.2 does complain:

x.c:3:10: warning: '$' in identifier or number

when invoked with -ansi -pedantic (or the equivalent -std=c89
-pedantic) but it does not when invoked with -std=c99 -pedantic. This
looks wrong to me.
 
V

vippstar

[...]
I think the idea is that an implementation isn't allowed to quietly
ignore other characters in those contexts, which would be allowed if
the behavior were undefined.  For example, an implementation may
permit additional implementation-defined characters in identifiers
('$' is not uncommon); if it does so, it must treat the identifiers xy
and x$y as distinct.  Similarly, if you have an '@' character in a
string literal, the compiler must either accept it (after the
source-to-execution conversion in translation phase 5) or issue a
diagnostic.

Hmm, I just tried to use '$' in both an identifier declaration and a
character constant and gcc didn't complain.

It doesn't have to complain for a character constant but it's
undefined behavior. If you use it as an identifier the compiler must
provide at least a warning.
If gcc does this (Bacarisse has verified this in another post and then
I tested it too), then it's not compliant, because it does not provide
a diagnostic for the constraint violation. You can complain if you
care enough - I don't. But it's not really wrong, because simply gcc
never claimed C99 compliance, and that is not what the -std=c99 option
currently does.
But I also tried with '@' character and now gcc accept if only for
character constant. For identifier declaration it said:

    main.c:3: error: stray ‘@’ in program

confusing...

gcc is free to accept @ as a character constant, but it's undefined
behavior too (like $). As for using it in identifiers - it simply is
not an extension supported by gcc. ($ in identifiers is an extension)
Because it is a constraint violation, the diagnostic is provided as
required, at least when invoked in compliance mode.
 
K

Keith Thompson

[...]
I think the idea is that an implementation isn't allowed to quietly
ignore other characters in those contexts, which would be allowed if
the behavior were undefined.  For example, an implementation may
permit additional implementation-defined characters in identifiers
('$' is not uncommon); if it does so, it must treat the identifiers xy
and x$y as distinct.  Similarly, if you have an '@' character in a
string literal, the compiler must either accept it (after the
source-to-execution conversion in translation phase 5) or issue a
diagnostic.

Hmm, I just tried to use '$' in both an identifier declaration and a
character constant and gcc didn't complain.

It doesn't have to complain for a character constant but it's
undefined behavior. If you use it as an identifier the compiler must
provide at least a warning.

Using '$' in a character constant is perfectly well defined if '$' is
part of the implementation's extended character set, which in the case
of gcc it surely is.

No diagnostic is required for an identifier with a '$' in it if the
implementation supports it. C99 6.4.2.1 defines the syntax of an
identifier; it includes "other implementation-defined character" as
one of the possibilities for "identifier-nondigit". Paragraph 3 says:

An implementation may allow multibyte characters that are not part
of the basic source character set to appear in identifiers; which
characters and their correspondence to universal character names
is implementation-defined.

This is a change from C90. In C90, the only characters allowed in
identifiers are uppercase and lowercase letters, digits, and
underscore. A C90 implementation may permit other characters as an
extension, but it must still issue the required diagnostic for the
syntax error.

Allowing '$' in identifiers is an extension (in the sense of C90 4p2)
for C90, but is merely an implementation-defined feature in C99. So
producing a diagnostic in C90 mode but not in C99 mode is actually
correct. (Of course, producing a diagnostic in C99 mode would also be
correct, but it's not required.)
If gcc does this (Bacarisse has verified this in another post and then
I tested it too), then it's not compliant, because it does not provide
a diagnostic for the constraint violation. You can complain if you
care enough - I don't. But it's not really wrong, because simply gcc
never claimed C99 compliance, and that is not what the -std=c99 option
currently does.

<OT>
If this were a failure to diagnose a C99 constraint violation,
then it would certainly be worth complaining about. The goal of
"gcc -std=c99" is to support the C99 standard; the gcc folks know
they're not there yet, but they do want to document any shortcomings
at <http://gcc.gnu.org/c99status.html>.
gcc is free to accept @ as a character constant, but it's undefined
behavior too (like $). As for using it in identifiers - it simply is
not an extension supported by gcc. ($ in identifiers is an extension)
Because it is a constraint violation, the diagnostic is provided as
required, at least when invoked in compliance mode.

As with '$', '@' in a character constant is ok (though strictly
non-portable), but you can't use '@' in an identifier. Attempting to
do so yields a syntax error. For example this:
int foo@bar;
becomes this sequence of preprocessing tokens:
int foo @ bar ;
and since the @ preprocessing token cannot be converted to a token,
this violates the constraint in 6.4p2. If gcc permitted '@ in
identifiers (either as an extension in C90 or as an "other
implementation-defined character" in C99), the sequence of
preprocessing tokens would be
int foo@bar ;
 
F

Francis Moreau

[...]
As with '$', '@' in a character constant is ok (though strictly
non-portable), but you can't use '@' in an identifier. Attempting to
do so yields a syntax error. For example this:
int foo@bar;
becomes this sequence of preprocessing tokens:
int foo @ bar ;
and since the @ preprocessing token cannot be converted to a token,
this violates the constraint in 6.4p2. If gcc permitted '@ in
identifiers (either as an extension in C90 or as an "other
implementation-defined character" in C99), the sequence of
preprocessing tokens would be
int foo@bar ;

Hmm, if '@' is valid for character constant but not for identifier,
doesn't this means that '@' character is part of the local-specific
members but not part of implementation-defined characters ?

thanks
 
K

Keith Thompson

Francis Moreau said:
[...]
As with '$', '@' in a character constant is ok (though strictly
non-portable), but you can't use '@' in an identifier. Attempting to
do so yields a syntax error. For example this:
int foo@bar;
becomes this sequence of preprocessing tokens:
int foo @ bar ;
and since the @ preprocessing token cannot be converted to a token,
this violates the constraint in 6.4p2. If gcc permitted '@ in
identifiers (either as an extension in C90 or as an "other
implementation-defined character" in C99), the sequence of
preprocessing tokens would be
int foo@bar ;

Hmm, if '@' is valid for character constant but not for identifier,
doesn't this means that '@' character is part of the local-specific
members but not part of implementation-defined characters ?

Sure, it's part of the locale-specific set of "extended characters"
described in C99 5.2.1p1, but not part of the set of "other
implementation-defined characters" referred to in the syntax of an
identifier in C99 6.4.2.1p1. But the latter, though it's a set of
characters, isn't a "character set" as the term is used in 5.2.1;
the only relationship is that characters that can be part of an
identifier have to be part of the execution character set.
 
F

Francis Moreau

[...]
But the latter, though it's a set of characters, isn't a "character
set" as the term is used in 5.2.1;

hmm not sure I understand this...
the only relationship is that characters that can be part of an
identifier have to be part of the execution character set.

What make you assert this ?

thanks
 
K

Keith Thompson

Francis Moreau said:
hmm not sure I understand this...

And with the amount of context you've left in your followup ("the
latter" what?), it's going to be difficult for anyone else to
understand it.
What make you assert this ?

What makes you doubt it?

Your remark from upthread was:

| Hmm, if '@' is valid for character constant but not for identifier,
| doesn't this means that '@' character is part of the local-specific
| members but not part of implementation-defined characters ?

5.2.1 discusses various "character sets", which are sets of characters
supported by the implementation, either in source files or in run-time
data. For example, the "source character set" is the set of
characters from which all constructs in source code are built.

On the other hand, the phrase "other implementation-defined
characters" in 6.4.2.1 is merely part of the syntax of an identifier,
which is just one of a large number of different kinds of tokens.
 
G

Guest

And what's a typical size of char in these cases ?

apparently (I've never programmed a DSP so I rely on other people's
statements about DSPs) 32 bits is common.

Apparently some fairly common C idioms don't work on these platforms
 
G

Guest

Yes, C requires and guarantees, not claims, that sizeof(char) is
exactly one byte.  However C does not, and never has, stated that a
byte is an "octet", the precise term for a data type consisting of
exactly 8 bits.

It becomes more confusing over time as imprecise usage has come to
cause most people, at least in English-speaking countries, to think
that a byte is always 8 bits, but that is not the C definition of
byte.

this why people in the comms world speak of octets. It has an
unambiguous definition.

<snip>
 
F

Francis Moreau

Francis Moreau said:
[...]
But the latter, though it's a set of characters, isn't a "character
set" as the term is used in 5.2.1;
hmm not sure I understand this...

And with the amount of context you've left in your followup ("the
latter" what?), it's going to be difficult for anyone else to
understand it.
What make you assert this ?

What makes you doubt it?

Your remark from upthread was:

| Hmm, if '@' is valid for character constant but not for identifier,
| doesn't this means that '@' character is part of the local-specific
| members but not part of implementation-defined characters ?

5.2.1 discusses various "character sets", which are sets of characters
supported by the implementation, either in source files or in run-time
data.  For example, the "source character set" is the set of
characters from which all constructs in source code are built.

On the other hand, the phrase "other implementation-defined
characters" in 6.4.2.1 is merely part of the syntax of an identifier,
which is just one of a large number of different kinds of tokens.

OK, I guess I need to dig the spec before I can comment what you said.

Thanks for your answers anyway
 

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

No members online now.

Forum statistics

Threads
474,416
Messages
2,571,560
Members
48,797
Latest member
shadowoftheunknown

Latest Threads

Top