c - C中的 boolean 表达式

标签 c boolean boolean-operations

我在 C 程序中找到了这个表达式,但我没有理解它:

 struct stack_rec *ss;                                          
 ss=(struct stack_rec *)EMalloc(sizeof(struct stack_rec));       
 if (ss) {                                                      
   int res;                                                     
   res = (ss->elem  = * i , 1); // what does this mean ????
   if (res <= 0)                                                
     return res;                                                
   if (*s == 0) {                                               
     ss->next = 0;                                              
   } else {                                                     
     ss->next = *s;                                             
   }                                                            
   *s = ss;                                                     
   return 2;                                                    
 }                                                              
 return 0;                                                      

res = (ss->elem = * i , 1); 是什么意思?它是一个 boolean 表达式吗?我试过用 0 而不是 1,它总是返回第二个参数的值!谁能解释一下这个表达式?

最佳答案

看起来坏了。这是对逗号运算符的使用,它只计算最终表达式的值,即 1

因此,由于该代码等同于:

ss->elem = *i;
res = 1;

res 的后续测试似乎毫无意义,因此被破坏了。

关于c - C中的 boolean 表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12213747/

相关文章:

java - 这 ? boolean 禅宗运算符

c - fork() 系统调用和 while 循环(第 2 部分)

c - 将变量类型与 token 合并

c - 为什么通过直接函数调用替换变量时会得到不同的结果?

c - 为树添加值

python - 不明白为什么当变量为整数时 if 条件的计算结果为 True

java - 在类之间传递 boolean 值未按预期工作

php - 使用 php 在 mysql 数据库中保存复选框( boolean 值)值

sql - 在tsql中使用 bool 代数来避免CASE语句或处理复杂的WHERE条件

C++ bool 代数