# C++ and C locale are not equal after creation of qSlicerApplication

**URL:** <https://discourse.slicer.org/t/c-and-c-locale-are-not-equal-after-creation-of-qslicerapplication/33718>\
**Category:** Development\
**Created:** [January 10, 2024, 1:24pm UTC](https://discourse.slicer.org/t/c-and-c-locale-are-not-equal-after-creation-of-qslicerapplication/33718 "2024-01-10T13:24:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![aymeric.chataigner](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/aymeric.chataigner/32/4977_2.png) [@aymeric.chataigner](https://discourse.slicer.org/u/aymeric.chataigner)\
**Post date:** [January 10, 2024, 1:24pm UTC](https://discourse.slicer.org/t/c-and-c-locale-are-not-equal-after-creation-of-qslicerapplication/33718/1 "2024-01-10T13:24:34Z")

</div>

Hi,

I added some code in Main.cxx to check the values of C and C++ locale settings:

```c++
int SlicerAppMain(int argc, char* argv[])
{
  typedef qSlicerAppMainWindow SlicerMainWindowType;
  typedef qSlicerStyle SlicerAppStyle;

  std::cout << "C locale: " << std::setlocale(LC_ALL, nullptr) << std::endl;
  std::cout << "Cpp locale: " << std::locale().name() << std::endl;

  qSlicerApplicationHelper::preInitializeApplication(argv[0], new SlicerAppStyle);

  std::cout << "C locale: " << std::setlocale(LC_ALL, nullptr) << std::endl;
  std::cout << "Cpp locale: " << std::locale().name() << std::endl;

  qSlicerApplication app(argc, argv);
  if (app.returnCode() != -1)
    {
    return app.returnCode();
    }

  std::cout << "C locale: " << std::setlocale(LC_ALL, nullptr) << std::endl;
  std::cout << "Cpp locale: " << std::locale().name() << std::endl;

```

On my PC it prints:  
C locale: C  
Cpp locale: C  
C locale: C  
Cpp locale: C  
C locale: LC\_CTYPE=en\_US.UTF-8;LC\_NUMERIC=C;LC\_TIME=C;LC\_COLLATE=C;LC\_MONETARY=C;LC\_MESSAGES=C;LC\_PAPER=C;LC\_NAME=C;LC\_ADDRESS=C;LC\_TELEPHONE=C;LC\_MEASUREMENT=C;LC\_IDENTIFICATION=C  
Cpp locale: C

So qSlicerApplication construction set LC\_CTYPe to en\_US.UTF-8  
It is done during Python initialization line 426 in Python-3.9.10/Python/pylifecycle.c

This difference between C and C++ locale can lead to issues if C++ locale settings are saved/restored using std::global as it is done in vtkDataReader::OpenVTKFile and vtkDataReader::CloseVTKFile:

Before open & save calls:  
C locale: LC\_CTYPE=en\_US.UTF-8;LC\_NUMERIC=C;LC\_TIME=C;LC\_COLLATE=C;LC\_MONETARY=C;LC\_MESSAGES=C;LC\_PAPER=C;LC\_NAME=C;LC\_ADDRESS=C;LC\_TELEPHONE=C;LC\_MEASUREMENT=C;LC\_IDENTIFICATION=C  
Cpp locale: C

After open & save calls:  
C locale: C  
Cpp locale: C

Because std::global set C++ locale AND C locale.

So I have several questions:

- Is it ok to let python initialization set LC\_CTYPE?
  - If yes: should we synchronize C++ locale using this line after qSlicerApplication creation: std::locale::global(std::locale(std::setlocale(LC\_ALL, nullptr))); (this is what we did in our custom app)
  - If no: so C and C++ locale should always be C, is it not a problem if users type special characters as input ?

Regards

---

<div class="post-metadata">

**Author:** ![aymeric.chataigner](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/aymeric.chataigner/32/4977_2.png) [@aymeric.chataigner](https://discourse.slicer.org/u/aymeric.chataigner)\
**Post date:** [January 15, 2024, 1:38pm UTC](https://discourse.slicer.org/t/c-and-c-locale-are-not-equal-after-creation-of-qslicerapplication/33718/2 "2024-01-15T13:38:10Z")

</div>

@lassoan do you have any comment ?
