Character Constants

A

Akhil

Since a character constant is an int value represented as a character
in single quotes,so it is treated as a 1 byte integer now look at the
following snippet.

#include<stdio.h>
int main(void)
{
char a='Abbc';
printf("%c",a);

return 0;
}

Please explain how the initialization is interpreted,on compilation it
gives a warning "multi-character character constant" but runs without
error also possibly the last character within the single quotes is
being stored in a.
 
D

david ullua

I got an error message when compile it in TC2.0
Error: Character constant too long in function main
 
V

Vladimir S. Oka

Akhil said:
Since a character constant is an int value represented as a character
in single quotes,so it is treated as a 1 byte integer now look at the
following snippet.

#include<stdio.h>
int main(void)
{
char a='Abbc';
printf("%c",a);

return 0;
}

Please explain how the initialization is interpreted,on compilation it
gives a warning "multi-character character constant" but runs without
error also possibly the last character within the single quotes is
being stored in a.

For explanation of initialisation, refer to your compiler documentation,
as Standard says it's implementation defined.

As for the warning, your compiler is just being nice, letting you know
that you may be doing something you don't want. Mine also tells me that
implicit conversion has overflown.


--
BR, Vladimir

I am a friend of the working man, and I would rather be his friend
than be one.
-- Clarence Darrow
 
K

Keith Thompson

Akhil said:
Since a character constant is an int value represented as a character
in single quotes,so it is treated as a 1 byte integer now look at the
following snippet.

A character constant is of type int; for example,
sizeof('x') == sizeof(int). If sizeof(int) happens to be 4,
a character constant specifies a 4-byte value.
#include<stdio.h>
int main(void)
{
char a='Abbc';
printf("%c",a);

return 0;
}

Please explain how the initialization is interpreted,on compilation it
gives a warning "multi-character character constant" but runs without
error also possibly the last character within the single quotes is
being stored in a.

C99 6.4.4.4p10:

The value of an integer character constant containing more than
one character (e.g., 'ab'), or containing a character or escape
sequence that does not map to a single-byte execution character,
is implementation-defined.

