I would like to know the definition of the character/literal '\1' in the c language. Or more generally the definition of '\x' where latex{x\in\N} (is a natural number 0,1,2,3,4,5,6,...etc.)
I wrote this to try to figure it out but I would like an "authoritative" definition. Or even better how/where to find this "authoritative" definition. Would this be in the c standard? or compiler dependent? or something else?
#include <stdio.h>
int main(void) {
printf("test###_%c_###test\n", '\0');
printf("test###_%c_###test\n", '\1');
printf("test###_%c_###test\n", '\2');
printf("test###_%c_###test\n", '\3');
printf("test###_\1\2\3_###test\n");
// printf("tetest###_\0_###test\n"); ###this will cause a compiler warning
return 0;
}
Note: compiled with gcc version: gcc (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0 with no special option (i.e. gcc test.c)
The program outputs the following: test######test test######test test######test test######test test###__###test
I know '\0' is the null character, so that makes sense to me. But the next lines just seems to also be treated as "null characters" but not exactly. They more accurately just seem to be ignored. They don't signify the end of a string like the null character. For example, un-commenting the line with '\0' in the format string of printf, and ignoring the compiler error, stops the line printing at test###_ which makes sense since it's the "end" of the string. But '\0' is just ignored if it is "passed" into the format string? Do I need to look more into the implementation details of printf to understand this '\0' behavior?
Sorry, I know it's a few questions in one. Please let me know if I can make my question clearer or explain more.
I tried other "characters" like '\a' and get the same result, is this because these "escaped" characters are "unknown/undefined", compared to '\n', or something else?
CodePudding user response:
A \
followed by a sequence of up to three digits between 0 and 7 represent the octal code for a character.
So \0
has the value 0, \1
has the value 1, and \10
has the value 8. Assuming ASCII encoding, values less than 32 are control characters.
CodePudding user response:
These are integer character constants with octal escape sequence. From the C Standard (6.4.4.4 Character constants)
octal-escape-sequence:
\ octal-digit
\ octal-digit octal-digit
\ octal-digit octal-digit octal-digit
and
octal-digit: one of
0 1 2 3 4 5 6 7
Hexadecimal escape sequences start with \x
hexadecimal-escape-sequence:
\x hexadecimal-digit
hexadecimal-escape-sequence hexadecimal-digit
For example '\17'
is the same as '\xf'
that the both represent the decimal number 15
.