
The first chapter lays the foundation for programming. This article focuses on character constants in version 1.8.1. A character constant is enclosed within a pair of single quotes, such as 'O', where the compiler understands this symbol to represent the ASCII value of the letter O, which is 79. Similarly, you can use ' ' to denote a space or '9' to represent the numeral 9. It's important to note that the constant '9' refers to a character and should not be mistaken for the integer value 9. Unless a programmer memorizes the ASCII table, someone seeing 79 might not connect it to the letter O. However, the character constant 'O' clearly conveys its meaning. In C, characters can be treated like integers without requiring any special conversion. This allows you to add an integer to a character. For instance, if c is a character and n is an integer, c+n represents the nth character after c. Similarly, you can subtract an integer from a character, such as c-n representing the nth character before c. Additionally, you can subtract one character from another, for example, if c1 and c2 are both characters, then c1-c2 represents the distance between the two characters. Furthermore, you can compare two characters. If in the ASCII table, c1 precedes c2, then c1 < c2 is true. Suppose you're inputting a character and storing it in the variable ch. How would you determine whether the input character ch is a digit? For example: ```c if( ch >= '0' && ch <= '9' ) { ... } ``` This separates numeric characters from others in the ASCII table. While the standard C library, ctype.h, provides corresponding functions, implementing them from scratch can help deepen your understanding of their functionality. If ch is an uppercase letter, return its lowercase counterpart; otherwise, return ch itself, as shown in Listing 1.35. Program Listing 1.35 Sample Code for the tolower() Function: ```c 1 char tolower(char ch) 2 { 3 if( ch >= 'A' && ch <= 'Z' ){ 4 return (ch + ('a' - 'A')); 5 } else { 6 return (ch); 7 } 8 } ``` >>> 2. Character Input and Output Although the format specifier %c allows the scanf() and printf() functions to read and write individual characters. For example: ```c char ch; scanf("%c", &ch); printf("%c", ch); ``` However, before reading the characters, the scanf() function does not skip spaces. This means spaces are read as characters and stored in the variable ch. To resolve this issue, you must include a space before %c: ```c scanf(" %c", &ch); ``` Although scanf() does not automatically skip spaces, it is easy to detect if the character being read is a newline character '\n'. For example: ```c while(ch != '\n'){ scanf("%c", &ch); } ``` Alternatively, you can use the macros getchar() and putchar() to read and write individual characters. These are defined in stdio.h and are used to read data from the keyboard and print characters to the screen. Although macros and functions differ technically, their usage is similar. For example: ```c int getchar(void); // Reads one character int putchar(int ch); // Outputs one character ``` The getchar() function takes no arguments and returns a character from the input queue. For instance, the following statement reads a character input and assigns its value to the variable ch: ```c ch = getchar(); ``` This statement is equivalent to the following: ```c scanf("%c", &ch); ``` The putchar() function prints its argument. For example, the following statement prints the value previously assigned to ch as a character: ```c putchar(ch); ``` This statement has the same effect as the following: ```c printf("%c", ch); ``` Since these functions only handle characters, they are faster than the scanf() and printf() functions, which are typically defined in stdio.h. These are actually preprocessor macros, not real functions. Although these macros may seem straightforward, there can be issues. For example: ```c char ch1, ch2; ch1 = getchar(); ch2 = getchar(); printf("%d %d", ch1, ch2); ``` If the character 'a' is input, the printed result is "97, 10". This happens because a result is printed immediately after inputting a character from the keyboard. The program hasn't been fully entered yet. Keyboard input is stored temporarily in a storage area known as a buffer. Once the Enter key is pressed, the program can access the characters entered by the user. Thus, scanf() and getchar() are also buffered from the input stream. People often mistakenly believe that these functions take values from the keyboard buffer. In reality, the data is directly taken from the input stream buffer by the cin function, so when there is leftover data in the buffer, the cin function will read the leftover data without requesting new keyboard input. The 10 here is precisely the newline character '\n' entered by the Enter key. When the read data encounters the newline character '\n', the newline character is also read into the input stream buffer, so the first time input is accepted, the character leaves the character '\n', so the second time it will be taken directly from the buffer. Why is there a buffer? First, storing multiple characters as one block saves time by sending these characters one by one. Second, if the user makes a mistake, the error can be corrected directly via the keyboard. Once the Enter key is pressed, the correct input is transmitted. While the benefits of the input buffer are numerous, there are times when buffered input is unnecessary. For example, in a game, if you want to execute a corresponding command by pressing a key, both buffered and unbuffered input have their uses, but this book assumes all inputs are buffered. Buffers fall into two categories: fully buffered I/O and line buffered I/O. Fully buffered input flushes the buffer when it is filled and sends the content to the destination, usually in file input. The size of the buffer depends on the system, with common sizes being 512 bytes and 4096 bytes. Line buffered I/O flushes the buffer when a newline occurs. Keyboard input is usually line buffered, so the buffer is flushed after the Enter key is pressed. Getchar() and scanf() Getchar() reads each character, including spaces, tabs, and newlines; scanf() skips spaces, tabs, and newlines when reading numbers. Although both functions work well, they cannot be mixed. Although the parameter ch of putchar() is defined as an int type, it essentially receives a char type character, because inside putchar(), the system will cast ch to char type before using it. If the character output is successful, putchar() returns the output character ((char)ch) rather than the input parameter ch; if unsuccessful, it returns the predefined constant EOF (end of file), which is an integer. Getchar() has no input parameters, and its return value is int, not char. Here you need to distinguish between valid data in the file and the input terminator. When there is a character readable, getchar() does not return the end-of-file EOF, so: ```c ch = getchar() != EOF // Equivalent to ch = (getchar() != EOF) ``` The value is true and the variable ch is assigned the value 1. When there is no input, getchar() returns the file end character EOF, i.e., the expression evaluates to false. At this point, the variable ch is assigned a value of 0, and the program ends. If the return value of the getchar() function is defined as an int, both possible characters and the end-of-file EOF can be stored. An example of copying input to output is shown in Listing 1.36. Listing 1.36: Sample Program for Copying Input to Output ```c 1 #include
Li-Ion 36V Battery,Lithium Ion 36V Battery,Lithium Ion Battery 36V 20Ah,36V Lithium Marine Battery, Super Batteries,LFP Battery, High rate lithium battery
Langrui Energy (Shenzhen) Co.,Ltd , https://www.langruibattery.com