C++对and、or等运算符的支持

本文解析了一段C代码的编译问题,指出增加头文件后代码可以成功编译,并介绍了ISO646标准下支持的一系列运算符。文中还提到了在OceanBase代码中遇到的误用并被审查的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// test.c
// gcc test.c  --- compile error
#include <stdio.h>
int main()
{
  int a = (0) or (0);
  printf("%d", a);
  return a;
}

上面这段c代码能编译通过吗?答案是不能。gcc test.c


增加一个头文件,换成这样呢:

// test.c
// gcc test.c  --- compile success
#include <stdio.h>
#include <iso646.h>
int main()
{
  int a = (0) or (0);
  printf("%d", a);
  return a;
}


答案是能!

对于C++,不增加头文件,依然能编译过下面的test.cpp文件。

// test.cpp
// g++ test.cpp     ---- compile success<pre name="code" class="cpp">#include <stdio.h>
#include <stdio.h>
int main()
{
  int a = (0) or (0);
  printf("%d", a);
  return a;
}


Oh shit, g++什么时候自动支持这么”牛逼“的运算符了,这不是php和python的专利吗?不光是and or,ISO646标准一口气支持了一堆运算符:

定义为
and&&
and_eq&=
bitand&
bitor|
compl~
not!
not_eq!=
or||
or_eq|=
xor^
xor_eq^=

为什么这么做:http://zh.wikipedia.org/wiki/Iso646.h

做到了什么程度:http://zh.wikipedia.org/wiki/ISO/IEC_646

之所以发本文,是发现OceanBase代码中我写了一个or,被千拂同学review的时候发现。不过,居然没有被编译器发现。最近php写多了是么?


