1

I read that booleans in C first came with C99. Now I wonder why bool nevertheless gets colored by Xcode's syntax highlighting, or what you call it...
When one searches "dial" in the build settings in Xcode, the "Language Dialect" pops up, and there I "dial-in" the C89 standard, and still bool gets colored.

Why is that?

I also read this:
Using boolean values in C

and I see how they did it, but I also don't understand how Example 3 and 4 works...

Option 3
typedef int bool;
enum { false, true };

Option 4
typedef int bool;
#define true 1
#define false 0

Note: I don't understand how the typedef int bool; could in anyway be connected with the line enum { false, true };.

Why is xcode with C89 not ignoring the bool keyword?
How do the examples work?

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 1
    I think it's likely that Xcode's syntax highlighter is just not paying attention to the "language dialect" setting. Do your "option 3" and "option 4" _compile_ in C89 mode? They should, as long as you don't `#include `. – zwol Feb 27 '20 at 15:59
  • 2
    Xcode is an IDE and not a compiler. It has its own rules. – Eugene Sh. Feb 27 '20 at 15:59
  • Also, you should be aware that `bool`, `true`, and `false` are _not_ keywords even in the latest version of the C standard. C99 added a genuine boolean _type_ and the keyword `_Bool`, but programs that define `bool`, `true`, and `false` as they like are still valid in C99 and later (again, as long as they don't include `stdbool.h`). This is an intentional difference from C++. – zwol Feb 27 '20 at 16:00
  • hey! i didn't try Option 3 and 4 since i don't understand how the " typedef int bool; " could in anyway be connected with the line " enum { false, true }; " –  Feb 27 '20 at 16:01
  • @Nickname Does it help if I tell you that the constants defined by an `enum` declaration have type `int`? – zwol Feb 27 '20 at 16:02
  • i'm not concerned about the latest C version, only C89... –  Feb 27 '20 at 16:04
  • @zwol i know they are int's but still its confusing.. i mean why isn't there a bool instead of enum later on, when it got typedef-ined a line before?? i take Option 2 then –  Feb 27 '20 at 16:08
  • `typedef int bool; bool { true, false };` would not do what is needed... – zwol Feb 27 '20 at 16:10
  • `typedef enum bool_t` `{` `b_false = 0,` `b_true = 1` `} bool;` is what i use, and these flags -Wall `-pedantic -std=c89` as well, and still it gets _colored_ –  Feb 27 '20 at 17:42

1 Answers1

0

The syntax highlighting is an approximation used by XCode; it's not guaranteed to follow any standard. Not to say the syntax highlighting in XCode is not fairly advanced, but it would be very difficult to tell without compiling for a specific standard, what the symbols mean exactly, in real-time.

In the case of Option 3, it's performing an implicit conversion from an anonymous enum to an int. Since they are compatible types, it is "always a no-op and does not change the representation." The default enum values for { false, true } are { 0, 1 }, so this should result in the same assembler output. This answer has the order of preference when one doesn't know what to pick.

Neil
  • 1,767
  • 2
  • 16
  • 22