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
#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' };
    // - '\0' is the null terminator.
    // - It is an unprintable character.
    // - It goes right after the last character in a string, in memory.
    // - Do not print the null terminator.
    // - Null terminator only exists in memory, not in files.
    // - Any string literal (e.g., "ABC") implies the null terminator.
    //   - s2 and s3 are both arrays of 4 characters (not 3).
    // - Null terminator tells C functions that work with strings when they have 
    //   reached the end.
    //   - Without it they will go on reading into other memory that they shouldn't touch.
    // - If you accidentally print '\0' to the terminal, you won't see anything.
    //   - If you redirect the output to a file and open in Vim, you will ^@ for the
    //     null terminator.
    return EXIT_SUCCESS;
}
/* 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.