找到给定代码的输出并给出适当的解释 [英] Find the output of the given code with proper explanation

查看:47
本文介绍了找到给定代码的输出并给出适当的解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<conio.h>
#include<stdio.h>
using namespace std;
int main()
{   int i=4,j=-1,k=0,w,x,y,z;
  w=i||j||k;
  x=i&&j&&k;
  y=i||j&&k;
  z=i&&j||k;
	printf("w=%d x=%d y=%d z=%d\n",w,x,y,z);
	getch();
	return 0;
}





我的尝试:



我有输出但我无法理解.... PLzz给我一些解释



What I have tried:

I have got the output but i can't understand that ....PLzz provide me some explanation

推荐答案

对不起,这是你的作业,不是我们的 - 所以你必须在这里工作。



首先记住C没有真或假 - 它认为任何数字不为零为真,任何数字为零为假。

然后记住||和&&是逻辑运算符,而不是二进制:所以输出|| b或&& b为true(非零)或false(零)。

最后,查看逻辑AND和OR运算符的作用:它非常简单,结果很明显。
Sorry, but it's your homework, not ours - so you will have to do the work here.

Start by remembering that C does not have a "true" or "false" - it considers any number that is not zero to be "true", and any number that is zero to be "false".
Then remember that || and && are logical operators, not binary: so the output of a || b or a && b with be "true" (non-zero) or "false" (zero).
Finally, look up what logical AND and OR operators do: it pretty simple, and the results become obvious.


首先让我们把它写成一个正确的 C 程序(因为 C ++ 不是 C ):

First let's write it as a proper C program (as C++ is not C):
#include <stdio.h>
int main()
{
  int i=4, j=-1, k=0, w, x, y, z;
  w = i || j || k;
  x = i && j && k;
  y = i || j && k;
  z = i && j || k;
  printf("w=%d x=%d y=%d z=%d\n", w, x, y, z);
  getchar();
  return 0;
}





然后,考虑



Then, consider

w = i || j || k = (4) || (-1) || (0) = true AND true AND true = true = 1;



其中带有'true','AND'等的表达式是'imaginary',只是为了说明评估是如何发生的。


where the expressions with 'true', 'AND', etc. are 'imaginary', just there to illustrate how evaluation happens.

x = i && j && k = .. = true AND true AND false = false = 0;




y = i || j && k = .. = true OR (true AND false) = true OR false = true = 1;




z = i && j || k = .. = (true AND true) OR false = true OR false = true = 1;





在您看到的最新两个表达式中 C 运营商优先规则 [ ^ ]正在行动(&的优先级;& 运算符高于 || 运算符的优先级。



in the latest two expressions you see the C operator precedence rules[^] in action (the precedence of && operator is higher than the precedence of the || operator).


这篇关于找到给定代码的输出并给出适当的解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