-1

In this block of code I show you that I know a string array should end with a NULL character.

#include <iostream> void main(){ char c[4] = { 'a','b','c', '\0'}; //OR char c[4]={'a', 'b', 'c'} std::cout << c << "d"; } 

Output is: "abcd" -- What I was expecting: "abc d"

And in this one, I was taught that strings end with a NULL character in cpp.

#include <iostream> #include <string> void main() { string c = "abc"; std::cout << c << "d"; } 

Output is: "abcd" -- What I was expecting: "abc d"

16
  • Do you have a specific question? Commented Dec 11, 2017 at 21:27
  • 3
    The null marks the end of the string and is not printed. It's only reason to exist is to tell readers where to stop reading. Commented Dec 11, 2017 at 21:27
  • @JakeFreeman yes it is, try char c = 0; std::cout << "Hello" << c << "World"; Commented Dec 11, 2017 at 21:28
  • 2
    Nope. What happened is your terminal software didn't know what to do with a null, so it printed a space. Other common solutions are a box or a question mark. Commented Dec 11, 2017 at 21:38
  • 1
    @Shayan If you have too few initializers for an array of primitives, the unspecified elements are initialized to zero, but in your case c[3] doesn't even exist. Commented Dec 11, 2017 at 22:00

2 Answers 2

1

"Strings" when represented as a sequence of characters (like in char c[4]) are terminated by a special character, which you call the NULL character (actually '\0'). As datatypes like char c[4] do not maintain any additional information on the length of the string, this is the convention followed by functions interpreting an object of type char[] as a string to "know where to stop". cout interprets an input of type char[] as such a string and will stop before outputting this character, i.e. it will not print a space or something else. So character 'c' will be the last one to print, and whenn printing a "d" right afterwards no speparator will show up in between.

Note that char c[4]={'a', 'b', 'c'} will work, too, though the reason is a little bit subtle. Initializing a sequence of characters this way will - even if you pass less characters in the brace initializer than you declare as the size of the array - will implicitly fill up the array with '\0'. So it works. char c[4]={'a', 'b', 'c', 'd'}, in contrast, will yield undefined behaviour once you use this character sequence in a function that expects a '\0'-terminated string.

Sign up to request clarification or add additional context in comments.

3 Comments

If I do char c[5]={'a', 'b', 'c'} where is the NULL character going to be? in c[3] or in c[4]?
In both, c[3] and c[4]; the complete array will be filled up with it. For string functions, the first occurrence counts, i.e. that in c[3].
Going to add this for anybody else who might be interested: char c[4] = {'a','b','c','d'}; does not return an error but char c[4] = "abcd"; returns an error because it's too large, char c[4] = "abc" is acceptable.
1

The answer to your post is that the '\0' is not a space and thus your outputs are correct.

Hope this helps.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.