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 |
/** * Fail: characters.c * Author: Risto Heinsar * Created: 06.11.2014 * Modified: 30.11.2021 * * Description: The example shows that characters in C are nothing * but just numbers from ASCII table and how they can be * manipulated one character at a time. */ #include <stdio.h> int main(void) { char character1 = 'a'; char character2 = 65; printf("Characters are just numbers based on ASCII table:\n"); printf("(%c %hhd)\n", character1, character1); // windowsis %hd printf("(%c %hhd)", character2, character2); printf("\n\n"); char character3 = character1 + 1; printf("You can do basic math with characters: %c + 1 -> %c\n\n", character1, character3); char character4 = character1 - 32; printf("Upper and lower case characters have an offset of 32. " "Notice that it's only a single bit (2^5) " "%c - 32 -> %c\n\n", character1, character4); char word[] = "cafe"; word[2] = 'k'; printf("Text is just an array of characters that can be indexed" " and manipulated one character at a time.\n" "The %s is a lie.\n\n", word); char string[] = "Text manipulation"; string[4] = '\0'; printf("The end of the string is denoted by a zero-terminator. " "Where-ever it is put, that's where the string is considered " "to end. E.g. if i put it in the middle of a word: %s\n\n", string); printf("You don't also need to start printing from the beginning: %s\n", &string[5]); printf("I can also print characters from a string: %c%c%c%c\n", string[0], string[1], string[2], string[3]); return 0; } |