Topic: char c = 'ab' legal??


Author: Gennaro Prota <gennaro_prota@yahoo.com>
Date: Thu, 20 Jun 2002 17:30:06 GMT
Raw View
On Mon, 17 Jun 2002 18:02:52 GMT, whatiscpp@yahoo.com (John the
newbie) wrote:

> Moreover, this means that '\n'
>is a multicharacter literal, but it's treated in a special way and has
>type char?

No, because by definition a multicharacter literal is an ordinary
character literal that contains more than one c-char, and an escape
sequence is a single c-char (see the grammar in 2.13.2). So:

int i1   = 'aa';
char nl  = '\n';
int i2   = '\n\n';
int i3   = '\na';
int i4   = '\129';
int i5   = 'jpeg';

are all multi-character literals except the second one; i1, i2, i3 and
i4 contain 2 c-chars, while i5 contains 4 c-chars.


Genny.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: kanze@gabi-soft.de (James Kanze)
Date: Mon, 17 Jun 2002 18:03:08 GMT
Raw View
Francis Glassborow <francis.glassborow@ntlworld.com> wrote in message
news:<8h4qcUBNhFD9EwgN@robinton.demon.co.uk>...
> In article <102a8848.0206151734.4c559f6f@posting.google.com>, John the
> newbie <whatiscpp@yahoo.com> writes

> >yesterday, in the intent of writing '\t' I mistakenly wrote '\\t'
> >and my compiler accepted it without complaints. Hence I tried
> >writing

> >char c = 'ab';

> >and it still accepted it! Is it legal to put 2 characters in one
> >literal? What is the meaning?

> IIRC, yes and I think the result is implementation defined.

It is definitly the case in C, where the type of 'ab' is int.  I think
it is implementation defined, but the intent (or at least what
historically happened) is that the int contained both characters.  In
an implementation defined order -- the equivalent of 256 * 'a' + 'b',
or 256 * 'b' + 'a' on an eight bit machine.

Since the type of 'x' in C++ is char, and not int, I would expect them
to be illegal in C++.  But I still don't have a copy of my C++
standard on line here to verify.  And both g++ (2.95.2) and Sun CC
(5.1) accept them, treating them as type int (and not char)!  (G++
does generate a warning.)

--
James Kanze                                mailto:kanze@gabi-soft.de
Conseils en informatique orient   e objet/
                    Beratung in objektorientierter Datenverarbeitung
Ziegelh   ttenweg 17a, 60598 Frankfurt, Germany Tel. +49(0)69 63198627

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Jack Klein <jackklein@spamcop.net>
Date: Tue, 18 Jun 2002 00:04:06 CST
Raw View
On Mon, 17 Jun 2002 18:02:52 GMT, whatiscpp@yahoo.com (John the
newbie) wrote in comp.std.c++:

> "Jim Fischer" <jfischer110@attbi.com> wrote
> > "John the newbie" <whatiscpp@yahoo.com> wrote
> > > Hi everybody,
> > >
> > > yesterday, in the intent of writing '\t' I mistakenly wrote '\\t' and
> > > my compiler accepted it without complaints. Hence I tried writing
> > >
> > > char c = 'ab';
> > >
> > > and it still accepted it! Is it legal to put 2 characters in one
> > > literal?
> >
> > Yes.
> >
> >
> > > What is the meaning?
> >
> > It's a multi-character literal whose type is 'int' and whose value is
> > implementation-defined (ref: 2.13.2/1).
>
> Thanks. What's the rationale for them? Moreover, this means that '\n'
> is a multicharacter literal, but it's treated in a special way and has
> type char?

What do you mean, what's the rationale?  This is the way it is
defined.  Most implementations of C++ (and C, from which C++ inherited
this behavior) have ints that are larger than characters.  Where a
character may only be able to hold a single character, in most cases
an int has space to store more than one.

As for '\n', it is _not_ a multicharacter literal, nor are any of the
other standard escape sequences that begin with a back slash.  They
are escape sequences, and are specifically defined patterns that the
compiler recognizes as representing certain non-printable characters
in string and character literals.  They are replaced with single
characters during the compilation process.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Ray Lischner <dontspamme@spam.you>
Date: Tue, 18 Jun 2002 17:30:57 GMT
Raw View
On Monday 17 June 2002 11:03 am, James Kanze wrote:

> Since the type of 'x' in C++ is char, and not int, I would expect them
> to be illegal in C++.  But I still don't have a copy of my C++
> standard on line here to verify.

