LoginSignup
14
5

More than 5 years have passed since last update.

bool型のtrue,falseはintです。

Posted at

今日は、新たな発見がありました。
stdbool.hの中で定義されているbool型用のtrue,falseについてです。stdbool.hには下記ようのに定義されています。

stdbool.h
#ifndef __cplusplus

#define bool    _Bool
#define true    1
#define false   0

#else /* __cplusplus */
以下省略

c言語で利用する場合は、__cplusplusが定義されていないので、true,falseはint型です。下のように書いても問題ないはず。

sample.c
#include <stdio.h>
#include <stdbool.h>

static int s_value = 0;
void func(bool state )
{
    s_value = state;
}

int main( int argc, char *argv[] )
{
    func(false);
    printf("value=%d\n", s_value);
    return 0;
}
14
5
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
5