Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C代码的非逻辑输出_C_Pointers_Char_Nested If - Fatal编程技术网

C代码的非逻辑输出

C代码的非逻辑输出,c,pointers,char,nested-if,C,Pointers,Char,Nested If,我写了以下几行代码 #include "stdio.h" int main(){ float t,res; char c; scanf("%f",&t); getchar(); scanf("%s",&c); if (c=='R') res = 4/5 * t; else if (c=='F') res = (9/5 * t) + 32; else if (c=='K') res = t + 273; p

我写了以下几行代码

#include "stdio.h"

int main(){
    float t,res;
    char c;
    scanf("%f",&t); 
    getchar();
    scanf("%s",&c);
    if (c=='R') res = 4/5 * t;
    else if (c=='F') res = (9/5 * t) + 32;
    else if (c=='K') res = t + 273;
    printf("%.2f",&res);
    return 0;
}

我不知道为什么当我给出t=25和c='R'时,输出显示为0.00。控制台看起来像这样

25
R
0.00
有谁能给我一个解释吗

scanf("%s",&c);

改变

printf("%.2f",&res);

/
&
*
具有相同的优先级,因此表达式从左到右求值。因此,运营商的出现至关重要

因为4和5都作为整数计算,所以除法得到零 这就是为什么结果为零。要强制编译器将
4/5
视为浮点,可以明确地将至少一个值称为浮点:

res = 4/5.0F * t;
改变

改变

printf("%.2f",&res);

/
&
*
具有相同的优先级,因此表达式从左到右求值。因此,运营商的出现至关重要

因为4和5都作为整数计算,所以除法得到零 这就是为什么结果为零。要强制编译器将
4/5
视为浮点,可以明确地将至少一个值称为浮点:

res = 4/5.0F * t;
  • 首先,您可以按以下方式使用
    printf

    printf("%.2f",&res);
    
    但根据您想要打印的内容,您应该将其更改为:

    printf("%.2f", res);
    
    res = (float)4/5 * t;
    
  • 其次,当您阅读一个简单的字符时,请修改此行:

    scanf("%s",&c);
    
    致:

  • 最后也是最重要的是,声明:

    res = 4/5 * t;
    
    将计算整数商(如果
    4/5
    ,将给出
    0
    ),但您需要进行浮点除法。因此,您应该将其更改为:

    printf("%.2f", res);
    
    res = (float)4/5 * t;
    
    或:

    因为除法的两个成员之一需要是
    float
    ,才能得到想要的结果

    当然,除了铸造,您还可以:

    res = 4.0/5 * t;
    
    或:

    同样的情况也应适用于
    9/5*t
    ,它应更改为
    9.0/5*t
    9/5.0*t

      • 首先,您可以按以下方式使用
        printf

        printf("%.2f",&res);
        
        但根据您想要打印的内容,您应该将其更改为:

        printf("%.2f", res);
        
        res = (float)4/5 * t;
        
      • 其次,当您阅读一个简单的字符时,请修改此行:

        scanf("%s",&c);
        
        致:

      • 最后也是最重要的是,声明:

        res = 4/5 * t;
        
        将计算整数商(如果
        4/5
        ,将给出
        0
        ),但您需要进行浮点除法。因此,您应该将其更改为:

        printf("%.2f", res);
        
        res = (float)4/5 * t;
        
        或:

        因为除法的两个成员之一需要是
        float
        ,才能得到想要的结果

        当然,除了铸造,您还可以:

        res = 4.0/5 * t;
        
        或:

        同样的情况也应适用于
        9/5*t
        ,它应更改为
        9.0/5*t
        9/5.0*t

      • 当然

        printf("%.2f",res);  // without &
        

        或者更好

        4.0F/5   9.0F/5  // .0F makes literal of float type
        
        在表达式中

        但我的第二个建议是针对以下代码

        scanf("%f",&t); 
        getchar(); // cleaning the input stream from one `'\n'`
        scanf("%s",&c);
        
        最好将这部分写为

        scanf("%f",&t);
        while (getchar() != '\n'); // cleaning the input stream from everithing
        c = getchar(); // reading one character
        
        这种方法允许在输入以下内容后读取正确的字符

        1.34abdfsk

        R

        在这种情况下,
        abdfsk
        以及
        \n
        将被循环
        跳过,而(getchar()!='\n')

        当然

        printf("%.2f",res);  // without &
        

        或者更好

        4.0F/5   9.0F/5  // .0F makes literal of float type
        
        在表达式中

        但我的第二个建议是针对以下代码

        scanf("%f",&t); 
        getchar(); // cleaning the input stream from one `'\n'`
        scanf("%s",&c);
        
        最好将这部分写为

        scanf("%f",&t);
        while (getchar() != '\n'); // cleaning the input stream from everithing
        c = getchar(); // reading one character
        
        这种方法允许在输入以下内容后读取正确的字符

        1.34abdfsk

        R

        在这种情况下,
        abdfsk
        以及
        \n
        将被循环
        跳过,而(getchar()!='\n')

        • scanf(“%s”和&c)很有可能导致超出范围的访问,这会调用未定义的行为。使用
          scanf(“%c”和&c)
          %c
          而不是
          %s
          )只读取一个字符
        • printf(“%.2f”,&res)将调用未定义的行为,方法是将类型错误的数据传递到
          printf()
          。使用
          printf(“%.2f”,res)
          (不带
          )打印类型为
          double
          (或
          float
          ,将在可变长度参数中自动转换为
          double
        • 表达式
          res=4/5*t
          res=(9/5*t)+32
          可能无法满足您的要求,因为
          4/5
          9/5
          是整数除法,其中余数被截断。尝试使用
          res=4.f/5*t
          res=(9.f/5*t)+32
        • 您应该检查
          scanf()
          s是否成功以及读取的数据是否有效,以便不使用具有自动存储持续时间(不确定)的未初始化变量的值来调用未定义的行为
            • scanf(“%s”和&c)很有可能导致超出范围的访问,这会调用未定义的行为。使用
              scanf(“%c”和&c)
              %c
              而不是
              %s
              )只读取一个字符
            • printf(“%.2f”,&res)将调用未定义的行为,方法是将类型错误的数据传递到
              printf()
              。使用
              printf(“%.2f”,res)
              (不带
              )打印类型为
              double
              (或
              float
              ,将在可变长度参数中自动转换为
              double
            • 表达式
              res=4/5*t
              res=(9/5*t)+32
              可能无法满足您的要求,因为
              4/5
              9/5
              是整数除法,其中余数被截断。尝试使用
              res=4.f/5*t
              res=(9.f/5*t)+32
            • 您应该检查
              scanf()
              s是否成功以及读取的数据是否有效,以便不使用具有自动存储持续时间(不确定)的未初始化变量的值来调用未定义的行为
            printf()
            期望
            res
            ,而不是
            &res
            scanf(“%s”和&c)-->
            scanf(“%c”和&c)