0
char sentence[] ={'k','k','k','k','k','k','k','k'}; // (8 character)
std::cout << sentence << std::endl;

Then output is just kkkkkkkk.

But if we decrement characters of array (i.e. preceding array have 8 character after less 8 character)

char sentence[] ={'k','k','k','k','k','k','k'}; // ( 7 character)
std::cout << sentence << std::endl;

output:

kkkkkkk`\363\277\357\376.
Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
isabuhi
  • 13
  • 2
  • 9
    Undefined behavior, because the char array needs to be `'\0'` terminated to be used that way. – Eljay May 17 '21 at 14:20
  • 1
    You can't decrement the length of an array. What do you mean with that? Please show some more code to clarify. Ideally you would post a [mre]. Most likely both pieces of code exhibit undefined behaviour, thus output is inderterminate – Lukas-T May 17 '21 at 14:23
  • 1
    `for (char c : sentence) std::cout << c; std::cout << std::endl;` would do the job. – Scheff's Cat May 17 '21 at 14:23
  • 2
    Or maybe [`std::cout.write(sentence, sizeof sentence);`](https://stackoverflow.com/a/1542413/11613622) – brc-dd May 17 '21 at 14:24
  • 1
    You have a fixed length array here. How would you propose to decrement its length? – Logicrat May 17 '21 at 14:30
  • 2
    @Logicrat My understanding is that OP modifies the source so that the array has fewer initialisers, recompiles and runs the modified program. If this is not the case, OP may correct me. Regardless, they should clarify the question. – eerorika May 17 '21 at 14:31
  • 2
    You you have an array of `char` (not a null terminated C string) that you'd like to visually inspect and if adding a `\0` to it isn't an option, you could use `std::string_view(sentence, std::size(sentence))` to create a cheap wrapper that you can print. – Ted Lyngmo May 17 '21 at 14:50
  • @eerorika yes, your assumption exactly what I meant. – isabuhi May 17 '21 at 22:31

3 Answers3

5

The operand sentence decays to a pointer to first element of the array. The stream insertion operator overload that accepts const char* parameter requires that the pointer is to a null terminated array. If that pre-condition is violated, then the behaviour of the program is undefined.

sentence does not contain the null terminator character. You insert it into a character stream. The behaviour of the program is undefined.

Then output just "kkkkkkkk".

That's one potential behaviour. This behaviour isn't guaranteed because the behaviour of the program is undefined.

But if we decrement length of array then output kkkkkkk`\363\277\357\376.

That's one potential behaviour. This behaviour isn't guaranteed because the behaviour of the program is undefined.

eerorika
  • 232,697
  • 12
  • 197
  • 326
2

char arrays are often used for null-terminated strings, but not always.

When you write

const char* str = "Hello";

then str is a pointer to the first element of a const char[6]. 5 for the characters, and the rules of the language make sure that there is room for the terminating \0.

On the other hand, sometimes a char array is just that: An array of characters. When you write

char sentence[] ={'H','e','l','l','o'};

Then sentence is an array of only 5 chars. It is not a null-terminated string.

This two worlds (general char array / null-terminated strings) clash when you call

std::cout << sentence << std::endl;

because the operator<< overload does expect a null-terminated string. Because sentence does not point to a null-terminated string, your code has undefined behavior.

If you want sentence to be a string, make it one:

char sentence[] = {'H','e','l','l','o','\0'};

Or treat it like a plain array of characters:

for (const auto& c : sentence) {
    std::cout << c;
}
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
1

Try using

char sentence[] = {'k','k','k','k','k','k','k','k','\0'};

you must always use a '\0' at the end when you are declaring a char array manually and want to print it. Because either cout or printf function will look for '\0' character and will only stop printing if it finds '\0'. also make sure you wont you it in middle of your array as it wont print the complete array

  • "*you must always use a '\0' at the end when you are declaring a char array manually and want to print it*" - This is only true when you are printing the char array **as an null-terminated string** (as the OP's code is). `cout` and `printf()` both provide ways to print a char array that do not require a null terminator be present. – Remy Lebeau May 17 '21 at 18:56