Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

std::toupper — convert one character to uppercase

std::toupper from <cctype> returns a character's uppercase equivalent using the current C locale. Its argument must equal EOF or be representable as unsigned char; its return type is int.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

#include <cctype>
int std::toupper(int c);

std::toupper from <cctype> has the signature int std::toupper(int c). It returns the corresponding uppercase character using the current C locale, or the unchanged argument when no conversion is available. It does not modify its argument: assign or otherwise use the result.

The value passed to the int parameter must equal EOF or be representable as unsigned char. Any other value causes undefined behavior. Because plain char can be negative, use std::toupper(static_cast<unsigned char>(ch)) for a char variable provided that the value after the cast fits in int. If int cannot represent every unsigned char value, check that the cast value fits in int before calling the function. Otherwise, conversion to int can produce a negative value other than EOF, causing undefined behavior. If it produces EOF, the function receives EOF rather than the character value. Check a value returned as int by an input function for EOF before converting it to unsigned char.

The example processes a string one character at a time and explicitly converts the int result back to char. It assumes that every value obtained by casting to unsigned char fits in int. The expected output is HELLO, C++23! followed by a newline under the initial "C" locale. This is not a general Unicode case-conversion mechanism: processing UTF-8 bytes this way is insufficient.

Do not confuse this function with the std::toupper(c, loc) overload from <locale>, which uses an explicitly supplied locale object. The guidance above on casting to unsigned char applies to the one-argument function from <cctype>.

Examples

#include <cctype>
#include <iostream>
#include <string>

int main() {
    std::string text = "Hello, C++23!";
    for (char& ch : text) {
        ch = static_cast<char>(
            std::toupper(static_cast<unsigned char>(ch)));
    }
    std::cout << text << '\n';
}

Result

HELLO, C++23!

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources