📜  C 中所有形式的格式化 scanf()

📅  最后修改于: 2022-05-13 01:54:54.129000             🧑  作者: Mango

C 中所有形式的格式化 scanf()

C 语言具有允许在程序中输入和输出的标准库。 C 语言中的stdio.h标准输入-输出库具有输入和输出的方法。

scanf(): c 语言的 scanf() 方法,根据指定的类型从控制台读取值。它返回一个整数类型,成功匹配和分配的输入项的数量。

句法:

int scanf(const char*format, arg1, agr2, arg3, ...);

scanf()函数从标准输入(键盘)中读取字符,根据 format(const char*format) 中的转换规范对其进行解释,并通过匹配的参数列表 ( arg1, arg2, arg3, ... ) 存储结果。参数列表中的每一个都必须是一个指针,指示相应的转换后的输入应该存储在哪里。它停止时——

  • 它用尽了它的格式字符串(所有输入都被采用)
  • 当某些输入与控制规范不匹配时。

程序 1:使用格式化输入 scanf() 读取两个整数值:



C
// C program to implement the above idea
#include 
  
// Driver Code
int main()
{
    int a, b, c;
    c = scanf("%d %d", &a, &b);
    printf("Number of successful "
           "inputs read : %d",
           c);
  
    return 0;
}


C
// C program to implement the above idea
#include 
  
// Driver Code
int main()
{
    // Declaring character array
    char str[10];
  
    // & is not used
    scanf("%s", str);
    printf("%s", str);
    return 0;
}


C
// C program to implement the above idea
#include 
  
// Driver Code
int main()
{
    int x = 4;
  
    // Read but do not store
    // in x
    scanf("%*d", &x);
  
    // Print initialized value
    printf("%d", x);
  
    return 0;
}


C
// C program to implement the above idea
#include 
  
// Driver Code
int main()
{
    // Array of 10 character elements
    char str[10];
  
    // Can read maximum four characters
    scanf("%4s", str);
    printf("%s", str);
    return 0;
}


C
// C program to implement the above idea
#include 
  
// Driver code
int main()
{
    // For reading long int
    long int x;
    scanf("%ld", &x);
    printf("%ld", x);
  
    // For reading short int
    short int y;
    scanf("%hd", &y);
    printf("%hd", y);
    return 0;
}


C
// C program to implement the above concept
#include 
  
// Driver code
int main()
{
    // String from which input
    // to be read
    char str[] = "11:pm";
    int x;
    char time[3];
    sscanf(str, "%d:%s", &x, time);
    printf("%d:%s", x, time);
  
    return 0;
}


输出:

读取成功输入

成功的输入

说明:由于两个输入值都是整数,并且 scanf()函数的格式说明符是 '%d',因此读取输入值并且 scanf()函数返回读取的值的数量。

读取不成功的输入

不成功的投入

说明:因为这里输入的第二个参数是一个字符串,并且与格式说明符 '%d' 不匹配,该格式说明符是一个整数,因此 scanf 不会将该值存储到第二个参数中并返回 1,因为只有 1 个输入被成功读取,并且 scanf() 将在此处终止。



注意:由于 scanf() 中的参数列表必须是指针,这就是为什么它写成 &a, &b (&variablename 给出变量的地址)。如果 scanf()函数缺少“&”,则程序将显示未定义的行为,并且编译器不会显示错误。

程序 2:使用 scanf 读取字符串:

下面是使用格式化输入 scanf() 读取字符串的 C 程序:

C

// C program to implement the above idea
#include 
  
// Driver Code
int main()
{
    // Declaring character array
    char str[10];
  
    // & is not used
    scanf("%s", str);
    printf("%s", str);
    return 0;
}

输出:

读取字符串

注意: &不用于字符串读取,因为数组名称是指向第一个元素 (str[0]) 的指针。这里要注意的另一点是“%s”不读取空白字符(空格、换行符等)。以下是相同的输出示例:

说明:作为scanf() 遇到空格或换行符,它开始读取下一个参数(如果可用)。这就是为什么这里只读取 Hello 而不是 World。

格式字符串:如果仔细观察scanf()的语法,那么它有两个部分:



  1. 格式字符串。
  2. 参数的数量。

