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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | #include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
int main(int argc, char* argv[]) {
char* s1 = "ABC"; // characters 'A', 'B', 'C', followed by '\0'
// '\0' is the null terminator and indicates the end of a string IN MEMORY (not files).
char s2[] = "ABC"; // array of size 4 (including '\0')
char s3[4] = "ABC"; // array of size 4 (including '\0')
char s4[] = { 'A', 'B', 'C', '\0' }; // array of size 4 (including '\0')
char s5[4];
s5[0] = 'A'; // character constant uses single quotes ('A' not "A")
s5[1] = 'B';
s5[2] = 'C';
s5[3] = '\0'; // null terminator
char s6[] = { 65, 66, 67, 0 }; // array of size 4 (including '\0')
// Remember: To the compiler, 'A' and 65 are exactly the same.
// Remember: Character constants use single quotes, e.g., 'A'.
// Remember: String literals use double quotes, e.g., "ABC".
char s7[4];
s7[0] = 65; // character constant uses single quotes ('A' not "A")
s7[1] = 66; // character constant uses single quotes ('A' not "A")
s7[2] = 67; // character constant uses single quotes ('A' not "A")
s7[3] = 0; // character constant uses single quotes ('A' not "A")
char s8[4];
s8[0] = 0x41; // hexadecimal integer constant
s8[1] = 0x42;
s8[2] = 0x43;
s8[3] = 0x00;
char s9[4];
s9[0] = 0101; // octal (base 8) integer constant
s9[1] = 0102;
s9[2] = 0103;
s9[3] = 00;
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);
printf("s9 == \"%s\"\n", s9);
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.