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 | #include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
// STRINGS ... are arrays of characters. Every char is just a byte.
int main(int argc, char* argv[]) {
char* s1 = "Shorp"; // '\0' is added implicitly when you use "▒▒▒"
printf("s1 == \"%s\"\n\n", s1); // \" includes a double quotation mark in your string (i.e., to print it)
char s2[] = "Shorp"; // an array of SIX chars because the '\0' is added implicitly.
printf("s2 == \"%s\"\n\n", s2);
char s3[6] = "Shorp";
printf("s3 == \"%s\"\n\n", s3);
char s4[] = { 'S', 'h', 'o', 'r', 'p', '\0' };
printf("s4 == \"%s\"\n\n", s4);
char s5[] = { 83, 104, 79, 82, 80, 0 };
printf("s5 == \"%s\"\n\n", s5);
char s6[] = { 0x53, 0x68, 0x6f, 0x72, 0x70, 0x00 };
printf("s6 == \"%s\"\n\n", s6);
// s2 … s6 are the equivalent. Compiler automatically interprets 'S', 83, and 0x53 as the same quanity.
// ∙ Type of s2…s6 is char[6]
// ∙ s2…s6 are on the STACK SEGMENT
//
// s1 is different from the rest.
// ∙ Type of s1 is char*
// ∙ The string referred to by s1 is on the DATA SEGMENT
// ∙ s1 itself is just the address in memory where you can find that string.
return EXIT_SUCCESS;
}
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */
|
© Copyright 2021 Alexander J. Quinn This content is protected and may not be shared, uploaded, or distributed.