Previously, when taking input from the keyboard using the scanf function,
I explained that the program wouldn't know about input errors.
As a workaround, I will explain how to enter it as a string and then convert it to a number later.
C language provides a function called gets() that allows you to input a line of characters from the keyboard.
Note that you need to include <stdio.h> to use the `gets` function.
The following is how to use the gets() function.
gets function
gets(文字array);
The `gets` function, like `scanf`, will enter a waiting-for-input state.
The string entered by the user from the keyboard is stored within a designated character array.
"Also, while I'm at it, I'll also explain the puts function, which displays a line of text on the screen."
"Note that you need to include `` to use the `puts` function."
The usage of the puts function is as follows.
puts function
puts(文字array);
The specified string will be displayed on the screen.Also, it will always end with a newline.
While less functional than the printf function, the puts function is simpler for displaying strings only.
The following program is an example of displaying a string entered by the user.
The input string is being displayed as is.
Unlike when using %s with the scanf function, it works fine even with spaces.
Buffer overflow protection
It seems the author's cantankerous personality hasn't improved at all.
Because I intend to issue a prohibition against the use of the gets function I just explained.
When I explained earlier how to pass an array to a function,
I explained that a function is passed the address of the beginning of the array, and the size of the array is ignored.
I was only passing character arrays to the `gets` function before.
This means the `gets` function doesn't know the size of the character array.
This means it accepts input exceeding the number of elements, which leads to a bug.
"Since the previous program had 32 elements, entering more than 31 characters would cause a bug."
The following results are examples of what you'd see when you actually enter the input.
Execution results
0123456789012345678901234567890123456789 入力した文字列
When I tried to run it, an error screen appeared and it crashed.
As long as there is such a problem, the `gets` function cannot be used.
Alternatively, you can use the fgets function.
"Note that you need to include <stdio.h> to use the fgets function."
The fgets function is used as follows:
fgets function
fgets(文字array, arrayの要素数, FilePointer);
As can also be seen from the fact that the file pointer is being specified,
This function reads a string from a file.
However, the truth is that in C, all peripheral devices can be treated as files.
A file pointer named stdin is assigned to the keyboard.
"By specifying this stdin, functions that read from files can quickly switch to reading from the keyboard."
It's easy and reliable to know the number of elements in an array using the sizeof operator.
"That is, you can achieve a safe alternative to the `gets` function by doing the following."
It appears the input is being cut off at the point where the element count reaches its limit, preventing bugs.
Extract numbers and other values from a string.
While string input is now foolproof, issues remain.
"The difference is that it cannot accept numeric input, unlike the scanf function."
You need to convert the input string to a number.
The easiest way to convert a string to a number is to use the atoi function.
As it has already been explained, I will omit the details.
The following program is an example that displays the square of an input number.
Source code
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char str[32];
int val;
fgets(str, sizeof(str), stdin);
val = atoi(str);
printf("%d\n", val * val);
return 0;
}
The output of this program is as follows:
Execution results
5 25
"Since the atoi function ignores any characters other than digits in the input string,"
You won't get those inexplicable numbers you sometimes see with scanf.
Also, counting the number of characters in the original string reveals the number of digits entered.
It can also check for inputs that are too large.
Furthermore, the `atof` function is used for real numbers.It's the same way to use.
Simply entering numbers is sufficient with this.
"When you want to input multiple numbers separated by a delimiter, similar to the scanf function,"
You need to find the symbols yourself and convert each one to a number.
To extract words from a string, you use the strtok function.
Please note that you need to include <string.h> to use the strtok function.
The usage of the strtok function is as follows.
The strtok function searches for delimiters and replaces them with null terminators (EOS).
You can split a string into sections before and after a delimiter.
After that, you can obtain a numerical value by converting the word with the atoi function.
Return NULL if the word is not found.
The following program is an example of displaying a list of entered numbers.
Source code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main(void)
{
int i, j, val[10];
char str[32], *ch;
fgets(str, sizeof(str), stdin);
ch = strtok(str, ",\n");
for (i = 0; i < 10; i++) {
if (ch == NULL) {
break;
} else {
val[i] = atoi(ch);
}
ch = strtok(NULL, ",\n");
}
for (j = 0; j < i; j++)
printf("%d\n", val[j]);
return;
}
The output of this program is as follows:
Execution results
85,41,26,956,12 入力した文字列 85 41 26 956 12
Whether you increase the number of digits or enter a string,
You will never encounter an error.
About This Site
Learning C language through suffering (Kushi C) is
This is the definitive introduction to the C language.
It systematically explains the basic functions of the C language.
The quality is equal to or higher than commercially available books.