Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gtk_init changes sprintf functionality

Tags:

c

gtk3

I'm starting to work with Linux and GTK and ran in a strange problem. I am using sprintf() in my code to parse a float into a char array.

When parsing the number 1 into the string this resulted in "1.000000" but strangely after calling gtk_init() when I then do the sprintf it results in "1,000000". How does gtk_init() modify this behavior and how can I force the program to keep parsing it to "1.000000".

This is my small example program that reproduces the problem:

#include <gtk/gtk.h>

int main(int argc, char** argv)
{
    char cMessage[12];
    float fNumber = 1;
    sprintf(cMessage, "T:%f", fNumber);
    printf("%s\n", cMessage);

    gtk_init(&argc, &argv);

    sprintf(cMessage, "T:%f", fNumber);
    printf("%s\n", cMessage);

    return 0;
}

The output of this program is the following:

T:1.000000
T:1,000000
like image 671
Roel Balink Avatar asked Mar 05 '23 07:03

Roel Balink


1 Answers

This is related to your locale/language environment. Before calling gtk_init, you LOCALE variable must be set the the default value, C. gtk_init by default, sets the locale to whatever your desktop environment is set to.

To turn this behaviour off, you can use gtk_disable_setlocale.

like image 135
wazoox Avatar answered Mar 10 '23 09:03

wazoox