skip to Main Content

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{xinN} (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_###testn", '');
        printf("test###_%c_###testn", '1');
        printf("test###_%c_###testn", '2');
        printf("test###_%c_###testn", '3');
        printf("test###_123_###testn");
        // printf("tetest###__###testn"); ###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 ” 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 ” 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 ” 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 ” 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?

2

Answers


  1. A followed by a sequence of up to three digits between 0 and 7 represent the octal code for a character.

    So 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.

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search