2

Answers to this question on SO specify that _Bool was introduced in C99 (specifically BobbyShaftoe's answer).

But the following code compiles perfectly with gcc -std=c90 -pedantic test.c and gcc -std=c89 -pedantic test.c both, producing output 1 (my gcc version is 4.7.1)

#include<stdio.h>
#include<stdlib.h>

int main(){

    _Bool a = 0;
    printf("%u\n",sizeof(a) );
    return 0;
}

So in which version was _Bool introduced ?

Community
  • 1
  • 1
Nishant
  • 2,571
  • 1
  • 17
  • 29
  • 2
    In C99 as you mention in your question. You are confusing an individual implementation with the specification to which all implementations *should* adhere. – 0xC0000022L Oct 14 '14 at 10:22
  • 8
    @SantoshA Why do you think it's not related to programming? – Yu Hao Oct 14 '14 at 10:23
  • 3
    strictly speaking, identifiers starting with an _ are reserved for the implementation - a c89 compliant compiler might very well give you e.g. a macro or typedef called a _Bool. – nos Oct 14 '14 at 10:33
  • @0xC0000022L by `implementation` do you mean the implementation of `gcc` compiler ? – Nishant Oct 14 '14 at 10:34
  • 2
    `-std=c90 -pedantic` doesn't disable/catch *all* the extensions of gcc. GCC doc hints *Some features that are part of the C99 standard are accepted as extensions in C90 mode, and some features that are part of the C11 standard are accepted as extensions in C90 and C99 modes.* Regardless, you can be sure that bool was introduced only in ISO C99 and not before. – P.P Oct 14 '14 at 11:05
  • @Nishant: indeed. Actually that's what you imply with your question, since you give GCC and GCC only as an example for an implementation. Basically the flaw of your question is the assumption that GCC behaves perfectly true to the spec when given the flags you mention. Which, as Blue Moon pointed out, is not a good assumption. – 0xC0000022L Oct 14 '14 at 11:11

1 Answers1

2

As pointed by @0xC0000022L you need to differentiate between standard and actual implementation. Interestingly there is however some inconsistency how GCC (and Clang as well) treats _Bool. C99 introduced also _Complex type, for which there is diagnostic message:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    printf("%lu\n", (unsigned long) (sizeof(_Complex)));
    return 0;
}

results into (GCC 4.4.7):

$ gcc -ansi -pedantic-errors check.c 
check.c: In function ‘main’:
check.c:5: error: ISO C90 does not support complex types
check.c:5: error: ISO C does not support plain ‘complex’ meaning ‘double complex’

You may examine source code, and then you will find that indeed _Complex type is checked against standard version, while _Bool is not:

gcc/po/gcc.pot:19940

#: c-decl.c:7366
#, gcc-internal-format
msgid "ISO C90 does not support complex types"
msgstr ""

gcc/c-decl.c:7380

case RID_COMPLEX:
  dupe = specs->complex_p;
  if (!flag_isoc99 && !in_system_header)
pedwarn (input_location, OPT_pedantic, "ISO C90 does not support complex types");

I wouldn't call it a bug, but rather a decision that was made by developers.

Grzegorz Szpetkowski
  • 36,988
  • 6
  • 90
  • 137
  • 1
    it is worth noting that your error is a warning in GCC 4.7.1 resulted due to the `-pedantic` flag, otherwise the codes compiles successfully with output `16`. – Nishant Oct 14 '14 at 11:27
  • 1
    This is simply, as the message indicates, because gcc still supposes C90 per default and not C99. – Jens Gustedt Oct 14 '14 at 11:32