A single character literal has type char. "A multicharacter literal has type
int and implementation-defined value." (2.13.2 [lex.ccon])
--
Ray Lischner, author of C++ in a Nutshell (forthcoming)
http://www.tempest-sw.com/cpp/

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: "Al Grant" <tnarga@arm.REVERSE-NAME.com>
Date: 19 Jun 2002 15:10:11 GMT
Raw View
"Jack Klein" <jackklein@spamcop.net> wrote in message
news:sketgugl70im5govf1dhkr7jkolipoq2mq@4ax.com...
> What do you mean, what's the rationale?  This is the way it is
> defined.  Most implementations of C++ (and C, from which C++ inherited
> this behavior) have ints that are larger than characters.  Where a
> character may only be able to hold a single character, in most cases
> an int has space to store more than one.

Given that single-character literals in C++ now have type
'char', what is the rationale for multi-character literals
in C++ having type 'int', rather than the smallest type that
can contain them?


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Jack Klein <jackklein@spamcop.net>
Date: 20 Jun 2002 04:25:04 GMT
Raw View
On 19 Jun 2002 15:10:11 GMT, "Al Grant" <tnarga@arm.REVERSE-NAME.com>
wrote in comp.std.c++:

> "Jack Klein" <jackklein@spamcop.net> wrote in message
> news:sketgugl70im5govf1dhkr7jkolipoq2mq@4ax.com...
> > What do you mean, what's the rationale?  This is the way it is
> > defined.  Most implementations of C++ (and C, from which C++ inherited
> > this behavior) have ints that are larger than characters.  Where a
> > character may only be able to hold a single character, in most cases
> > an int has space to store more than one.
>
> Given that single-character literals in C++ now have type
> 'char', what is the rationale for multi-character literals
> in C++ having type 'int', rather than the smallest type that
> can contain them?

1. C compatibility.

2. General way of C and C++ operation, in that any integer constant
expression, which this is, has at least type int.

The C++ change of the type of a character literal constant from int to
char is required for the proper scheme of overloaded functions and
operators, particularly << to text streams.

What is the rationale for the for the following integer constant
expressions having type int, rather than type short or even type char?

    3

    0x0d

    033

What possible gain would there be in changing the type of any of the
above to char or short?

Integer constant expressions in general, barring the case of the
single char literal which is different for a specific C++ reason, have
the first type in the following list that can contain their value:

   signed int
   unsigned int
   signed long
   unsigned long
   signed long long (C only at the moment)
   unsigned long long (C only at the moment)

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: whatiscpp@yahoo.com (John the newbie)
Date: Sun, 16 Jun 2002 04:38:11 GMT
Raw View
Hi everybody,

yesterday, in the intent of writing '\t' I mistakenly wrote '\\t' and
my compiler accepted it without complaints. Hence I tried writing

char c = 'ab';

and it still accepted it! Is it legal to put 2 characters in one
literal? What is the meaning?

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: "Jim Fischer" <jfischer110@attbi.com>
Date: Sun, 16 Jun 2002 09:51:50 GMT
Raw View
"John the newbie" <whatiscpp@yahoo.com> wrote in message
news:102a8848.0206151734.4c559f6f@posting.google.com...
> Hi everybody,
>
> yesterday, in the intent of writing '\t' I mistakenly wrote '\\t' and
> my compiler accepted it without complaints. Hence I tried writing
>
> char c = 'ab';
>
> and it still accepted it! Is it legal to put 2 characters in one
> literal?

Yes.


> What is the meaning?

It's a multi-character literal whose type is 'int' and whose value is
implementation-defined (ref: 2.13.2/1).


Jim



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: Mon, 17 Jun 2002 05:02:14 GMT
Raw View
In article <102a8848.0206151734.4c559f6f@posting.google.com>, John the
newbie <whatiscpp@yahoo.com> writes
>Hi everybody,
>
>yesterday, in the intent of writing '\t' I mistakenly wrote '\\t' and
>my compiler accepted it without complaints. Hence I tried writing
>
>char c = 'ab';
>
>and it still accepted it! Is it legal to put 2 characters in one
>literal? What is the meaning?

IIRC, yes and I think the result is implementation defined.


--
Francis Glassborow      ACCU
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: whatiscpp@yahoo.com (John the newbie)
Date: Mon, 17 Jun 2002 18:02:52 GMT
Raw View
"Jim Fischer" <jfischer110@attbi.com> wrote
> "John the newbie" <whatiscpp@yahoo.com> wrote
> > Hi everybody,
> >
> > yesterday, in the intent of writing '\t' I mistakenly wrote '\\t' and
> > my compiler accepted it without complaints. Hence I tried writing
> >
> > char c = 'ab';
> >
> > and it still accepted it! Is it legal to put 2 characters in one
> > literal?
>
> Yes.
>
>
> > What is the meaning?
>
> It's a multi-character literal whose type is 'int' and whose value is
> implementation-defined (ref: 2.13.2/1).

Thanks. What's the rationale for them? Moreover, this means that '\n'
is a multicharacter literal, but it's treated in a special way and has
type char?

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]