Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

std::tolower — lowercase conversion

std::tolower from <cctype> converts one character to lowercase according to the current C locale. For char data, convert to unsigned char first and call the function only if the resulting value fits in int.

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

Syntax

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

std::tolower from <cctype> takes an int and returns the lowercase equivalent as an int, or the unchanged value if conversion is unavailable. It does not modify its argument; store the result.

The argument must equal EOF or have a value representable as unsigned char. Other values cause undefined behavior. Plain char can be negative, so first convert c with static_cast<unsigned char>(c). The call std::tolower(static_cast<unsigned char>(c)) is valid for every char value if UCHAR_MAX <= INT_MAX (both constants are available in <climits>). Otherwise, check u <= INT_MAX for the resulting unsigned char value u before calling; if the check fails, handle that value without calling the function. Converting an out-of-range value to the int parameter can produce a negative value: if it is not EOF, the behavior is undefined; if it is EOF, the function does not treat it as a character. Check an int stream value against EOF before converting it to unsigned char.

The result depends on the current C locale. This is not Unicode case conversion or UTF-8 decoding. The std::tolower(c, loc) overload from <locale> is separate: it uses std::ctype for the supplied locale object.

The example processes a string character by character and stores each result as char. It does not check the range: it assumes the values obtained by converting to unsigned char fit in int. It leaves the initial C locale unchanged. Do not change global settings through std::setlocale concurrently with calls to this function without synchronization.

Examples

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

int main() {
    std::string text = "Hello, CPP23!";
    for (char& c : text) {
        c = static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
    }
    std::cout << text << '\n';
}

Result

hello, cpp23!

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