scanf()関数:
scanf()関数は、フォーマットされた入力関数であり、標準の入力デバイス(キーボード)から入力情報を読み取ります。
呼び出し形式は、scanf( "<format string>"、<address table>)です。
scanf_s()関数:
scanf_s()はscanf()と同じ関数ですが、scanf_s()は「scanf()が文字列を読み取るときに境界をチェックしないため、メモリリークが発生する可能性があるため、scanf()よりも安全です。 「この質問は設計されています。
scanf_s()を使用して文字列を読み取る場合、オーバーフローを防ぐために読み取る最大文字数を示す番号を指定する必要があります。
例:(入力文字列の原因文字の数を数える)(デバッグ環境:visual studio 2010 C ++)
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<CountVowel.h>
int CountVowel(char str[])
{
int counter = 0;
int i;
for (i = 0; str[i] != '\0' ; ++i )
{ switch(str[i])
{ case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
++counter;
}
}
return counter;
}
void main()
{
char buffer[128];
printf("Please input a string:\n");
scanf_s("%s" , buffer,128); /* 这里必须要有128,以表明最多读取128个字符,如果写成scanf_s("%s",buffer),程序将无法执行到底,且编译器会提示“Unhandled exception at 0xfefefefe in array.exe:0xC0000005: Access tion.” 。当然在安全性要求不高的情况下,不 一定非要用scanf_s()函数,可用scanf("%s",buffer)代替。 */
printf("%d vowels appear in your string.\n",CountVowel(buffer));
system("pause");
}
著者:徐の実話
出典:CSDN
オリジナル:https://blog.csdn.net/silleyj/article/details/8545408
著作権:この記事はブロガーのオリジナル記事であり、複製されています。ボーエンのリンクを添付してください。