Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Character Constants

Reply
Thread Tools

Character Constants

 
 
Akhil
Guest
Posts: n/a
 
      02-21-2006
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.

 
Reply With Quote
 
 
 
 
david ullua
Guest
Posts: n/a
 
      02-21-2006
I got an error message when compile it in TC2.0
Error: Character constant too long in function main

 
Reply With Quote
 
 
 
 
pete
Guest
Posts: n/a
 
      02-21-2006
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
 
Reply With Quote
 
Vladimir S. Oka
Guest
Posts: n/a
 
      02-21-2006
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

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-21-2006
"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) kst- <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.
 
Reply With Quote
 
Richard G. Riley
Guest
Posts: n/a
 
      02-21-2006
>
> 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
 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      02-21-2006
"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_
 
Reply With Quote
 
Richard G. Riley
Guest
Posts: n/a
 
      02-21-2006
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
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      02-21-2006
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
 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      02-21-2006
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Questions on ISO C character constants Edward Rutherford C Programming 1 11-08-2011 09:09 PM
Questions on character constants Luca Forlizzi C Programming 2 12-13-2010 06:18 AM
Multi-character constants Mirco Wahab C++ 2 07-10-2008 07:44 AM
sizeof character constants in C++ Kavya C++ 2 11-08-2006 12:18 PM
getting the character code of a character in a string Velvet ASP .Net 9 01-19-2006 09:27 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57