Character Constants

Discussion in 'C Programming' started by Akhil, Feb 21, 2006.

  1. Akhil

    Akhil Guest

    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.
    Akhil, Feb 21, 2006
    #1
    1. Advertising

  2. Akhil

    david ullua Guest

    I got an error message when compile it in TC2.0
    Error: Character constant too long in function main
    david ullua, Feb 21, 2006
    #2
    1. Advertising

  3. Akhil

    pete Guest

    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
    pete, Feb 21, 2006
    #3
  4. 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
    Vladimir S. Oka, Feb 21, 2006
    #4
  5. "Akhil" <> 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) <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.
    Keith Thompson, Feb 21, 2006
    #5
  6. >
    > 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
    Richard G. Riley, Feb 21, 2006
    #6
  7. Akhil

    Ben Pfaff Guest

    "Richard G. Riley" <> 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_
    Ben Pfaff, Feb 21, 2006
    #7
  8. On 2006-02-21, Ben Pfaff <> wrote:
    > "Richard G. Riley" <> 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
    Richard G. Riley, Feb 21, 2006
    #8
  9. Akhil

    pete Guest

    Richard G. Riley wrote:
    >
    > On 2006-02-21, Ben Pfaff <> wrote:
    > > "Richard G. Riley" <> 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
    pete, Feb 21, 2006
    #9
  10. 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
    Emmanuel Delahaye, Feb 21, 2006
    #10
  11. Emmanuel Delahaye, Feb 21, 2006
    #11
  12. 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
    Peter Nilsson, Feb 21, 2006
    #12
  13. Akhil

    Jordan Abel Guest

    On 2006-02-21, Peter Nilsson <> 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"
    Jordan Abel, Feb 21, 2006
    #13
  14. "pete" <> wrote in message
    news:...
    > Richard G. Riley wrote:
    > >
    > > On 2006-02-21, Ben Pfaff <> wrote:
    > > > "Richard G. Riley" <> 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."
    Rod Pemberton, Feb 21, 2006
    #14
  15. Akhil

    Al Balmer Guest

    On Tue, 21 Feb 2006 16:41:42 -0500, "Rod Pemberton"
    <> wrote:

    >
    >"pete" <> wrote in message
    >news:...
    >> Richard G. Riley wrote:
    >> >
    >> > On 2006-02-21, Ben Pfaff <> wrote:
    >> > > "Richard G. Riley" <> 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.'"


    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
    Al Balmer, Feb 21, 2006
    #15
  16. Akhil

    Micah Cowan Guest

    Emmanuel Delahaye <> 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
    Micah Cowan, Feb 21, 2006
    #16
  17. Emmanuel Delahaye <> 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) <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.
    Keith Thompson, Feb 21, 2006
    #17
  18. "Peter Nilsson" <> 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) <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.
    Keith Thompson, Feb 21, 2006
    #18
  19. Akhil

    Jordan Abel Guest

    On 2006-02-21, Rod Pemberton <> 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]
    Jordan Abel, Feb 21, 2006
    #19
  20. In article <>, Al Balmer <> 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

    Cooperation is just like two pagodas, one hardware and one software.
    -- Wen Jiabao
    Michael Wojcik, Feb 23, 2006
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Velvet
    Replies:
    9
    Views:
    14,771
    Joerg Jooss
    Jan 19, 2006
  2. Kavya
    Replies:
    2
    Views:
    287
  3. Mirco Wahab

    Multi-character constants

    Mirco Wahab, Jul 9, 2008, in forum: C++
    Replies:
    2
    Views:
    1,959
    James Kanze
    Jul 10, 2008
  4. Luca Forlizzi

    Questions on character constants

    Luca Forlizzi, Dec 12, 2010, in forum: C Programming
    Replies:
    2
    Views:
    326
    luser- -droog
    Dec 13, 2010
  5. Edward Rutherford

    Questions on ISO C character constants

    Edward Rutherford, Nov 8, 2011, in forum: C Programming
    Replies:
    1
    Views:
    333
    Kaz Kylheku
    Nov 8, 2011
Loading...

Share This Page