简体   繁体   English

谁能解释一下程序的输出?

[英]Can anyone please explain the output of the program?

#include<stdio.h>
#include<conio.h>
int main (void)
 {
  int a,b,c,d;
  clrscr();
  a=3;
  b=5;
  c=a,b;
  d=(a,b);
  printf("c=%d",c);
  printf("d=%d",d);
  getch();
 }

I am getting the output as c=3d=5 Can anyone please explain me that how am i getting this output? 我得到的输出为c = 3d = 5谁能解释一下我如何获得此输出?

Your code: 您的代码:

a=3;
b=5;
c=a,b;
d=(a,b);

is treated as if it was written: 就像被写为:

a=3;
b=5;
(c=a),b;
d=(a,b);

That's why c == 3 and d == 5 . 这就是为什么c == 3d == 5

A good compiler might warn you about the line containing the assignment to c ; 一个好的编译器可能会警告您有关c赋值的代码行; the evaluation of b does nothing to the state of the computation. b的求值对计算状态没有任何作用。 Similarly for the evaluation of a in the expression for d . 类似地,对于d表达式中的a的求值。


What about d=(a,b) — how does it display d=5 in the output? d=(a,b)怎么样—它如何在输出中显示d=5

The comma operator is the lowest priority of all operators in C. It is also important to remember that the commas in the argument list of a function are not comma operators. 逗号运算符是C中所有运算符的最低优先级。同样重要的是要记住,函数的参数列表中的逗号不是逗号运算符。

The behaviour of the comma operator exemplified by: 逗号运算符的行为示例如下:

x, y;

is to evaluate the expression x and discard the result, then evaluate the expression y (and the overall result is the value of y ). 是评估表达式x并丢弃结果,然后评估表达式y (总结果为y的值)。 There is a sequence point at the comma operator; 在逗号运算符处有一个序列点; that's a refinement that doesn't matter to you now, but may do in the future. 这种改进现在对您来说并不重要,但将来可能会适用。

In your expression: 在您的表情中:

d = (a, b);

the expression a is evaluated (it's 3) and ignored; 表达式a被求值(为3)并被忽略; then the expression b is evaluated (it's 5), and that is used as the result of the comma operator expression, and therefore of the parenthesized expression, and therefore the value 5 is assigned to d . 然后对表达式b求值(值为5),并将其用作逗号运算符表达式的结果,并因此用作括号表达式的结果,因此将值5分配给d

In contrast, in your expression: 相反,在您的表情中:

c = a, b;

the LHS of the comma operator is c = a , so a is evaluated (it's 3) and assigned to c . 逗号运算符的LHS为c = a ,因此计算a (为3)并将其分配给c This is then thrown away, and b is evaluated. 然后将其丢弃,并对b求值。 The overall expression statement just assigns 3 to c, therefore. 因此,整个表达式语句仅将3分配给c。

You might well ask, "Why is the comma operator useful?" 您可能会问:“为什么逗号运算符有用?”

There are places where it is useful, such as the initialization and increment sections of a for loop: 在某些地方有用,例如for循环的初始化和增量部分:

for (i = 0, s = p; *s != '\0'; i++, s++)
{
     ...
}

There are two comma operators there. 那里有两个逗号运算符。 The first contains a pair of assignments; 第一个包含一对作业;第二个包含两个作业。 the second contains a pair of expressions with side-effects. 第二个包含一对带有副作用的表达式。

Another place where it is sometimes used (though I'd argue it is seldom good style), is: 有时使用它的另一个地方(尽管我认为它很少是好的样式)是:

if (some_variable == A_VALUE)
    p = q++, r = z;

This 'saves' having to add braces around the condition body. “节省”必须在条件主体周围添加括号。 However, it is not good style to hide assignments like that. 但是,隐藏这样的作业并不是一种好的样式。 The code should be (give or take the placement of braces, which is controversial): 代码应该是(给出或使用大括号的位置,这是有争议的):

if (some_variable == A_VALUE)
{
    p = q++;
    r = z;
}

If you like Obfuscated C, there's the International Obfuscated C Code Contest . 如果您喜欢混淆C,那么可以参加国际混淆C代码竞赛

If you are writing macros, sometimes the comma operator can be a life-saver. 如果您正在编写宏,则有时逗号运算符可以起到救生作用。

Because the comma operator ( , ) has lower precedence than the assignment operator ( = ). 因为逗号( , )具有比赋值运算符(优先级低= )。 So your first example is equivalent to: 因此,您的第一个示例等效于:

(c=a),b;

See here: http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Operator_precedence . 参见此处: http : //en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Operator_precedence

The question revolves mainly around the statements 问题主要围绕陈述

c=a,b;
d=(a,b);

The = operator has lower precendence than the , operator, so the first statement means: =运算符的优先级低于,运算符,因此第一条语句的意思是:

(c=a),b;

The , operator returns the value of the second operand, so the second statement means: ,操作者返回第二个操作数的值,那么第二条语句的意思是:

a,(d=b);

OK, I will doit 好的,我会做

c=a,b;

Means do: 手段是:

c=a;
b;

d=(a,b);

Mean do: a; 意思是: b; b; assign the last "result" to d; 将最后的“结果”分配给d;

"," comma separates two statements. “,”逗号分隔两个语句。 The return of the last statement is the result of the combined statements 最后一条语句的返回是合并语句的结果

ur output will be c=3 d=5; 您的输出将是c = 3 d = 5; to visualise the result u should know the associativity of {=}-assignment operator and {,}-comma operator. 为了使结果可视化,您应该知道{=}-赋值运算符和{,}-逗号运算符的关联性。 ->because assignment (=) takes precedence over comma (,) your first statement c=a,b is equivalent to ->因为赋值(=)优先于逗号(,),因此第一个语句c = a,b等于

(a=3),5;

expressions separated by commas are always evaluated left to right. 用逗号分隔的表达式始终按从左到右的顺序求值。 So the value of an expression 所以表达的价值

(expr1, expr2 ) (expr1,expr2)

is always the value of the last expression. 始终是最后一个表达式的值。 Thus, in your second statement d=(a,b) the result of the right-hand side is d=5... 因此,在您的第二条语句d =(a,b)中,右侧的结果为d = 5 ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM