Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

or is not valid C++ : why does this code compile?

Tags:

Here is a very simple C++ application I made with QtCreator :

int main(int argc, char *argv[]) {     int a = 1;     int b = 2;      if (a < 1 or b > 3)     {        return 1;     }     return 0; } 

To me, this is not valid C++, as the keyword or is not a reserved keyword.

But if I compile and run it, it works fine without any warnings ! The exit code is 0 and if I change b = 4, the exit code is 1 !

I'm not including anything to make sure there is no hidden define.

This is really strange to me. Is this something Qt is defining ? I didn't find anything in the documentation regarding that.

like image 473
Jérôme Avatar asked Sep 16 '09 14:09

Jérôme


People also ask

Is C valid C++ code?

In the strict mathematical sense, C isn't a subset of C++. There are programs that are valid C but not valid C++ and even a few ways of writing code that has a different meaning in C and C++. However, C++ supports every programming technique supported by C95 (C90 plus an Amendment) and earlier.

Does C need to be compiled?

C is a mid-level language and it needs a compiler to convert it into an executable code so that the program can be run on our machine.

Can C code can be complied in CPP compiler?

If the C++ compiler provides its own versions of the C headers, the versions of those headers used by the C compiler must be compatible. Oracle Developer Studio C and C++ compilers use compatible headers, and use the same C runtime library. They are fully compatible.

Does C++ have to be compiled?

Each C++ source file needs to be compiled into an object file. The object files resulting from the compilation of multiple source files are then linked into an executable, a shared library, or a static library (the last of these being just an archive of object files).


1 Answers

According to Wikipedia:

C++ defines keywords to act as aliases for a number of symbols that function as operators: and (&&), bitand (&), and_eq (&=), or (||), bitor (|), or_eq (|=), xor (^), xor_eq (^=), not (!), not_eq (!=), compl (~).

As MadKeithV points out, these replacements came from C's iso646.h, and were included in ISO C++ as operator keywords. The Wikipedia article for iso646.h says that the reason for these keywords was indeed for international and other non-QWERTY keyboards that might not have had easy access to the symbols.

like image 104
Thomas Owens Avatar answered Dec 12 '22 08:12

Thomas Owens