-4

This is is the simple program that I was writing in C

#include <stdio.h>

int main(void){
  int i, j, k;
  i = 2; j = 3;

  k = i * j == 6;
  printf("%d", k);
}

so I know what this program is actually doing two values for the variable's are given here it does the calculation and then check;s the calculated value to the given condition.

Now here is what I am not getting When the program get executed it return's the value 1 when the given condition is satisfied and 0 if not, and i know that 1 stand's for true and o stand's for false but what i was thinking how doe's it do that i mean there is nothing in the program that tell' it to print 0 or 1 for the condition. Is it default in some C compilers to return that values or am i missing some point.

4

3 回答 3

3

There is nothing in the program that tells it to print 0 or 1 for the condition.

Yes there is, you print k and you assigned the result of the comparison to k. These are all equivalent (given that i = 2 and j = 3):

k = i * j == 6;
k = (i * j == 6);
k = (6 == 6);
k = 1;

Then you print it:

printf("%d", k); // Prints 1
于 2013-08-30T15:53:28.777 回答
1

正如您自己所说,C 中的逻辑表达式的计算结果为 0(假)或 1(真)。这正是“告诉它”放入01放入k变量的内容,具体取决于条件是否满足。

现在,您似乎在谈论您的程序“返回”的内容。我不确定您在这里所说的“退货”是什么意思。也许程序退出代码?你的main函数不包含任何return语句,这意味着在 C89/90 中它的返回值将是不可预测的,而在 C99 中它将保证返回0。我怀疑您使用的 C89/90 编译器只是maink.

于 2013-08-30T16:01:08.827 回答
0

在 C 语言中,没有真假,只有 1 和 0,或者更准确地说是 0 而不是 0。

于 2013-08-30T15:54:46.017 回答