-1

I'm not doing something right here.

This is my code:

#include<iostream>

using namespace std;

int main()
{
  char a='2Z';
  cout<<a<<endl;
  return 0;
}

But only Z is printed and not the 2 proceeding it. How can print the integer too?

Daria Pydorenko
  • 1,754
  • 2
  • 18
  • 45
  • 4
    A `char` can only contain a single character. You want a std::string. –  Aug 13 '18 at 15:23
  • A `char` can only hold 1 character. If you want more then one character use `std::string`. Also note that `'x'` is used for a literal character and `"xx"` is used for a literal string,. – super Aug 13 '18 at 15:23
  • This question is probably relevant, and definitely informative here: https://stackoverflow.com/questions/7459939/what-do-single-quotes-do-in-c-when-used-on-multiple-characters – Andrew Henle Aug 13 '18 at 15:26
  • It's not really clear what you're asking. Are you trying to store a string of multiple characters? Are you trying to store a single multibyte character? Are you trying to store an actual `int` value into a single-byte `char` type? – Andrew Henle Aug 13 '18 at 15:29
  • Possible duplicate of [What do single quotes do in C++ when used on multiple characters?](https://stackoverflow.com/questions/7459939/what-do-single-quotes-do-in-c-when-used-on-multiple-characters) – Alan Birtles Aug 13 '18 at 15:48

2 Answers2

1

You probably wanted std::string a = "2Z"; instead. Note the double quotation characters, and the change of type.

'2Z' is a multicharacter constant with an implementation-defined value, but a type int.

Most likely it's 256 * '2' + 'Z'. That's likely to be too big to fit into a char, and if char is signed on your platform, then that narrowing conversion again will be implementation defined. You get only the Z as your implementation seems to be computing (256 * '2' + 'Z') % 256 which is 'Z'.

In other words, multicharacter constants are best avoided, as are narrowing conversions to char types.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • 1
    I've seen (clear|evil) use of multicharacter constants, when every constant would be 4 characters and their values would have semantic meaning, reading as 4-characters (abbreviated) English words. Poor man's self-reflective enums. – SergeyA Aug 13 '18 at 15:31
  • @SergeyA: So have I. In some kernel code too. And my favourite answer: https://stackoverflow.com/questions/45550674/this-source-code-is-switching-on-a-string-in-c-how-does-it-do-that/45550696#45550696 – Bathsheba Aug 13 '18 at 15:32
  • Your favorite answer is the one wrote by yourself? And I thought of myself as a narcissist! :))) – SergeyA Aug 13 '18 at 15:33
  • @SergeyA: Ha. In all fairness I meant the favourite answer I've given. But let's leave my other comment in; there's nothing wrong with a bit of hubris from time to time ;-) – Bathsheba Aug 13 '18 at 15:34
0

You have 2 problems there, first you have single quotes, second you want to keep in one char a string, solution:

#include<iostream>
using namespace std;
int main()
{
 const char *a="2Z";
 char str[] = "234";
 std::string aString = "4444ZZZZZ"; // Using STD namespace

 cout<<a<<endl;
 cout<<str<<endl;
 cout<<aString<<endl;

return 0;
}
Simion
  • 353
  • 1
  • 9