Cprimer笔记:scanf函数转换规则和在判断语句中用法

scanf使用的转换说明字符与printf基本没有差别,主要区别在printf把%f,%e,%E,%g同时用于float和double类型,

而scanf只把它们用于float类型,用于double时要用l修饰符。

在循环语句用法:

#include <stdio.h>
void temperatures(double n);
int main (void)
{
    double h;
 
 
    printf("Please enter the fahrenheit: ");
    while(scanf("%lf",&h)==1){
        temperatures(h);
        printf("Please enter the fahrenheit: ");
    }
    printf("end\n");
    return 0;
}
void temperatures(double n)
{
    const double x=1.8,y=32.0,z=273.16;
    double c,k;
 
 
    c=x*n+y;
    k=x*n+y+z;
    printf("Fahrenheit is: %.2f\n",n);
    printf("Celsius is: %.2f\n",c);
    printf("Kelvin is: %.2f\n",k);
}

C primer plus5-8题,scanf语句作为判断语句,它的返回值表示成功输入的变量的数目,非数字并不会被成功输入,没见过的语句副作用。

同时,也相当于在循环语句后面加入了scanf语句,不需要重复加入输入语句。

猜你喜欢

转载自blog.csdn.net/lcqin111/article/details/79950405