即使条件在c程序中失败,if()仍在执行

时间:2017-06-26 17:08:05

标签: c if-statement

我在C中编写了一个程序。我正在使用goto语句然后我认为它是goto语句的错误但后来我使用了简单的printf()语句并发现它不是' t goto的错误,但是if语句的错误。这是我的计划:

#include <stdio.h>
#include <math.h>

float power(float a, float b);

int main()
{
    char z;
    float x, y;

    printf("please,enter the number on which you want to raise \nand the 
    number by which you want to raise other number respectively:- ");
    scanf("%f %f",&x,&y);

    printf("%f \n",power(x,y));
    fflush(stdin);
    printf("do you want to enter the numbers again? y/n:- ");
    scanf(" %c",&z);

    if(z == 'y');
    {
        printf("y\n");
    }
}

float power(float a, float b)
{  
    float d = pow(a,b);
    return d;
}

这是输出

 linuxman@Aspire:~/c programs/a raised to b$ ./a.out
 please,enter the number on which you want to raise 
 and the number by which you want to raise othe number resplectevly:- 4 2
 16.000000 
 do you want to enter the numbers again? y/n:- n
 y
 linuxman@Aspire:~/c programs/a raised to b$

我输入n,甚至打印y。为什么会这样?

2 个答案:

答案 0 :(得分:4)

使用GCC 6.3.0-12ubuntu2和警告启用编译,得到:

^[^{}]+\{[^}]+#(?!ffffff)[0-9a-f]{6}[^}]+\}

我通常不回答拼写错误的问题,但也许您应该考虑打开一些开关/升级您的编译器linuxman。

答案 1 :(得分:2)

if的谓词后面的分号(即(z =='y'))应该被删除,因为你在它之后放置的语句是if语句的结果。因此,应该阅读您的部分代码:

if(z=='y')
{
   printf("y\n");
}