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 | #include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
// STRINGS
// ADVICE: Write lots of snippets to understand concepts and C language constructs.
int main(int argc, char* argv[]) {
char s0[4] = "264"; // string.... [4] means there will be four elements in this array.
printf("%s\n", s0);
char s1[] = "264"; // string
printf("%s\n", s1);
char s2[] = { '2', '6', '4', '\0' }; // s is an array of 4 characters: '2', '6', '4', and '\0'.
//
// s is a string.
//
// '\0' is the "null terminator" and marks the end of
// a string in memory. This is just a convention,
// but it is widely followed, at least in C programming.
printf("%s\n", s2);
// In memory, s1 and s2 are identical and indistinguishable. They are just two ways of telling
// the compiler (GCC) the same thing.
char s3[] = {0x32, 54, 52, 0 }; // s is an array of 4 characters: '2', '6', '4', and '\0'.
//
// s is a string.
//
// '\0' is the "null terminator" and marks the end of
// a string in memory. This is just a convention,
// but it is widely followed, at least in C programming.
printf("%s\n", s3);
// RULE: When you are referring to a character, use the character literal (aka character constant).
// Use either "264" or {'2', '6', '4', '\0'}, not the decimal or hexadecimal notation above.
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.