Czy bool to rodzimy Typ C?

Zauważyłem, że kod jądra Linuksa używa boola, ale myślałem, że bool jest typem C++. Czy bool jest standardowym rozszerzeniem C (np.

Author: Matt Joiner, 2009-10-22

10 answers

bool istnieje w obecnym C-C99, ale nie w C89 / 90.

W C99 natywny typ jest tak naprawdę nazywany _Bool, podczas gdy bool jest standardowym makrem biblioteki zdefiniowanym w stdbool.h (który prawdopodobnie rozwiązuje się na _Bool). Obiekty typu _Bool posiadają 0 LUB 1, natomiast true i false są również makrami z stdbool.h.

 310
Author: AnT,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-01-17 23:43:18

C99 dodał Wbudowany typ danych _Bool (zobacz Wikipedia Po szczegóły), a jeśli #include <stdbool.h>, Dostarcza bool jako makro do _Bool.

Pytałeś w szczególności o jądro Linuksa. Zakłada obecność _Bool i dostarcza bool typedef w include/linux/types.h .

 114
Author: Josh Kelley,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-10-23 12:19:01

Nie, Nie Ma bool W ISO C90.

Oto lista słów kluczowych w standardzie C (nie C99):

  • auto
  • break
  • case
  • char
  • const
  • continue
  • default
  • do
  • double
  • else
  • enum
  • extern
  • float
  • for
  • goto
  • if
  • int
  • long
  • register
  • return
  • short
  • signed
  • static
  • struct
  • switch
  • typedef
  • union
  • unsigned
  • void
  • volatile
  • while

Oto artykuł omawiający niektóre inne różnice z C używanym w jądrze i standardzie: http://www.ibm.com/developerworks/linux/library/l-gcc-hacks/index.html

 31
Author: BobbyShaftoe,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-11-27 15:24:32

C99 ma go w stdbool.h , ale w C90 musi być zdefiniowany jako typedef lub enum:

typedef int bool;
#define TRUE  1
#define FALSE 0

bool f = FALSE;
if (f) { ... }

Alternatywnie:

typedef enum { FALSE, TRUE } boolean;

boolean b = FALSE;
if (b) { ... }
 28
Author: Rob,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-04-26 15:05:59
/* Many years ago, when the earth was still cooling, we used this: */

typedef enum
{
    false = ( 1 == 0 ),
    true = ( ! false )
} bool;

/* It has always worked for me. */
 15
Author: user2705144,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-10-26 13:38:28

_Bool jest słowem kluczowym w C99: określa typ, podobnie jak int lub double.

6.5.2

2 obiekt zadeklarowany jako typ _bool jest wystarczająco duży, aby zapisać wartości 0 i 1.

 11
Author: pmg,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-10-22 16:19:22

C99 definiuje bool, true i false w stdbool.h.

 5
Author: starblue,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-04-03 09:53:12

Stdbool.h został wprowadzony w c99

 2
Author: Nick Van Brunt,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-10-22 16:16:29

Nie ma czegoś takiego, pewnie tylko makro dla int

 0
Author: sindre j,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-10-22 16:13:50

stdbool.h definiuje makra true I false, ale pamiętaj, że definiuje się je jako 1 i 0.

Dlatego sizeof(true) jest 4.

 0
Author: Neha Gangwar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-11-26 05:58:09