Library contents

C toupper

Normalize ASCII identifier bytes using the C character-conversion function in the active locale.

A simple example

toupper

  1. Start with "ab"
  2. Do this Convert to uppercase
  3. Result "AB"
Both ASCII letters acquire their uppercase forms.

Syntax

C syntax
toupper
Run these examples in C

Save one example at a time as lesson.c. Use C17 and a Unix-style shell.

cc -std=c17 -Wall -Wextra lesson.c -lm -o lesson
./lesson

Normalize a campus code

A school imports a short campus code from a form with inconsistent capitalization. Produce the standard spelling before joining the form record to the campus directory.

From the programchar code[] = "bio"
ResultBIO
lesson.c
#include <ctype.h>
#include <stdio.h>

int main(void) {
    char code[] = "bio";
    for (size_t i = 0; code[i]; ++i)
        code[i] = (char)toupper((unsigned char)code[i]);
    puts(code);
    return 0;
}
Output
BIO

The input contains ASCII. Converting each byte to unsigned char makes it valid for the character-classification API.

Advertisements

Normalize an airport tag

Two terminals can enter the same short label with different capitalization or padding. Normalize the incoming spelling before lookup so the stored identifiers can follow a consistent convention.

From the programchar code[] = "maa-2"
ResultMAA-2
lesson.c
#include <ctype.h>
#include <stdio.h>

int main(void) {
    char code[] = "maa-2";
    for (size_t i = 0; code[i]; ++i)
        code[i] = (char)toupper((unsigned char)code[i]);
    puts(code);
    return 0;
}
Output
MAA-2

The input contains ASCII. Converting each byte to unsigned char makes it valid for the character-classification API.

Print a workshop badge

A student types a lower-case room assignment. Convert the letter part to uppercase for the printed label. Digits and the separator retain their spelling.

Givenlab-8
Worked resultLAB-8

What to watch for

For UTF-8 human names choose a Unicode-aware casing library. These examples run with the default C locale.

Documentation for toupper