So 'Abbc' has some implementation-defined value of type int. Using
that value to initialize a variable of type char requires a conversion
from int to char. If char happens to be unsigned, the conversion is
well defined; if char is signed, the result of the conversion is
implementation-defined (or it can raise an implementation-defined
signal, but that's not likely).

Try changing a from a char to an int and displaying the value in
hexadecimal:

int a = 'Abbc';
printf("a = 0x%x\n", a);

On one implementation, I get

a = 0x41626263

which corresponds to the ASCII values of the individual characters
'A', 'b', 'b', and 'c'. Assigning that value to a char happens to
give you the low-order 8 bits, the value of 'c'. All of this,
including the use of ASCII, is implementation-defined; you shouldn't
depend on any of it if you care at all about portability.

Incidentally, you need a '\n' after you print the value; otherwise
your output may not appear.
 
R

Richard G. Riley

C99 6.4.4.4p10:

The value of an integer character constant containing more than
one character (e.g., 'ab'), or containing a character or escape
sequence that does not map to a single-byte execution character,
is implementation-defined.

keith, can you recommend me an online easy to navigate web source for the
C standards please.

thanks

r
 
B

Ben Pfaff

Richard G. Riley said:
keith, can you recommend me an online easy to navigate web source for the
C standards please.

There is none, because the C standard is non-free. If you want
the standard, you have to buy it from a standards body. It is
$18 as a PDF from ANSI.
 
R

Richard G. Riley

There is none, because the C standard is non-free. If you want
the standard, you have to buy it from a standards body. It is
$18 as a PDF from ANSI.

Great, thanks : I found it. I guess they have to earn a crust too.
 
E

Emmanuel Delahaye

Akhil a écrit :
Since a character constant is an int value represented as a character
in single quotes,so it is treated as a 1 byte integer now look at the
following snippet.

#include<stdio.h>
int main(void)
{
char a='Abbc';

Undefined behavior. You're dead.
 
P

Peter Nilsson

Emmanuel said:
Akhil a écrit :

Undefined behavior. You're dead.

In what way? I can only think of the C99 case where the implementation
defined int constant is outside the range of char and an implementation
defined signal is raised. But such an implementation is likely be
commercially unprofitable.
 
J

Jordan Abel

In what way? I can only think of the C99 case where the implementation
defined int constant is outside the range of char and an implementation
defined signal is raised. But such an implementation is likely be
commercially unprofitable.

Some people here have a bad habit as using "undefined" as a catch-all
for "undefined, implementation-defined, unspecified, or exceeds a
minimum maximum limit"
 
R

Rod Pemberton

<snip>

A 554 page Adobe .pdf titled "International Standard ISO/IEC 9899 Second
Edition 1999-12-01" containing the text ISO/IEC 9899:1999(E) and official
ISO/IEC TM's is easily found with Google or Yahoo. At the very end, it also
lists a price of ICS 35.060 based on 537 pages...

Rod Pemberton

PS. I'm not encouraging theft. Just stating the facts.

Sony Corp. v. Universal City Studios
"...use of a copyrighted work is noncommercial, defeating a fair use defense
requires 'proof either that the particular use is harmful, or that if it
should become widespread, it would adversely affect the potential market for
the copyrighted work.'"

Feist Publications, Inc. v. Rural Tel. Serv. Co
"The primary objective of copyright is not to reward the labor of authors,
but 'to promote the Progress of Science and useful Arts.' . . . To this end,
copyright assures authors the right to their original expression, but
encourages others to build freely upon the ideas and information conveyed by
a work."
 
A

Al Balmer

<snip>

A 554 page Adobe .pdf titled "International Standard ISO/IEC 9899 Second
Edition 1999-12-01" containing the text ISO/IEC 9899:1999(E) and official
ISO/IEC TM's is easily found with Google or Yahoo. At the very end, it also
lists a price of ICS 35.060 based on 537 pages...

Rod Pemberton

PS. I'm not encouraging theft. Just stating the facts.

Sony Corp. v. Universal City Studios
"...use of a copyrighted work is noncommercial, defeating a fair use defense
requires 'proof either that the particular use is harmful, or that if it
should become widespread, it would adversely affect the potential market for
the copyrighted work.'"

I've seen the decision. It really has nothing to do with illegal
copying of the standard (or any other work.) No competent lawyer would
bother trying to contend that copying the entire work is "fair use."

Even the snippet you quote says "... if it should become widespread,
it would adversely affect the potential market for the copyrighted
work." which is certainly the case here. Fair use would be, e.g., the
quoting of a particular passage from the standard, which even if
widespread, would not affect the sales of the work as a whole.
Feist Publications, Inc. v. Rural Tel. Serv. Co
"The primary objective of copyright is not to reward the labor of authors,
but 'to promote the Progress of Science and useful Arts.' . . . To this end,
copyright assures authors the right to their original expression, but
encourages others to build freely upon the ideas and information conveyed by
a work."
Of course. That's why the author of a book about C can, for example,
provide explanations and examples illustrating and clarifying what the
standard says. It doesn't mean that he can insert portions of the
standard verbatim in his book without acquiring permission from the
copyright holder.
 
K

Keith Thompson

Peter Nilsson said:
In what way? I can only think of the C99 case where the implementation
defined int constant is outside the range of char and an implementation
defined signal is raised. But such an implementation is likely be
commercially unprofitable.

Raising an implementation-defined signal isn't undefined behavior
either (no nasal demons).
 
J

Jordan Abel

A 554 page Adobe .pdf titled "International Standard ISO/IEC 9899 Second
Edition 1999-12-01" containing the text ISO/IEC 9899:1999(E) and official
ISO/IEC TM's is easily found with Google or Yahoo. At the very end, it also
lists a price of ICS 35.060 based on 537 pages...

What is an "ICS", anyway? It doesn't seem to be an ISO 4217 code.

[I actually found this same file, while I was looking for N1124. not
having the term "N1124" to search for at the time, I just entered "c99
pdf" and crossed my fingers]
 
M

Michael Wojcik

No competent lawyer would
bother trying to contend that copying the entire work is "fair use."

Nit: Under USC Title 17 (US Federal copyright law), at least prior to
WIPO, archival copying of an entire work is fair use. (Of course, the
original copy may still be a violation.) But I agree that Rod's legal
argument is nonsense.
 

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,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top