C语言scanf函数转换说明表及其修饰符表

  1. 对于上一篇文章,总结printf()输出,C库也包含了多个输入函数, scanf()是最常用的一个,也是经常与printf()经常一起搭配使用的函数之一.

  scanf()和printf()类似, 也是使用格式字符串和参数列表. scanf中的格式字符串表明字符输入流的目标整数类型.两个函数主要的区别在参数列表中. printf()函数使用变量, 常量和表达式, 而scanf()函数使用指向变量的指针, 指针将在后续学习中总结. 使用scanf()有一下两个规则:

  1⃣️ 如果用scanf()读取基本变量类型的值, 在变量名前加上一个&;

  2⃣️ 如果用scanf()把字符串读入字符串数组中, 不要使用&.

  举例一:

#include <stdio.h>
int main(int argc, const char * argv[]) {
    int age;
    float assets;
    char per[30];
    printf("Enter your age,assets,and favorite
");
    scanf("%d",&age);
    scanf("%f",&assets);
    scanf("%s",per);
    printf("%d, $%.2f %s
",age,assets,per);
    return 0;
}

  打印结果

Enter your age,assets,and favorite
27
43.25
apple
27, $43.25 APPLE
Program ended with exit code: 0

  2. 总结完基本规则, 我们先把列表转换说明附上

  ANSI C 中scanf基本转换说明

   ANSI C 中scanf转换说明修饰符

   3. 从scanf() 角度看输入

  假设scanf()根据一个%d转换说明读取一个整数. scanf()函数每次读取一个字符, 跳过所有的空白字符, 直至遇到第一个非空白字符才开始读取. 因为要读取整数, 所以scanf()希望发现一个数字字符或者一个符号(+或-).如果遇到一个数字或符号, 它便保存该字符, 并读取下一个字符. 如果下一个字符是数字, 它便保存该数字并读取下一个数字. scanf()不断第读取和保存, 直到遇到非数字字符. 如果读取到一个非数字字符, 它便认为读到了整数的末尾. 然后,scanf()把非数字字符放回输入. 这意味着程序在下一次读取输入时, 首先读到的是上一次丢弃的非数字字符. 最后, scanf()计算已读取数字(可能)相应的数值, 并将计算后值放入指定的变量中.

 示例二: 

#include <stdio.h>
int main(int argc, const char * argv[]) {
    int age;
    char name[40];
    scanf("%d",&age);
    scanf("%s",name);
    printf("%d,and %s 
", age,name);
    return 0;
}

  打印结果

78 wuhan
78,and wuhan 
Program ended with exit code: 0

  4. 如果使用字段宽度, scanf()会在字段结尾或第一个空白字符处停止读取( 满足两个条件之一就停止)

#include <stdio.h>
int main(int argc, const char * argv[]) {
    char name[40];
    scanf("%10s",name);
    printf("%s 
",name);
    return 0;
}

  在运行中输入 ‘wuhanHangzShanghai’,打印结果

wuhanHangz

  5 如果使用%s转换说名, scanf()会读取除空白以外的所有字符. scanf()跳过空白字符开始读取第一个字符非空白字符. 并保存非空白字符直到遇到空白字符. 这意味这scanf()根据%s转换说明读取i一个单词, 即不包含空白字符的字符串.

  如果使用字段宽度, scanf() 在字段末尾或者第一个空白字符处停止读取. 无法利用字段宽度让只有一个%s的scanf()读取多个单词. 最后注意一点: 当scanf()把字符串放进指定数组中时, 它会在字符序列末尾加上‘’, 让数组中的内容成为一个C字符串.

  6 scanf()的返回值

  scanf()函数返回成功读取的项数. 如果没有读取到如何项, 且需要读取一个数字而用户却输入一个非数字字符串, scanf()返回0.

  当scanf() 检测到‘文件结尾’时, 会返回EOF (EOF是stdio.h中定义的特殊值, 通常用#define 指令把EOF定义为-1)

  示例三:

#include <stdio.h>
int main(int argc, const char * argv[]) {
    int n1,n2,n3,m1,m2,s,num;
    m1 = scanf("%d",&n1);
    m2 = scanf("%d %d",&n2,&n3);
    s = scanf("%d",&num);
    printf("Enter number n1 = %d, n2 = %d, n3 = %d,num = %d
",n1,n2,n3,num);
    printf("scanf return m1 = %d, m2 = %d, s = %d
",m1,m2,s);
    return 0;
}  

   输入‘12 45 678 string’, 打印结果

12 45 678 string
Enter number n1 = 12, n2 = 45, n3 = 678,num = 0
scanf return m1 = 1, m2 = 2, s = 0  
Program ended with exit code: 0

  7 printf() 和 scanf() 的*修饰符

  printf() 和 scanf() 都可以使用8修饰符来修改转换说明都含义. 但是, 他们都用法不太一样.

  如果你不想预先设定指定字段宽度, 希望通过程序来指定, 那么可以用*修饰符代替字段宽度. 但还是要用一个参数告诉函数, 字段宽度应该是多少. 也就是说转换说明是%*d, 那么参数列表中应包含*和d对应都值. 这个技巧也可应用于浮点值指定精度和字段.

  示例四:

#include <stdio.h>
int main(int argc, const char * argv[]) {
    unsigned width,precision;
    int number = 20;
    double weight = 242.5;
    printf("Enter a field 
");
    scanf("%d",&width);
    printf("The number is :%*d
",width,number);
    printf("Now enter a width and a precision:
");
    scanf("%d %d",&width,&precision);
    printf("Weight = %*.*f.
",width,precision,weight);
    printf("Done
");
    return 0;
}

  打印结果

Enter a field 
12 // 输入
The number is :          20
Now enter a width and a precision:
30 6 // 输入
Weight =                     242.500000.
Done
Program ended with exit code: 0

  scanf()中的*用法与此不同, 把*放在%和转换说明符之间,会使得scanf()跳过相应的输出项.

  示例五:

#include <stdio.h>
int main(int argc, const char * argv[]) {
    int n;
    printf("Please enter three integers:
");
    scanf("%*d %d %*d",&n);
    printf("The last integer was %d
",n);
    return 0;
}

  打印结果

  

Please enter three integers:
12 23 45 // 输入
The last integer was 23
Program ended with exit code: 0

  示例六

int main(int argc, const char * argv[]) {
    int n;
    printf("Please enter three integers:
");
    scanf("%*d %*d %d",&n);
    printf("The last integer was %d
",n);
    return 0;
}

  打印结果

Please enter three integers:
65 78 967 // 输入
The last integer was 967
Program ended with exit code: 0

  

原文地址:https://www.cnblogs.com/yyy6/p/8995449.html