Introduction
Hello, my name is Kuramii!
I will be assigned to a team supporting mainly Altera® SoC and Nios® II starting in October.
The only language I used during the production training was hardware language (HDL), but I am now studying to learn software languages such as C.
So this time, I would like to write an article about an event that impressed me while learning C language.
What is "getchar()"?
The other day, I learned about a function called "getchar()". This is a function that "returns a character from the keyboard".
Figure 1 shows a program that returns a character from the keyboard, assigns it to the char variable ch, and then prints the character stored in ch to the screen using "printf()".
Figure 1
For example, if you type "altima" as shown in Figure 2, the character displayed on the screen will be "a" (Figure 3).
Figure 2 Figure 3
Using a loop statement
Now, to try using a loop statement to input a character multiple times, we created the code shown in Figure 4.
Figure 4.
After the code was created, I immediately typed in "altima," and lo and behold, not only a single
character, but a string of characters was displayed on the screen! (Figure 5)
My expectation was that when I typed "altima", only "a" would be displayed and I would have to wait for input from the keyboard (Figure 6).
Figure 5 Figure 6
I could not understand at all why the looping made it possible to display multiple characters. So I asked Poki, a senior member of the same team, and found out that I had benefited from something called a "line buffer.
I would like to explain what the line buffer is and what I was taught.
Thanks to the line buffer...!
A line buffer is a memory that temporarily stores data and other data until a newline code is entered.
When "getchar()" is used, the string entered from the keyboard until ENTER is pressed is temporarily stored in the line buffer. In this case, the string "altima" is stored in the line buffer. (Figure 7)
Then, when you try to display the string on the screen using "printf ()", the first character you entered will be printed. Since we entered "altima", "a" will be displayed on the screen.
Figure 7, Line buffer contents before output Figure 8, Line buffer contents after output
By repeating this process, even the last line feed code "¥n" is output and waits for input.
As a result, the code in Figure 4 is output as shown in Figure 5.
Conclusion
By looping the code "return a character using "getchar()" and print that character with "printf()"," it looks as if "all input strings are returned and the string is printed with "printf()". Thank goodness for the line buffer. While I was immersed in the excitement, Poki, a senior member of the same team, said to me, "Well, "scanf()" is a very simple method.
Well, you can use the "scanf()" statement to return the string input from the keyboard by choosing "%s" as the format specifier.
I also learned that "getchar()" can handle values in the range -128 to 127, while "scanf()" can handle values in the range -32768 to 32767. "scanf()" has a wider range.
From now on, I will use "scanf()" when I want to return strings.
The C language is very deep.
Please look forward to my next article!
New Engineer's Blush Blog Article List