
The foundation of programming starts with understanding basic concepts like character constants. This piece focuses on the concept of a character constant in version 1.8.1.
>>> 1. Reference to a Character Constant
A character constant is enclosed within a pair of single quotes, such as 'O'. The compiler recognizes that this symbol corresponds to the ASCII value of the letter 'O', which is 79. Similarly, ' ' can represent a space, and '9' can indicate 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. Without memorizing the ASCII table, seeing 79 wouldn't immediately suggest the letter 'O'. However, the character constant 'O' clearly conveys its meaning. In C, characters can be treated like integers without requiring any special conversions. This means you can add an integer to a character. For instance, adding the integer n to the character c results in c+n, representing the nth character after c. Subtracting an integer from a character is also possible, 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 indicates the distance between the two characters. Furthermore, you can compare two characters. If, according to the ASCII table, c1 precedes c2, then c1 < c2 evaluates to true. To determine whether an input character ch is a digit, you can use the following logic:If( ch >= '0' && ch <= '9' ) { ... }
This effectively separates numeric characters from others in the ASCII table. While the standard C library, ctype.h, provides relevant functions, implementing them yourself can deepen your understanding of their functionality. For example, if ch is an uppercase letter, return its corresponding lowercase letter; otherwise, return ch unchanged, as illustrated in Listing 1.35.>>> 2. Character Input and Output
Though the format specifier %c allows scanf() and printf() to handle single characters, such as:Char ch;
scanf("%c", &ch);
printf("%c", ch);
Before reading characters, scanf() does not automatically skip whitespace characters, meaning spaces are read as characters and stored in the variable ch. To address this issue, include a space before %c:scanf(" %c", &ch);
Although scanf() doesn’t skip whitespace, it’s straightforward to check if the character being read is a newline character '\n':while(ch != '\n'){
scanf("%c", &ch);
}
Alternatively, you can use getchar() and putchar() for reading and writing individual characters. These are macros defined in stdio.h, designed to read data from the keyboard and print characters to the screen. Despite some technical differences between macros and functions, their usage remains similar:int getchar(void); // Read one character
int putchar(int ch); // Write one character
The getchar() function takes no arguments and retrieves a character from the input queue. For example, the following statement reads a character from the keyboard and assigns its value to the variable ch:ch = getchar();
This is equivalent to:scanf("%c", &ch);
The putchar() function outputs its argument. For instance, the following statement prints the value of ch as a character:putchar(ch);
This is identical to:printf("%c", ch);
Since these functions deal exclusively with characters, they're faster than scanf() and printf(), which are typically defined in stdio.h. These are actually preprocessor macros rather than actual functions. Although seemingly simple, these macros can sometimes cause issues. For example:char ch1, ch2;
ch1 = getchar();
ch2 = getchar();
printf("%d %d", ch1, ch2);
If the character 'a' is input, the output will be "97, 10". This happens because a newline character '\n' is printed after inputting a character from the keyboard. The program isn't yet entered since the keyboard input is temporarily stored in a buffer. After pressing Enter, the program can access the user's input, so both scanf() and getchar() are buffered from the input stream. This creates the illusion that they are pulling data from the keyboard buffer, but in reality, the data is fetched directly from the input stream buffer. The '10' here is precisely the newline character '\n' created by pressing Enter. When reading data encounters the newline character '\n', it is also read into the input stream buffer. Therefore, during the first input, the newline character is removed, leaving a '\n' behind, which is why the second input retrieves it directly from the buffer. Why do we have a buffer? Firstly, grouping several characters together and sending them as a block saves time. Secondly, users can correct mistakes by using the keyboard before pressing Enter, ensuring the correct input is transmitted. While the benefits of input buffering are clear, some interactive programs may not require it. For instance, in a game where commands are executed by pressing keys, both buffered and unbuffered input have their uses. However, this text assumes all inputs are buffered. Buffers come in two types: fully buffered I/O and line buffered I/O. Fully buffered input flushes the buffer when it’s full, sending the contents to the destination, typically files. The buffer size depends on the system, commonly 512 bytes or 4096 bytes. Line buffered I/O flushes the buffer upon encountering a newline. Keyboard input is usually line buffered, so the buffer is cleared after pressing Enter.Getchar() and scanf()
Getchar() reads every character, including spaces, tabs, and newlines; scanf() skips spaces, tabs, and newlines when reading numbers. Both functions work well, but they shouldn't be mixed. The parameter ch of putchar() is defined as an int type, but it essentially receives a char type character. Inside putchar(), the system casts ch to char type before using it. If the character output is successful, putchar() returns the output character ((char)ch) instead of 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 differentiate between valid data in the file and the input terminator. When there is a character to read, getchar() doesn't return EOF, so: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, making the expression evaluate to false. At this point, the variable ch is assigned a value of 0, and the program ends. Defining the return value of getchar() as an int allows storing both possible characters and the end-of-file EOF. An example of copying input to output is shown in Listing 1.36.Listing 1.36: Copying input to output sample program
#include
int main(int argc, char *argv[])
{
int ch;
while((ch = getchar()) != EOF){
putchar(ch);
}
return 0;
}
Alternatively, you can use the alternative idiom of getchar() to replace Listing 1.36(6):while((ch = getchar()) != '\n')
A character to be read is compared with a newline character. If the test result is true, the loop body is executed, then the test loop condition is repeated, and a new character is read. Getchar() is used to search for characters and skip equivalent characters. For example:while((ch = getchar()) == ' ')
When the loop terminates, the variable ch will contain the first non-space character encountered by getchar().Do-while
The do-while loop is less common than for and while loops because it requires at least one execution of the loop body, placing the logical condition at the end of the code rather than at the beginning. Conditions should appear before the code they "protect," which is how if, while, and for work. Typically, the habit of reading code is from front to back. When using a do/while loop, you need to read this code twice. This method is incorrect in many cases, such as:do{
? ch = getchar();
? putchar(ch);
? }while(ch != EOF);
Since the test was placed after the call to putchar(), the code writes one extra character unnecessarily. A do-while loop is only correct if the loop body must be executed at least once. Another confusing aspect is the continue statement in a do/while loop:do{
? continue;
? }while(false);
Will it cycle forever or only once? Although it will only cycle once, most people might ponder this. Bjarne Stroustrup, the creator of C++, said, "The do statement is the source of errors and confusion. I tend to place conditions where I can see them and avoid using the do statement."In conclusion, understanding the intricacies of character handling and control structures like do-while loops is crucial for mastering programming fundamentals. These tools form the building blocks for more complex applications and algorithms. By experimenting with these concepts, developers gain deeper insights into how programs function and interact with data.
Brand MIGHTY MAX BATTERY
The battery is composed of nickel -cadmium batteries
Recommended purpose of the product IROBOT
Voltage 14.4 volts
Rechargeable
500-2000Mah Lithium Battery,2000Mah Rechargeable Battery,5000 Mah Power Bank,5000Mah 26650 Battery,Cylindrical Lithium Battery
Langrui Energy (Shenzhen) Co.,Ltd , https://www.langruibattery.com