Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed bool type for C89 #597

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Fixed bool type for C89 #597

wants to merge 1 commit into from

Conversation

damon-kwok
Copy link

Before c99 doesn't have _Bool‘ and STDC_VERSION'. Compensate.

#ifndef __STDC_VERSION__
#define __STDC_VERSION__ 0
#endif
#if defined(_MSC_VER) || (__STDC_VERSION__ < 199901L)
/* `MSVC' and `Before c99' doesn't have <stdbool.h>. Compensate. */
typedef char bool;
#define true (bool) 1
#define false (bool) 0
#else
#include <stdbool.h>
#endif

@grynspan
Copy link
Contributor

grynspan commented May 6, 2022

What compilation environment are you using where you are constrained to C89? :)

Copy link
Contributor

@grynspan grynspan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MSVC++ definitely has stdbool.h, and defining bool in this way will conflict with the standard library's definition there.

If this is a real target you're trying to support, then presumably it's an old version of MSVC++? If so, you might just want to check for the existence of the header instead:

#if __has_include(<stdbool.h>)
#include <stdbool.h>
#else
typedef char bool;
#define true ((bool)1)
#define false ((bool)0)
#endif

Note how I've wrapped the (bool) 1 in parentheses: this is necessary in a macro definition in certain lexical contexts where (bool) 1 could instead be parsed as e.g. f(bool) 1 and which would produce an ill-formed program or unexpected behaviour.

Alternatively, if for some reason you can't use __has_include, you can check for an old version of C like so:

#if !defined(__cplusplus) && (!defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L)

But again, if you're trying to port your code to some old version of MSVC++, a bit of explanation might be in order. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants