C:奇怪的条件printf行为

时间:2017-05-30 18:54:42

标签: c printf

while(fscanf(f, "%s %s", logIn, pass) == 2){
    i = tabela_insere(logPass, logIn, pass);
    if(i != 0);
    {
        tabela_apaga(logPass);
        printf("%d diferente de 0???\n", i);
        return;
    }
}

非常直白:这里有什么问题?

因此,如果该函数“tabela_insere()”的结果与0不同,则此循环仅打印任何内容,但正如我们从printf结果中看到的那样,我是0 ...为什么打印呢?

我认为这不重要但是这里使用了所有变量类型:

int i; //not a global variable aka can't be changed in the function
char logIn[26],
     pass[26];
tabela_dispersao *logPass; //it's an hash table

我正在使用MinGW进行编译,没有错误或警告。

1 个答案:

答案 0 :(得分:1)

if语句后面有一个分号导致问题。

if (i!=0);
{
     //this will always execute
}

将其更改为

if (i!=0)
{
  //this will execute if i != 0
}

编译器不会警告您,因为第一个语句在语法上是有效的。

相关问题