From 0eace4863a54c94c090bf6e6c59f44ae77895eb6 Mon Sep 17 00:00:00 2001 From: xolatile Date: Thu, 9 Nov 2023 07:33:47 -0500 Subject: [PATCH] More text, unfinished chapter 1... --- chapters/chapter_1.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++ chapters/chapter_1.h | 22 +++++++++++++++++++++ 2 files changed, 78 insertions(+) diff --git a/chapters/chapter_1.c b/chapters/chapter_1.c index 3e08e78..2aed033 100644 --- a/chapters/chapter_1.c +++ b/chapters/chapter_1.c @@ -9,4 +9,60 @@ It is distributed in the hope that it will be useful or harmful, it really depen #ifndef CHAPTER_1_SOURCE #define CHAPTER_1_SOURCE +int character_is_uppercase (char character) { + return ((int) ((character >= 'A') && (character <= 'Z'))); +} + +int character_is_lowercase (char character) { + return ((int) ((character >= 'a') && (character <= 'z'))); +} + +int character_is_digit (char character) { + return ((int) ((character >= '0') && (character <= '9'))); +} + +int character_is_blank (char character) { + return ((int) ((character == ' ') || (character == '\t') || (character == '\r') || (character == '\n'))); +} + +int character_is_alpha (char character) { + return ((character_is_uppercase (character) != 0) || (character_is_lowercase (character) != 0)); +} + +int character_is_symbol (char character) { + return (character_compare_array (character, "~!@#$%^&*()+{}|:\"<>?`-=[]\\;',./")); +} + +int character_is_visible (char character) { + return ((int) ((character >= ' ') && (character <= '~'))); +} + +int character_is_invisible (char character) { + return (character_is_visible (character) == 0); +} + +int character_is_escape (char character) { + return ((int) (character == '\033')); +} + +int character_is_underscore (char character) { + return ((int) (character == '_')); +} + +int character_is_hexadecimal (char character) { + return (character_compare_array (character, "0123456789ABCDEF")); +} + +int character_compare_array (char character, char * character_array) { + int i; + + for (i = 0; i != string_length (character_array); ++i) + if (character == character_array [i]) { + return (1); + } + } + + return (0); +} + #endif diff --git a/chapters/chapter_1.h b/chapters/chapter_1.h index 60f1665..ab73e5b 100644 --- a/chapters/chapter_1.h +++ b/chapters/chapter_1.h @@ -65,6 +65,28 @@ Okay, now we're left with following actually useful and C-loving keywords, 18 of - return: Used in functions to return from them with or without a result (return value). It's best when function only uses it once. - break: When inside a loop, this statement will exit the loop. In newer standards it can take simple arguments, but we'll see why that's bad. - continue: Even more specific case, when inside a loop, this statement will skip to the end of the loop, and then continue again. + +Now, of those 18 actually useful C keywords, I like to avoid 'struct', 'switch', 'case', 'default', 'while', and use (functional) 'static' and 'extern' only in order to silence +compiler warnings, 'static' inside a function is more useful. That leaves us (me) with 12 C keywords that I love, out of complete 32 keywords in ANSI C standard. However, we'll +see that sometimes is preferable to use switch statement somewhere, or while loop when for loop feels like overkill. In most real-world cases, you'll need to use some API or +library that internally used structures everywhere, so you'll need to adapt to it, we'll see examples later... + +So, real men need these keywords { char, int, void, sizeof, static, if, else, for, enum, return }, and use the rest of them in order to silence compiler warnings, use some +standard library functions, clean the source code or access an API / library / header file. */ +extern int character_is_uppercase (char character); +extern int character_is_lowercase (char character); +extern int character_is_digit (char character); +extern int character_is_blank (char character); +extern int character_is_alpha (char character); +extern int character_is_symbol (char character); +extern int character_is_visible (char character); +extern int character_is_invisible (char character); +extern int character_is_escape (char character); +extern int character_is_underscore (char character); +extern int character_is_hexadecimal (char character); + +extern int character_compare_array (char character, char * character_array); + #endif