※スタイルシート未対応ブラウザではレイアウトを正確に再現できません。
  > | advanced by | contents  | that returns in <    
                   < [modosusu] > Color magazine monochrome light and shade   font predetermined, Gothic Ming-style type longhand   size Konaka large   standard  


  Input it as a character string of one line.   

  1. ..clause 1..: Keyboard input by gets function
  2. ..clause 2..: Measures of overrunning in buffer
  3. ..clause 3..: Take out the numerical value etc. of the character string.

[1] Keyboard input by gets function

When inputting it from the keyboard by the scanf function before
It explained it was not able to know it because of the program even if there was an inputting error.
As measures, it inputs as a character string, and it explains the method of reading as the numerical value in a different way later.

The gets function that inputs the character string of one line from the keyboard is prepared in C language.
To use the gets function # include should do < stdio.h >.
The usage of the gets function is as follows.


gets(文字配列);
If the gets function is executed, it becomes an input waiting state as well as the scanf function.
The character string that the user input from the keyboard is stored in the specified character array.

Moreover, it also explains the puts function that incidentally displays the character string of one line on the screen.
To use the puts function # include should do < stdio.h >.
The usage of the puts function is as follows.


puts(文字配列);
The specified character string is displayed on the screen. Moreover, it changes line without fail at the end of the character string.
If it is only a display of the character string, the puts function is easier than the printf function though it is a low function.

The following program is an example of displaying the character string that the user input as it is.

 
#include <stdio.h> 

int main(void)  
{
	char str[32];  
	
	gets(str);  
	puts(str);  
	
	return 0;
}
The execution result of this program becomes as follows.

Character string that inputs DRAGON QUEST 3
DRAGON QUEST 3
The input character string is displayed as it is.
There is no problem even if the blank is included unlike time that used %s by the scanf function.

It returns to contents.


[2] Measures of overrunning in buffer

Apparently, there seems still will not to be character that the author became crooked soon.
It is because the use ban on the gets function of just the explanation will be put out.

When you explain how to become it if the array is passed to the function before
It explained that it was in the initial address of the array to be passed to the function, and was disregarded the number of elements.

Only the character array had been passed to the gets function ahead.
Then, the gets function is said no understanding of the number of elements of character arrays.
In a word, the input that exceeds the number of elements accepts, and causes the bug.

The bug occurs if 31 characters or more are input because the number of program elements ahead was 32.
The following execution result is an example of the input actually and seeing.


0123456789012345678901234567890123456789 Input character string

A familiar error screen is displayed, and has been canceled if it executes it.

The gets function cannot be used because there is such a problem.
The fgets function can be used.
To use the fgets function # include should do < stdio.h >.
The usage of the fgets function is as follows.


fgets(文字配列,配列の要素数,ファイルポインタ);
As understood from the specification of the file pointer at the end
This function is a function to read the character string from the file.

However, all peripherals can be actually handled as a file in C language.
To the keyboard The file pointer named stdin is allocated.
This The function read from the file changes quickly for the keyboard if stdin is specified.


[ ..about   time.. equipment is a file treatment   ].
The file treatment of peripherals In the function taken with OS named UNIX,
Almost all install a similar mechanism on a present computer.
It is easy and is certain to use the sizeof function to know the number of array elements.
In a word, if as follows is done, a safe gets function or dividing is achieved.

Fgets (character array, sizeof (character 
array), and stdin);

[   Line-feed character   ]
Store the fgets function though the gets function doesn't store '\n'.
Retrieve '\n' to examine the end of the input.
The following program is an example of rewriting in a safe input method.
 
#include <stdio.h> 

int main(void)  
{
	char str[32];  
	
	fgets(str,sizeof(str),stdin);  
	puts(str);  
	
	return 0;
}
The result of inputting a long character string by this program and testing safety is as follows.

0123456789012345678901234567890123456789 Input character string
0123456789012345678901234567890
It is understood to break off the input in the part that becomes limits of the number of elements, and to prevent the bug occurring.

It returns to contents.


[3] Take out the numerical value etc. of the character string.

The problem has been still left though the input of the character string is thorough in this.
It is a point that the numerical value cannot be input unlike the scanf function.
It is necessary to read the input character string in a different way as the numerical value.

The easiest method of reading the character string in a different way as the numerical value is to use the atoi function.
Already omit a detailed explanation because it has explained.
The following program is an example of displaying the second power of the input numerical value.

 
#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 execution result of this program becomes as follows.

5
25
Because the atoi function is disregarded even if things except the figure are included in the input character string
It never becomes a numerical value not to understand division like the scanf function.
Moreover, because the digit number for the input figure is understood if the number of characters of former character strings is counted
Even if a too large numerical value is input, it is possible to check it.
Use the atof function for the real number. The usage is the same.

Though it is enough only if the numerical value is simply input by this
It wants to delimit two or more numerical values by the sign like the scanf function and to input it.
It is necessary to retrieve the sign at one's own expense, and to convert it into the numerical value respectively.


[ There is   sscanf function, too   ].
Though there is sscanf function that cuts out the numerical value from the character string, too
Do not treat here because there is a problem of looking like the scanf function.

Use the strtok function to take out the word from among the character string.
To use the strtok function # include should do < string.h >.
The usage of the strtok function is as follows.

 
/* address value = of */ word that takes out the 
first word Strtok (character array and delimiter);

/* address value = of */ word 
that takes out the following word Strtok (NULL and delimiter);

When the word is not found Return NULL. 


The strtok function retrieves the separator, and : the position Replace it with EOS.
The character string can be divided behind ahead of the separator.
Afterwards, if the word is converted by the atoi function, the numerical value can be obtained.

The following program is an example of displaying the list of the input numerical value.

 
#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 execution result of this program becomes as follows.

85, 41, 26,956, and 12 Input character string
85
41
26
956
12
Enlarge the digit number for the figure, and input the character string.
There must not be what becomes a fatal bug.

It returns to contents.


< - It is advanced -> | in | head that returns  to returning  next |.