Skip to Content
Course content

127: Alternative Tokens with iso646.h

Click on the "Edit" button in the top corner of the screen to edit your slide content.

Why does iso646.h even exist?

If you've spent any time with C, you're used to seeing &&, ||, and ! for your logical operations. But back in the day, not every keyboard in the world had those specific symbols. Some international layouts made it a nightmare to type an ampersand or a pipe character. To make C more portable and accessible, the iso646.h header was introduced.

Essentially, it provides a set of macros that map English words to those symbols. It's not a different language or a new set of operators; it's just a convenience layer that lets you type and instead of &&. I'll be honest: you'll rarely see this in modern open-source projects, but you might run into it if you're digging through legacy embedded systems code from the 80s or 90s.

How do I actually use these tokens in a program?

It's straightforward. You include the header, and then you can use the word-based tokens exactly where you would use the symbols. Here is a quick example of a permission check for a fictional game system:

#include <stdio.h>
#include <iso646.h>

int main() {
    int has_key = 1;
    int is_admin = 0;
    int is_banned = 0;

    // Using 'and', 'or', and 'not' instead of &&, ||, and !
    if ((has_key or is_admin) and not is_banned) {
        printf("Access granted to the secret vault.\n");
    } else {
        printf("Access denied.\n");
    }

    return 0;
}

The compiler treats and exactly the same as &&. There is zero performance penalty here because the preprocessor just swaps the text before the actual compilation happens.

Is this something I should actually use in my own projects?

In my professional opinion? No. Unless you have a very specific reason—like a weird hardware constraint or a personal preference for readability—stick to the symbols.

C developers are trained to scan for && and ||. When you use and and or, you're actually making the code slightly harder to read for the average C programmer because it looks like you're writing Python or C++. If you're working on a team, your lead will probably ask you to change it back during a code review. It's a neat piece of C history to know about, but it's not a tool I'd recommend for a modern codebase.




📋 Practical Task

Refactoring the Security Gate Validator

I've provided a snippet of a security gate validator below that uses standard C logical operators. Your task is to refactor this code to use the iso646.h alternative tokens. Replace all instances of &&, ||, and ! with their word-based equivalents.

#include <stdio.h>

int main() {
    int sensor_tripped = 0;
    int override_active = 0;
    int system_failure = 0;

    // Refactor this if-statement using iso646.h
    if (!system_failure && (sensor_tripped || override_active)) {
        printf("Alarm sounding!\n");
    } else {
        printf("System secure.\n");
    }

    return 0;
}

Requirements:

  • Include the necessary header.
  • Ensure the logic remains identical to the original code.
  • The program should compile without warnings.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.