1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>

int main(int argc, char* argv[]) {
    char ch = 'A';   // -127 to 128
    printf("ch == '%c'\n", ch);

    unsigned char u_ch_A = 'A';  // 0 to 255
    printf("u_ch_A == '%c'\n", u_ch_A);
    
    // Files contain bytes.  Each byte has range 0 to 255.  So we use unsigned char.
    unsigned char u_ch_0xff = '\xff';  // '\xff' is the character with value 0xff.
    // printf("u_ch_0xff == '%c'\n", u_ch_0xff);
    // printf("'\\xfa' == '%c'\n", '\xfa');
    printf("u_ch_0xff == 0x%02x\n", u_ch_0xff);
    printf("'\\xfa' == 0x%02x\n", (unsigned char) '\xfa');
    // If you are using character constant to initialize an unsigned char, use a typecast.
    
    char* s = "AB\xaa";
    printf("s[0] = 0x%02x\n", (unsigned char) s[0]);
    printf("s[1] = 0x%02x\n", (unsigned char) s[1]);
    printf("s[2] = 0x%02x\n", (unsigned char) s[2]);

    return EXIT_SUCCESS;
}
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */

© Copyright 2022 Alexander J. Quinn         This content is protected and may not be shared, uploaded, or distributed.