By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,985 Members | 1,001 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,985 IT Pros & Developers. It's quick & easy.

Character Constants

P: n/a
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.

Feb 21 '06 #1
Share this Question
Share on Google+
23 Replies


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

Feb 21 '06 #2

P: n/a
Akhil wrote:
char a='Abbc'; Please explain how the initialization is interpreted,


The initialization is interpreted
any way that the compiler wants to.
The C standard doesn't define it.

--
pete
Feb 21 '06 #3

P: n/a
Akhil wrote:
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

Feb 21 '06 #4

P: n/a
"Akhil" <ak*************@gmail.com> writes:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 21 '06 #5

P: n/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.


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

thanks

r
Feb 21 '06 #6

P: n/a
"Richard G. Riley" <rg***********@gmail.com> writes:
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.
--
"Programmers have the right to be ignorant of many details of your code
and still make reasonable changes."
--Kernighan and Plauger, _Software Tools_
Feb 21 '06 #7

P: n/a
On 2006-02-21, Ben Pfaff <bl*@cs.stanford.edu> wrote:
"Richard G. Riley" <rg***********@gmail.com> writes:
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.


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

--
Remove evomer to reply
Feb 21 '06 #8

P: n/a
Richard G. Riley wrote:

On 2006-02-21, Ben Pfaff <bl*@cs.stanford.edu> wrote:
"Richard G. Riley" <rg***********@gmail.com> writes:
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.


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


Here's some drafts.
http://rm-f.net/~orange/devel/specifications/

The C89 standard is a little more expensive.

--
pete
Feb 21 '06 #9

P: n/a
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.

--
C is a sharp tool
Feb 21 '06 #10

P: n/a
Richard G. Riley a écrit :
keith, can you recommend me an online easy to navigate web source for the
C standards please.


Here is a free downloadable PDF. It's very close to the standard.

http://www.open-std.org/jtc1/sc22/wg...docs/n1124.pdf

You can get the genuine standard from http://webstore.ansi.org for 20 USD.

--
C is a sharp tool
Feb 21 '06 #11

P: n/a
Emmanuel Delahaye wrote:
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.


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.

--
Peter

Feb 21 '06 #12

P: n/a
On 2006-02-21, Peter Nilsson <ai***@acay.com.au> wrote:
Emmanuel Delahaye wrote:
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.


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"
Feb 21 '06 #13

P: n/a

"pete" <pf*****@mindspring.com> wrote in message
news:43***********@mindspring.com...
Richard G. Riley wrote:

On 2006-02-21, Ben Pfaff <bl*@cs.stanford.edu> wrote:
"Richard G. Riley" <rg***********@gmail.com> writes:

> 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.

<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."

Feb 21 '06 #14

P: n/a
On Tue, 21 Feb 2006 16:41:42 -0500, "Rod Pemberton"
<do*********@sorry.bitbucket.cmm> wrote:

"pete" <pf*****@mindspring.com> wrote in message
news:43***********@mindspring.com...
Richard G. Riley wrote:
>
> On 2006-02-21, Ben Pfaff <bl*@cs.stanford.edu> wrote:
> > "Richard G. Riley" <rg***********@gmail.com> writes:
> >
> >> keith, can you recommend me an online easy to navigate web source forthe > >> 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.
>
<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.

--
Al Balmer
Sun City, AZ
Feb 21 '06 #15

P: n/a
Emmanuel Delahaye <em***@YOURBRAnoos.fr> writes:
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.


No, just implementation-defined. You're non-portable.

:-)

-Micah
Feb 21 '06 #16

P: n/a
Emmanuel Delahaye <em***@YOURBRAnoos.fr> writes:
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.


The behavior is implementation-defined, not undefined.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 21 '06 #17

P: n/a
"Peter Nilsson" <ai***@acay.com.au> writes:
Emmanuel Delahaye wrote:
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.


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).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 21 '06 #18

P: n/a
On 2006-02-21, Rod Pemberton <do*********@sorry.bitbucket.cmm> wrote:
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]
Feb 21 '06 #19

P: n/a

In article <32********************************@4ax.com>, Al Balmer <al******@att.net> writes:

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.

--
Michael Wojcik mi************@microfocus.com

Cooperation is just like two pagodas, one hardware and one software.
-- Wen Jiabao
Feb 23 '06 #20

P: n/a
On 2006-02-23, Michael Wojcik <mw*****@newsguy.com> wrote:

In article <32********************************@4ax.com>, Al Balmer <al******@att.net> writes:

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.


You do have to consider the landscape though - It's arguable that there
is an assumption that anything posted on the internet that's not behind
a login / credit card / etc is free to load in your web browser and
view, just like there is an assumption in the real that you can enter
any land that does not have a fence or any "no trespassing" signs
posted.

For another example: you included an excerpt from Al Balmer's
copyrighted work and I in turn included that excerpt and the entire text
of your own in this message. Under the strict interpretation of
copyright law that has been put forward in this thread, this would have
been illegal. I guess all the google groups users are really just trying
to avoid liability for copyright violation.
Feb 23 '06 #21

P: n/a
On 23 Feb 2006 17:11:54 GMT, Jordan Abel <ra*******@gmail.com> wrote:
On 2006-02-23, Michael Wojcik <mw*****@newsguy.com> wrote:

In article <32********************************@4ax.com>, Al Balmer <al******@att.net> writes:

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.


You do have to consider the landscape though - It's arguable that there
is an assumption that anything posted on the internet that's not behind
a login / credit card / etc is free to load in your web browser and
view, just like there is an assumption in the real that you can enter
any land that does not have a fence or any "no trespassing" signs
posted.

For another example: you included an excerpt from Al Balmer's
copyrighted work and I in turn included that excerpt and the entire text
of your own in this message. Under the strict interpretation of
copyright law that has been put forward in this thread, this would have
been illegal.


Probably not, for two reasons - including excerpts or even entire
short texts as part of a discussion or review, or for illustration,
falls under fair use, and the terms of service for your Internet
provider likely include a weakening of your copyrights, so as to allow
them to post your messages in a public forum, and to archive them.

OTOH, if someone collects a bunch of articles written here, and
republishes them as a book without getting permission from all the
authors, that's illegal. Even if you publish it on the Internet :-)

Having said that, if you distributed it for free, you could be made to
stop, but there would be little monetary penalty.
I guess all the google groups users are really just trying
to avoid liability for copyright violation.


--
Al Balmer
Sun City, AZ
Feb 23 '06 #22

P: n/a
On 2006-02-23, Al Balmer <al******@att.net> wrote:
OTOH, if someone collects a bunch of articles written here, and
republishes them as a book without getting permission from all the
authors, that's illegal. Even if you publish it on the Internet :-)


So where does google stand on that issue? (Sorry, I had to ask)
Though... I bet if it was tested, the X-no-archive header would provide
a basis for a ruling similar to the recent robots.txt decision. Local
custom [the "locality" here being the internet] clearly has some
influence on the interpretation of the law.
Feb 23 '06 #23

P: n/a
On Thu, 23 Feb 2006 10:58:45 -0700, Al Balmer <al******@att.net>
wrote:
On 23 Feb 2006 17:11:54 GMT, Jordan Abel <ra*******@gmail.com> wrote:
On 2006-02-23, Michael Wojcik <mw*****@newsguy.com> wrote:

In article <32********************************@4ax.com>, Al Balmer <al******@att.net> writes:

BTW, this is getting really off-topic for c.l.c., so I'll try to
restrain myself in future.

--
Al Balmer
Sun City, AZ
Feb 23 '06 #24

This discussion thread is closed

Replies have been disabled for this discussion.