Reputation: 3820
I have the following C code:
#include <stdio.h>
#include <strings.h>
int main(void){
char * str = "\012\0345";
char testArr[8] = {'\0','1','2','\0','3','4','5','\0'};
printf("%s\n",str);
printf("**%s**",testArr);
return 0;
}
See live code here
I'm having trouble understanding the results and I have googled but am unsure that I understand why a null character at the start of a string and why one in the middle would cause only the string "5" to display. Also, when I assign each string character to array testArr and then attempt to display that array of characters the result is different despite the string and the array having the same characters. So, I'm struck by the confounding results, especially their disparity. With the string str, does the code display "5" because the null characters overwrite what is in memory?
Also, with the array I created using the same characters, nothing displays of the data contained in array testArr. Is it that once the first null is encountered for some reason everything else is ignored? If so, why doesn't the same behavior occur with string str which contains the same characters?
Upvotes: 2
Views: 469
Reputation: 224546
An octal escape sequence is \
followed by one to three octal digits, per C 2018 6.4.4.4 1. Per 6.4.4.4 7: “Each octal or hexadecimal escape sequence is the longest sequence of characters that can constitute the escape sequence.” So, when the compiler sees "\012\0345"
, it interprets it as the sequence \012
(which is ten), the sequence \034
(which is twenty-eight), and the character 5
.
To represent the string you intended, you could use "\00012\000345"
. Since an octal escape sequence stops at three digits, this is interpreted as the sequence \000
, the characters 1
and 2
, the sequence \000
, and the characters 3
, 4
, and 5
. (A null terminating character will also be appended automatically.)
When you printed "\012\0345"
, the characters with codes ten and twenty-eight were printed but had no visible effect. (Your C implementation likely uses ASCII, in which case they are control characters. \012
is new-line, so it should have caused a line advance, but you probably did not notice that. \034
is a file-separator control character, which likely has no effect when printed to a regular terminal display.)
When you printed testArr
, the null character in the first position ended the string.
Upvotes: 8