C和C ++中的scanf()函数从命令行读取输入

本文详细介绍了C和C++编程中scanf()函数的使用,包括函数语法、返回值、格式说明符以及如何读取不同数据类型(如字符、字符串、整数、浮点数、十六进制)的输入。此外,还阐述了如何通过单个scanf()函数读取多个值。
摘要由CSDN通过智能技术生成

scanf() function is used to read input from the console or standard input of the application in C and C++ programming language. scanf() function can read different data types and assign the data into different variable types. The input data can be read in different formats by using format specifiers. The standard input can be also called as stdin in C and C++ terminology. The scanf()  function is provided by the stdio.h library or header.

scanf()函数用于以C和C ++编程语言从控制台读取输入或应用程序的标准输入。 scanf()函数可以读取不同的数据类型,并将数据分配为不同的变量类型。 通过使用格式说明符,可以以不同的格式读取输入数据。 在C和C ++术语中,标准输入也可以称为stdin。 scanf()函数由stdio.h库或标头提供。

scanf()函数语法 (scanf() Function Syntax)

As a function scanf() accepts different parameters. It has the following syntax and parameter meaning. We can also read multiple parameters with a single scanf() function. For single-parameter, only VARIABLE1 will be specified.

函数scanf()接受不同的参数。 它具有以下语法和参数含义。 我们还可以使用单个scanf()函数读取多个参数。 对于单参数,将仅指定VARIABLE1

int scanf(const char *FORMAT, VARIABLE1, VAARIABLE2, ...)
  • `int` is the return data type which is an integer after the reading input data. This will provide the status of the reading operation.

    int是返回数据类型,它是读取输入数据之后的整数。 这将提供读取操作的状态。
  • `scanf` is the function name.

    “ scanf”是函数名称。
  • `const char *FORMAT` is the format specifier of the input.

    const char * FORMAT是输入的格式说明符。
  • `VARIABLE1`, `VARIABLE2`, …  are the variable name in which value will be set from the user input.

    “ VARIABLE1”,“ VARIABLE2”,…是变量名,将从用户输入中设置值。

scanf()返回值 (scanf() Return Value)

The scanf() function will return an integer value which is not related to the input value. If the scanf() function is completed successfully the return value will be the input parameter count. If there is an error like matching failure, reading error or reach the end of file the return value will be less than zero which value is related to the error type.

scanf()函数将返回与输入值无关的整数值。 如果scanf()函数成功完成,则返回值将是输入参数计数。 如果出现诸如匹配失败,读取错误或到达文件末尾之类的错误,则返回值将小于零,该值与错误类型有关。

scanf()函数格式说明符 (scanf() Function Format Specifiers)

Before starting the scanf() example we will provide the format specifiers. Format specifiers are used to specifying the input data format which will be set for the given variable. The format should be the same as the given variable. There are different format specifiers for different variable types like below.

在开始scanf()示例之前,我们将提供格式说明符。 格式说明符用于指定将为给定变量设置的输入数据格式。 格式应与给定变量相同。 对于不同的变量类型,有不同的格式说明符,如下所示。

  • `%c` is used to input character input  for `char` variable type

    %c用于为char类型的变量输入字符
  • `%d` is used to input integer input for `int` variable type

    %d用于为int变量类型输入整数输入
  • `%e`,`%E`,`%f`,`%g`,`%G` are used to input floating number for `float` variable type

    %e,%E,%f,%g,%G用于输入float类型的浮点数
  • `%o&#
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值