Skip to content

Instantly share code, notes, and snippets.

@dbr
Created November 19, 2012 10:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbr/4110098 to your computer and use it in GitHub Desktop.
Save dbr/4110098 to your computer and use it in GitHub Desktop.
Impact of locale on sscanf
/*
$ gcc locale_test.c
$ ./a.out
Got floats: 0.10 0.20 0.30
$ LANG="de_DE" ./a.out
Got floats: 0,00 0,00 0,00
*/
#include <stdio.h>
#include <locale.h>
int main(){
// Respect LANG/LC_ALL/LC_NUMERIC env-var
setlocale (LC_NUMERIC, "");
char in[] = "0.1 0.2 0.3";
float a, b, c;
if(sscanf(in, "%f %f %f", &a, &b, &c)){
printf("Got floats: %.02f %.02f %.02f\n", a, b, c);
}
else
{
printf("sscanf failed\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment