FREE Subscription to Dr. Dobb’s Digest: Same Great Content, New Digital Edition
Site Archive (Complete)
Architecture & Design
Email
Print
Reprint

add to:
Del.icio.us
Digg
Google
Furl
Slashdot
Y! MyWeb
Blink
May 01, 2002

The New C:

(Page 2 of 3)
May 2002/The New C/Listing 2

Listing 2: Test program for <stdbool.h>

/* Test program for <stdbool.h>.  May be compiled by C90, C99, or C++.
 */

#include <stdio.h>
#include "stdbool.h"

struct S {
  bool b : 1;
};

int main()
{
    bool b;
    struct S s;

    if (true != 1)
        printf("warning: true does not have integer value 1\n");

    if (false != 0)
        printf("warning: false does not have integer value 0\n");

    b = 2;
    if (b != 1)
        printf("warning: conversion to bool does not compare "
               "against 0\n");

    b = -1;
    if (b < 0L)
        printf("warning: bool is not unsigned\n");

    s.b = true;
    if ((int) s.b != true)
        printf("warning: bool bitfield does not cleanly hold true\n");

    /* Some compilers mistakenly issue an error for the next line */
#if !true
    printf("warning: true did not work in #if\n");
#endif

    /* Some compilers mistakenly issue an error for the next line */
#if false
    printf("warning: false did not work in #if\n");
#endif

#ifdef __bool_true_false_are_defined
    printf("note: __bool_true_false_are_defined is defined "
      "with value %d\n", __bool_true_false_are_defined);
#else
    printf("note: __bool_true_false_are_defined is not defined\n");
#endif

    printf("note: sizeof(bool) is %u bytes\n",
      (unsigned int) sizeof(bool));

    return 0;
}
— End of Listing —
Previous Page | 1 | 2 | 3 Next Page
RELATED ARTICLES
No Related Articles
TOP 5 ARTICLES
No Top Articles.



MICROSITES
FEATURED TOPIC

ADDITIONAL TOPICS

INFO-LINK