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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>

int main(int argc, char* argv[]) {
    char* s1   = "ABC";  // string on the DATA SEGMENT
             // s1 is the address of string on the data segment.
             // The characters ('A', 'B', 'C') are on the data segment.
             // The address where the characters can be found is on the stack segment.

    char  s2[] = "ABC";  // string on the STACK SEGMENT
                         // s2 is an array of char's on the stack segment.
    
    char s3[] = { 'A', 'B', 'C', '\0' };
    char s4[] = { 65, 66, 67, 0 };
    char s5[] = { 0x41, 0x42, 0x43, 0x00 };  // hex
    char s6[] = { 0101, 0102, 0103, 00 };  // octal
    char s7[] = "\x41\x42\x43";  // hex
    char s8[] = "\101\102\103";  // octal
    printf("s1 == \"%s\"\n", s1);
    printf("s2 == \"%s\"\n", s2);
    printf("s3 == \"%s\"\n", s3);
    printf("s4 == \"%s\"\n", s4);
    printf("s5 == \"%s\"\n", s5);
    printf("s6 == \"%s\"\n", s6);
    printf("s7 == \"%s\"\n", s7);
    printf("s8 == \"%s\"\n", s8);

    //char fmt_s4[] = { 's', '4', ' ', '=', '=', ' ', '\"', '%', 's', '\"', '\n' }; BUG!!!
    //char fmt_s4[] = "s4 == \"%s\"\n";  // would also work
    char fmt_s4[] = { 's', '4', ' ', '=', '=', ' ', '\"', '%', 's', '\"', '\n', '\0' };
    printf(fmt_s4, s4);

    return EXIT_SUCCESS;
}

/*
 char fmt[] = "s1 == %s\n"
 // how many characters are in fmt.
 { 's', '1', ' ', '=', '=', ' ', '%', 's', '\n' } 
 */
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */

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