Product Description The author uses practical, concise code examples to illuminate a useful programming stratagem or warn against a dangerous practice. Readers will come away with a better understanding of how C++ is used in the real world. From the Inside Flap In the hands of an expert, C++ helps designers and programmers build systems that are modular, maintainable, and fast. To the novice, however, the size of the language can be intimidating. There are a lot of features in C++ and it takes some experience to learn which ones are appropriate for any situation. This book is intended to enhance and expedite that learning process. Most successful C++ programmers cannot recite chapter and verse from the language rules; instead, they have acquired a set of idioms and techniques that have worked well for them. Our goal is to help the C++ novice learn those idioms that have been most useful in practice. We also point out some of the most common pitfalls. We do not try to cover the entire language and we leave the ultra-precise definitions of language semantics to the reference manuals. Instead, we concentrate on helping the reader build programs that can be understood by someone who is not a C++ language lawyer. We not only discuss techniques for making programs elegant and fast; we also show how to make them easier to understand and maintain. Acknowledgements Almost none of the ideas and programming idioms in this book are my invention. My goal has been to present, in a way that allows novice C++ programmers to learn them quickly, what I consider to be the most important strategies and tactics I have learned from others in the eight years I have been using C++. Some of these lessons were learned by studying actual development projects as they moved from C to C++; others came from discussions with talented individuals. Many of the best ideas on templates and library design, including the ideas behind many of the container classes in this book, came from classes in the USL Standard Components that were originally designed by Martin Carroll, Andrew Koenig, and Jonathan Shopiro. I claim exclusive ownership of any errors in my versions. Andrew Koenig was a valuable resource as the local C++ language lawyer. The participants in the "C++ Strategies and Tactics" seminars I presented at several conferences helped inspire this book and refine its ideas. Other important ideas came from Tom Cargill, John Carolan, Jim Coplien, Mark Linton, Gerald Schwarz, and of course Bjarne Stroustrup, who also invented the C++ programming language that made the book possible in the first place. Brian Kernighan read several drafts of this book, and his excellent feedback has been a lot of help. I would also like to thank David Annatone, Steve Buroff, Tom Cargill, Bill Hopkins, Cay Horstman, Lorraine Juhl, Peter Juhl, Stan Lippman, Dennis Mancl, Scott Meyers, Barbara Moo, Lorraine Weisbrot Murray, Bjarne Stroustrup, Clovis Tondo, Steve Vinoski, and Christopher Van Wyk for their comments on early drafts of this book. Lorraine Weisbrot Murray also contributed the encouragement, understanding, support, and love that helped make the entire effort feasible. Rob Murray
<think>嗯,用户问C语言有没有OR判断符。首先,我得回想一下C语言的逻辑运算符。记得C语言里逻辑运算符是&&和||,分别对应逻辑与和逻辑或。那用户说的“or判断符”可能是指逻辑或运算符,或者可能用户是从其他语言比如Python里的"or"过来,产生混淆。 接下来,要确认C语言是否支持"or"这样的关键字作为逻辑或。记得C语言标准中,逻辑或的运算符是||,而"or"可能是在某些编译器中的扩展,比如在C++中可以使用"or"作为||的替代,但C语言是否支持呢?可能需要查证一下。 根据C标准,比如C89、C99、C11等,标准的逻辑运算符是||,而"or"并不是C语言的关键字。不过,在C99标准中,如果包含了<iso646.h>头文件,可以使用"or"作为||的宏定义。不过这种情况属于扩展,不是所有编译器默认支持,需要包含特定的头文件。 所以,用户的问题需要分情况回答:标准的C语言中,逻辑或是用||,而"or"可能作为扩展存在,但需要特定条件。要告诉用户这两种情况,避免他们直接使用"or"导致编译错误。 另外,用户可能对逻辑运算符和按位运算符有混淆,比如|是按位或,而||是逻辑或。需要说明两者的区别,避免用户错误使用导致逻辑错误。 总结回答的结构:首先明确回答C语言是否有OR判断符,指出标准用法是||,然后解释可能存在的扩展情况,接着对比按位或和逻辑或的区别,最后给出示例代码说明用法。确保用户理解正确使用方式,避免常见错误。</think>C语言中没有直接使用英文单词`or`作为逻辑“或”判断符,但可以通过以下两种方式实现类似功能: --- ### 一、标准逻辑运算符:`||` C语言中**逻辑“或”**运算符是`||`(两个竖线),用于组合两个条件。 **语法示例**: ```c if (条件1 || 条件2) { // 当条件1 或 条件2 为真时执行 } ``` **具体行为**: 1. 若`条件1`为真,直接判定整个表达式为真,不再检查`条件2`(短路求值)。 2. 若`条件1`为假,则继续检查`条件2`。 --- ### 二、扩展用法:`or`关键字(非标准) 某些编译器(如GCC)支持通过**包含头文件**使用`or`作为`||`的别名: ```c #include <iso646.h> // 需添加此头文件 if (条件1 or 条件2) { // 功能与 || 相同 } ``` ⚠️ **注意**: 1. 这是C99标准引入的宏定义,非所有环境默认支持。 2. 依赖编译器扩展,**可移植性差**,不推荐日常使用。 --- ### 三、对比:按位或运算符`|` 需注意区分逻辑或`||`和按位或`|`: - **逻辑或`||`**:用于布尔条件判断(真/假)。 - **按位或`|`**:对整数的二进制位逐位操作,例如: ```c int a = 5; // 二进制 0101 int b = 3; // 二进制 0011 int c = a | b; // 结果 0111(十进制7) ``` --- ### 四、示例代码 ```c #include <stdio.h> int main() { int x = 10; // 逻辑或 || if (x < 0 || x > 100) { printf("x超出合理范围\n"); } // 按位或 | int mask = 0x01; if (x & mask) { printf("x的最低位是1\n"); } return 0; } ``` --- ### 总结 - ✅ 标准写法:始终使用`||`实现逻辑“或”。 - ❌ 避免使用`or`:除非明确需要兼容特定环境且添加了头文件。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值