0

If we're outputting said array and the first character is \0, is it just ignored and the next character that isn't null treated as the first character?

6
  • 3
    You know you could just try it and see. It would be faster then asking a question and waiting for a response. Commented May 14, 2016 at 22:55
  • @NathanOliver I did, I just want to see if I'm right or if I did something wrong. Commented May 14, 2016 at 22:56
  • 2
    "I did..." - your observations and conclusions belong in your question as well. And your answer is entirely dependent on whether the output being performed is one that expects a nulchar-terminated character sequence (aka, a C-string). You included that moniker your tag list, but made no mention of it in your question. Commented May 14, 2016 at 23:02
  • How are you outputting the array? Commented May 14, 2016 at 23:03
  • @Bluasul Well, logically, how would you denote a string that's empty if the function were to skip over the first character? Go to the next character? And what if the second character is \0? Go to the next character? And what if that is \0? See where this is going? You could never have a string that's empty, or else that print function could wind up in never-never land looking for the first non-NULL character. Commented May 14, 2016 at 23:09

3 Answers 3

3

Depends on the 'outputting' function...if it knows the length of the array, it could output every element regardless of value. Most functions working with 'C strings' will stop at the first \0.

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

Comments

1

C-styled strings are by default, sentinel character arrays meaning they terminated at the first appearance of \0 (or some form of null), so it shouldn't be ignored. It should terminate the string, treating it as an empty string.

1 Comment

Only C strings behave this way. There's nothing special about 0 in other types of arrays.
0

By convention, if the "C string" (read char array) uses "string-ish" methods to do something with the string, it will stop at the first ASCII-zero (length parameters are regarded as "up-to" lengths). If it uses "array-ish" methods, it will regard a length parameter as a "precisely" length and iterate a for loop from zero to that size. The latter can often be found in binary handling, while the former is convention for string treatment of char arrays.

Examples in C standard headers are memxyz functions for binary array as opposed to strnxyz functions for strings.

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.