Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Literal "or" in c++ program? [duplicate]

Tags:

c++

c

keyword

I'm translating a C++ function I wrote some time ago into python when I noticed that my C++ code contains the following lines:

  if(MIsScaledOut()) {
    if(DataType()==UnknownDataType or DataType()==h)
      Descriptor = Descriptor + DataTypeString() + "OverM";

There's an or in there! This was probably because I previously translated from python, and forgot to switch to ||.

This code compiles in various OSes, with various compilers, and I've never seen a problem with it. Is this standard, or have I just gotten lucky so far, and this is something I should worry about?

like image 451
Mike Avatar asked Nov 05 '14 21:11

Mike


People also ask

Is string a literal?

A "string literal" is a sequence of characters from the source character set enclosed in double quotation marks (" "). String literals are used to represent a sequence of characters which, taken together, form a null-terminated string.

Can we change string literal in C?

The behavior is undefined if a program attempts to modify any portion of a string literal. Modifying a string literal frequently results in an access violation because string literals are typically stored in read-only memory.

Is a character literal?

A character literal contains a sequence of characters or escape sequences enclosed in single quotation mark symbols, for example 'c' . A character literal may be prefixed with the letter L, for example L'c' . A character literal without the L prefix is an ordinary character literal or a narrow character literal.


1 Answers

After remembering the right word to google, I now see that it is listed as a C++ keyword, along with various similar keywords like and that I'd never seen (noticed?) before in C++. The reason these exist is because there are encodings that don't have some of the required punctuation characters used by the traditional operator spellings: {, }, [, ], #, \, ^, |, ~.

As @mafso points out, the alternative "spelled out" versions can be used in C by including the <iso646.h> header, which defines them as macros.

The question of which this has been marked duplicate also points out the existence of digraphs and trigraphs, which can be used to substitute for the missing characters. (That question also says "everybody knows about" them. Obviously, I did not...)

like image 186
Mike Avatar answered Oct 22 '22 00:10

Mike