표준 라이브러리 헤더 <locale.h>

표준 라이브러리 헤더 <locale.h>

나라와 언어에 따라 숫자 표기(소수점이 .인지 ,인지), 통화 기호, 날짜 형식이 모두 달라져요. C 프로그램이 이런 로케일(locale) 차이를 이해하려면, 현재 로케일이 무엇인지 설정하고 물어봐야 해요. <locale.h>는 로컬라이제이션 라이브러리의 일부로, 로케일을 다루는 타입·상수·함수를 제공해요.

출처: cppreference

본문

타입(Types)

타입 설명
lconv localeconv가 돌려주는 서식 세부 정보를 담는 구조체

상수(Constants)

상수 설명
NULL 구현 정의된 널 포인터 상수
LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC, LC_TIME setlocale에서 쓰는 로케일 카테고리

함수(Functions)

함수 설명
setlocale 현재 C 로케일을 가져오거나 설정
localeconv 현재 로케일의 숫자·통화 서식 세부 정보를 조회

시그니처(Synopsis)

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

// In the "C" locale, the members shall have the values specified in the comments:
struct lconv
{
 char* decimal_point; // "."
 char* thousands_sep; // ""
 char* grouping; // ""
 char* mon_decimal_point; // ""
 char* mon_thousands_sep; // ""
 char* mon_grouping; // ""
 char* positive_sign; // ""
 char* negative_sign; // ""
 char* currency_symbol; // ""
 char frac_digits; // CHAR_MAX
 char p_cs_precedes; // CHAR_MAX
 char n_cs_precedes; // CHAR_MAX
 char p_sep_by_space; // CHAR_MAX
 char n_sep_by_space; // CHAR_MAX
 char p_sign_posn; // CHAR_MAX
 char n_sign_posn; // CHAR_MAX
 char* int_curr_symbol; // ""
 char int_frac_digits; // CHAR_MAX
 char int_p_cs_precedes; // CHAR_MAX
 char int_n_cs_precedes; // CHAR_MAX
 char int_p_sep_by_space; // CHAR_MAX
 char int_n_sep_by_space; // CHAR_MAX
 char int_p_sign_posn; // CHAR_MAX
 char int_n_sign_posn; // CHAR_MAX
};

#define NULL /* see description */
#define LC_ALL /* see description */
#define LC_COLLATE /* see description */
#define LC_CTYPE /* see description */
#define LC_MONETARY /* see description */
#define LC_NUMERIC /* see description */
#define LC_TIME /* see description */

lconv 구조체 멤버들은 "C" 로케일 기준의 기본값이 주석에 적혀 있어요. 예를 들어 decimal_point., thousands_sep는 빈 문자열이죠. 다른 로케일로 바꾸면 이 값들이 그 문화권의 관례에 맞게 바뀌어요.

주의할 점(Notes)

NULL<locale.h> 외에도 <string.h>, <time.h>, <stddef.h>, <stdio.h>, <wchar.h>에서도 정의돼요. 여러 헤더를 포함해도 중복 정의 충돌 없이 쓸 수 있도록 설계되어 있어요.

더 알아보기

  • 문자 분류가 로케일에 어떻게 영향을 받는지는 <ctype.h>를 봐요.
  • 와이드 문자 로케일 처리는 <wchar.h><wctype.h>에서 확인해요.