bhylu.com

Advertisements

C Characters, Booleans and Constants

Use one character and a boolean rule to label a field record without relying on magic numbers.

How it works

A character constant such as A in single quotes has type int in C and can initialize a char when representable. A string literal uses double quotes and contains a terminating zero byte. In C17, stdbool.h supplies bool, true and false as convenient spellings built on _Bool. A boolean conversion treats zero as false and nonzero as true. const prevents modification through that name, but a const int is not a general integer constant expression in C17 as it would be in some C++ contexts.

Run it locally

Use a hosted C17 compiler in a separate lab folder. The command assumes a driver named cc; substitute gcc, clang or zig cc as appropriate. C does not currently run in Bhylu’s browser playground. Apache, PHP and MySQL are not needed for these C programs.

cc -std=c17 -Wall -Wextra -Wpedantic -Werror characters-bool.c -o lesson

On Windows, use -o lesson.exe and run .\lesson.exe in PowerShell. On Unix-like systems run ./lesson. Math-library linking differs by toolchain; GCC on many Unix systems needs -lm after the source.

Download the complete C source. File-writing labs must run in an empty private directory, away from existing reports.

Example

C17 · characters-bool.c

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

int main(void) {
    const int minimum_samples = 3;
    int samples = 4;
    char sector = 'N';
    bool ready = samples >= minimum_samples;
    printf("Sector %c ready: %d\n", sector, ready);
    return 0;
}

Expected result

Sector N ready: 1

The relational expression produces a truth value and ready stores it. Default argument promotions make bool an int argument for printf here, so %d is appropriate. The letter is a compact code, not a complete internationalised site name. Later string lessons work with byte sequences and explain why one char cannot hold every visible character.

For language and library rules, consult the WG14 C working group and its public C11 committee draft, which describes the core facilities used here; C17 incorporates corrections. Check your compiler and runtime documentation for implementation details.

Advertisements