双引号中的所有内容都称为格式字符串,逗号(, ) 之后的所有内容都是匹配的参数。格式字符串可能包含-

  • 空白或制表符将被忽略。
  • 普通字符(不是 %),预期与输入流的下一个非空白字符匹配。
  • 转换规范,包括以下内容:
    1. 字符%。
    2. 一个可选的赋值抑制字符*。
    3. 指定最大字段宽度的可选数字。
    4. 一个可选的 h、l 或 L,表示目标的宽度。
    5. 一个转换字符。

转换规范以强制 %字符开始,以强制转换字符结束。例如 - d、s、f、i 等。如果需要添加任何点 2 – 4(可选),应按 % 和转换字符之间的顺序添加。

让我们通过一个例子来理解所有这些要点:

示例 1:以 11: PM 格式读取时间(一个整数值,后跟一个冒号 (:) 和一个字符串)。

阅读时间

注意:在上面的例子中 (:) 没有存储在任何变量中,它只是为了匹配给定的输入格式。

程序 3:以下是读取值但不存储在变量中的程序:

C

// C program to implement the above idea
#include 
  
// Driver Code
int main()
{
    int x = 4;
  
    // Read but do not store
    // in x
    scanf("%*d", &x);
  
    // Print initialized value
    printf("%d", x);
  
    return 0;
}

输出

输出



解释:输出是 4,而不是 10,因为使用了赋值抑制字符“*”,在使用它时,输入字段被跳过并且不进行赋值。

程序 4:以下是读取字符串的程序,但最多允许 4 个字符:

C

// C program to implement the above idea
#include 
  
// Driver Code
int main()
{
    // Array of 10 character elements
    char str[10];
  
    // Can read maximum four characters
    scanf("%4s", str);
    printf("%s", str);
    return 0;
}

输出

输出

说明:最多只能读取四个字符,但在输入中,第五个字符o 给出被跳过的。

程序 5:读取一个整数,但它的大小应该是长或短:

C

// C program to implement the above idea
#include 
  
// Driver code
int main()
{
    // For reading long int
    long int x;
    scanf("%ld", &x);
    printf("%ld", x);
  
    // For reading short int
    short int y;
    scanf("%hd", &y);
    printf("%hd", y);
    return 0;
}

输出:

输出



转换字符:

让我们来看看完整的转换字符列表:

Conversion CharacterInput Data
dDecimal integer (int*). For Example 10, 20.
Note: 10, 010 both means 10.
i

Integer (int*). The integer may be in octal (leading 0(zero)) or hexadecimal leading 0x or 0X  
Note:

  • 010 means 8 because it starts with 0 so it is octal number and 10(octal) = 8(decimal).
  • 10 means 10 (because no leading 0(zero) or leading 0x or 0X.
  • 0x10 means 16 because it starts with 0x so it hexadecimal number and 10(hexadecimal) =16(decimal).
o

Octal integer (with 0 or without leading zero) (int *)
Note: Any number entered will be treated as an octal number.

  • 10 means 8
  • 010 also means 8
uAn unsigned decimal integer (unsigned int*).
Note: It reads only positive number if you enter any negative number MSB(most significant bit will not be treated as a sign bit and will be taken for calculation )
x

Hexadecimal integer (with or without leading 0x or 0X) (int *).
Note: Any number entered will be treated as a hexadecimal number

  • 10 means 16.
  • 0x10 also means 16.
cCharacters( char*).The next input characters(default 1) are placed at the indicated spot the normal skip over white space is suppressed means it can read white space characters. To read the next non-whitespace character use 1s.
s

Character string (not quoted) (char*), use for reading the string.
Note:

  • White space characters cannot be read.
  • The size of the variable should be 1 extra character large as at the end NULL(‘\0’) is appended.
e, f, gFloating-point number with optional sign, optional decimal point & optional exponent (float*).
%Literal%, no assignment is made.

从其他字符串读取输入:sscanf()

在前面的示例中,输入是从键盘读取的。让我们看一下如何从其他字符串读取输入的示例。下面是实现上述概念的 C 程序:

C

// C program to implement the above concept
#include 
  
// Driver code
int main()
{
    // String from which input
    // to be read
    char str[] = "11:pm";
    int x;
    char time[3];
    sscanf(str, "%d:%s", &x, time);
    printf("%d:%s", x, time);
  
    return 0;
}
输出
11:pm

想要从精选的视频和练习题中学习,请查看C 基础到高级C 基础课程