bitwise OR in linux open flags

c, linux

Solution

How this is interpreted by the compiler

Not differently than any other bitwise OR operation. Consider the following `#define`s as found for example in `/usr/include/asm-generic/fcntl.h` (note that values are in octal):

#define O_RDONLY        00000000
#define O_CREAT         00000100
#define O_TRUNC         00001000

Then, in your example, the value passed to the function is `00000000 | 00000100 | 00001000` which is `00001100`. By evaluating the various bit positions, `open()` can reconstruct which of the flags had been set by the caller:

if (oflag & O_CREAT) {
   /* caller wants the file to be created */
}

if (oflag & O_TRUNC) {
   /* caller wants the file to be truncated */
}
...

Problem

In the linux `open` system call, what is the meaning of bitwise OR in flags. How this is interpreted by the compiler. Here's an example: ``` fd = open("myfile", O_RDONLY | O_CREAT | O_TRUNC, S_IRUSR); ``` Also, what does comma operator do in flags? Update: What is the effect of using other operators like if we do `&&` operator

Original source