I saw a comment that said initialization of a char array with "\001"
would put a nul as the first character. I have seen where \0
does set a nul.
The unedited comment:
char input[SIZE] = ""; is sufficient initialization. while ( '\001' == input[0]) doesn't do what you think it is doing if you have initialized input[SIZE] = "\001"; (which creates an empty-string with the nul-character as the 1st character.)
This program
#include <stdio.h>
#define SIZE 8
int main ( void) {
char input[SIZE] = "\001";
if ( '\001' == input[0]) {//also tried 1 == input[0]
printf ( "octal 1\n\n");
}
else {
printf ( "empty string\n");
}
return 0;
}
running on Linux, compiled with gcc
, outputs:
octal 1
so the first character is 1
rather than '\0'
.
Is this the standard behavior or just something with Linux and gcc
? Why does it not set a nul?
\0
by itself produces a NUL, but the standard behavior is for\001
to produce a character with numeric value 1, as you observed. (The general rule is that a backslash followed by one, two, or three octal digits is a single escape.) Please quote us the complete and unedited actual comment that you saw, with at least five lines of context above and below, and if possible provide a link to the entire program you are quoting. – zwol Jul 30 '18 at 18:02'\001'
and"\001"
– Saeid Yazdani Jul 30 '18 at 18:04