Little knowledge of the C language - scanf()
function
scanf()
Function:
Specified format input from the standard input device. Where: standard input device refers to an "embedded input devices (such as: keyboards)" (personal opinion).
scanf()
Prototype:
Scanf int (const char * the restrict the format, ...);
(when using this function call header <stdio.h>)
scanf()
Idiom:
- Use one: enter a single character / number:
#include <stdio.h>
int main()
{
int n; //定义一个整型数据n
char ch; //定义一个字符数据ch
printf("Please input a number:");
scanf("%d",&n); //输入一个数字
printf("\n You input number is %d\n",n);
printf("Please input a character");
scanf("%c",&ch); //输入一个字符
printf("\n You input character is %c\n",ch);
return 0;
}
- Usage of Two: enter multiple characters / numbers:
#include <stdio.h>
int main()
{
int n1,n2,n3;
char ch1,ch2,ch3;
printf("Please input a number:");
scanf("%d %d %d",&n1,&n2,&n3); //输入多个数字,输入空格或回车表示单个数字输入结束(回车为截止符)
printf("\n You input number is %d\n",n);
printf("Please input a character");
scanf("%c0%c0%c",&ch1,&ch2,&ch3); //输入多个字符,输入数字0或者回车表示单个字符输入结束
printf("\n You input character is %c\n",ch);
return 0;
}
- Usage three: the output
scanf()
function return value:
#include <stdio.h>
int main()
{
int n;
int ch;
printf("Please input a number:");
ch = scanf("%d",&n);
printf("\n You input number is %d\n SIZE is %d\n",n,ch); //此处在scanf()函数返回值中有涉及
}
scanf()
Function return value:
- The function returns a value of type int.
- If a and b have been successfully read, then the return value is scanf 2;
- If only a read is successful, the return value is 1;
- If a and b are not successfully read, the return value is 0;
- If you encounter an error or encountered end of file, the return value is EOF. end of file or as Ctrl + z Ctrl + d
scanf()
Function conversion specifier:
About printf()
introduction and function of scanf()
the function of conversion and conversion specification modifier in the small knowledge of C language - the printf()
function has been described in detail.