C++ clocale header file

Hello guys, in this post, we will discuss the clocale header file in C++. This header file is part of the localization library that is part of the C++ standard library. There are some functions defined in this header and they are used to perform some internationalization tasks such as monetary formatting, date-time formatting, etc. We will see more on this further in this tutorial.

clocale header file in C++

We need to include <clocale> to use functions and constants defined in this header file. These functions have been explained here.

setlocale() function

This function is defined in clocale header and used to get and set the C locale. There are some parameters defined as given below.

LC_ALL : It selects everything.

LC_COLLATE : It selects the collation category of the C locale.

LC_CTYPE : It selects the character classification category of C locale.

LC_MONETARY : It selects the monetary formatting category of the C locale.

LC_NUMERIC : It selects the numeric formatting category.

LC_TIME : It selects the time formatting category.

The syntax for this function is as follows:

int setlocale(int category, const char * locale);

The function returns a pointer to a string to the C locale if it identifies. Otherwise, it returns a null pointer.

localeconv() function()

Another function defined in this header is localeconv(). The syntax for this function is as follows:

struct lconv* localeconv();

As you can see from the given syntax, this function takes no parameter. The return value for this function is a pointer to a static object that contains rules for time and monetary formatting of the current C locale.

Example Program

Have a look at this code and try to understand what each statement of code is doing.

#include <iostream>
#include <clocale>

using namespace std;

int main()
{
  setlocale(LC_MONETARY, "en_US.utf8");
  struct lconv* locale_object;
  locale_object= localeconv();
  cout << "The currency of US is " << locale_object->currency_symbol << endl;
  
  return 0;
}

Output:

The currency of US is $

Thank you.

Also read: Ratio manipulation in C++ with examples

Leave a Reply