+ 2
What is the output of this code?
char c='abcde'; cout<<c; Explain please...
3 odpowiedzi
+ 5
You can't assign multiple chars to one char variable. This works somehow on sololearn (taking the e as char), but it's not correct. Try using strings instead.
+ 3
The output will be an _error_, something about "overflow".
A char is a single character. You allocate memory for a single character with that declaration:
char c
but then assign it a literal string:
= 'abcde'
First, this is C++, not a Unix/Linux shell. We don't put strings in single quotations. We use double quotations only. "abcde" for a string,
'a' for a character.
Because characters are treater as any number: ( ' ' == 32) evaluates true. Every primitive type is a value. Difference is storage size.
Next, a char is like _all_ primitives. It holds a _single_ value.
What you want is an _array_. Please read about arrays, don't just use this to "make it work" quickly.
char c[] = "abcde\0";
/* Compiler adds the \0 in declaration-assignment single-clauses (is my wording okay?),
however being aware of null-termination is good practice. */
// A string is also useable
std::string s = "\nabcde\n";
std::cout << c << s;