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?
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?
 
    
    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.
 
    
    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;
}
