C++ ios::Uppercase() Function



The C++ std::ios::uppercase() function is used to modify the behaviour of stream output operations. When it is invoked, it makes the characters to be displayed in uppercase. This manipulator is used with the output streams to ensure that the numeric values are printed in uppercase hexadecimal format, and for other characters where uppercase representation is desired.

Syntax

Following is the syntax for std::ios::uppercase() function.

ios_base& uppercase (ios_base& str);

Parameters

  • str − It indicates the stream object whose format flag is affected.

Return Value

This function returns the Argument str.

Exceptions

If an exception is thrown, str is in a valid state.

Data races

It modifies str. Concurrent access to the same stream object may cause data races.

Example

In the following example, we are going to consider the basic usage of the uppercase() function with hexadecimal format.

#include 
#include 
int main()
{
    int x = 1234;
    std::cout << "Result :  " << std::hex << std::uppercase << x << std::endl;
    return 0;
}

Output

Output of the above code is as follows −

Result :  4D2

Example

Consider the following example, where we are going to use the uppercase() function along with the showbase().

#include 
#include 
int main()
{
    int x = 1234;
    std::cout << std::hex << std::uppercase << std::showbase;
    std::cout << "Result : " << x << std::endl;
    return 0;
}

Output

Following is the output of the above code −

Result : 0X4D2

Example

Let's look at the following example, where we are going to use the uppercase() in a function.

#include 
#include 
void a(int num)
{
    std::cout << std::hex << std::uppercase << num << std::endl;
}
int main()
{
    a(2234);
    a(2232);
    return 0;
}

Output

If we run the above code it will generate the following output −

8BA
8B8
ios.htm
Advertisements