LoginSignup
0
0

7.11 Localization <locale.h>, CN3054:2022 (19) p229.c

Last updated at Posted at 2022-11-21

7.9 Alternative spellings
7.10 Characteristics of integer types
はソースコードなし。

はじめに(Introduction)

N3054 Working Draft, Standard for Programming Language C

C++ nは、ISO/IEC JTC1 SC22 WG14の作業原案(Working Draft)です。
公式のISO/IEC 9899原本ではありません。

ISO/IEC JTC1 SC22 のWG14を含むいくつかのWGでは、可能な限り作業文書を公開し、幅広い意見を求めています。

ISO/IEC JTC1 SC7からISO/IEC JTC1 SC22リエゾンとして、2000年頃、C/C++の品質向上に貢献しようとした活動をしていたことがあります。その頃は、まだISO/IEC TS 17961の原案が出る前です。Cの精神が優勢で、セキュリティ対策は補助的な位置付けでした。ISO/IEC TS 17961の制定と、C/C++のライブラリ類の見直しと、C++の進化はどんどん進んでいきます。 

進化の具合が、どちらに行こうとしているかは、コンパイルて実行させてみないとわかりません。C/C++の規格案の電子ファイルは、そのままコンパイルできる形式であるとよいと主張してきました。MISRA-C/C++, CERTC/C++でも同様です。MISRA-C/C++は、Example Suiteという形で、コード断片をコンパイルできる形で提供するようになりました。

一連の記事はコード断片をコンパイルできる形にする方法を検討してコンパイル、リンク、実行して、規格案の原文と処理系(gcc, clang)との違いを確認し、技術内容を検討し、ISO/IEC JTC1 SC22 WG21にフィードバックするために用います。
また、CERT C/C++, MISRA C/C++等のコーディング標準のコード断片をコンパイルする際の参考にさせていただこうと考えています。CERT C++, MISRA C++が標準化の動きとの時間的なずれがあれば確認できれば幸いです。また、boostライブラリとの関連、Linux OS, 箱庭プロジェクト、g++(GCC), clang++(LLVM)との関係も調査中です。
何か、抜け漏れ、耳より情報がありましたらおしらせくださると幸いです。

最新規格はCN3096

<この項は書きかけです。順次追記します。>

背景(back ground)

C/C++でコンパイルエラーが出ると、途方にくれることがしばしばあります。
何回かに1回は、該当するエラーが検索できます。
ただ、条件が違っていて、そこでの修正方法では目的を達成しないこともしばしばです。いろいろな条件のコンパイルエラーとその対応方法について、広く記録することによって、いつか同じエラーに遭遇した時にやくに立つことを目指しています。

過去に何度か、自分のネットでの記録に助けられたことがあります。

また
https://researchmap.jp/joub9b3my-1797580/#_1797580
に記載したサイトのお世話になっています。

作業方針(sequence)

clangでは--std=c11, -std=C17 -std=c2xの3種類
gccでは-std=c11, -std=C17 -std=c2xの3種類
でコンパイルし、

1)コンパイルエラーを収集する。
2)コンパイルエラーをなくす方法を検討する。
コンパイルエラーになる例を示すだけが目的のコードは、コンパイルエラーをなくすのではなく、コンパイルエラーの種類を収集するだけにする。
文法を示すのが目的のコード場合に、コンパイルエラーをなくすのに手間がかかる場合は、順次作業します。
3)リンクエラーをなくす方法を検討する。
文法を示すのが目的のコード場合に、リンクエラーをなくすのに手間がかかる場合は、順次作業します。
4)意味のある出力を作る。
コンパイル、リンクが通っても、意味のある出力を示そうとすると、コンパイル・リンクエラーが出て収拾できそうにない場合がある。順次作業します。

1)だけのものから4)まで進んだものと色々ある状態です。一歩でも前に進むご助言をお待ちしています。「検討事項」の欄に現状を記録するようにしています。

bash
$ docker run -v /Users/ogawakiyoshi/n4910/n3540:/Users/ogawakiyoshi/n4910/n3540 -it kaizenjapan/n3540 /bin/bash

読書感想文

CコンパイラによるC言語規格の読書感想文として掲載しています。

コンパイル実験が、CN3242に対する、gccとclangによる感想文だということご理解いただけると幸いです。

読書感想文は人間かAIだけが作るものとは限りません。
本(電子書籍を含む)を入力として、その内容に対する文字列を読書感想文として受け止めましょう。
元の文章をあり方、コンパイルできるように電子化しておくこと、コンパイラが解釈可能な断片の作り方など。

個人開発

Cコンパイラの試験を一人でもくもくとやっているのは個人開発の一つの姿です。

<この項は書きかけです。順次追記します。>

編纂器(Compiler)

clang --version

Debian clang version 14.0.6-++20220622053050+f28c006a5895-1~exp1~20220622173135.152
Target: x86_64-pc-linux-gnu Thread model: posix InstalledDir: /usr/bin

gcc --version

gcc (GCC) 12.1.0 Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

7.11 Localization , CN3054:2022 (19) p229.c

算譜(source code)

p229.c
// CN3054 Committee Draft, Standard for Programming Language C
// http://www.open-std.org/jtc1/sc22/wg14/docs/papers/2022/n3054.pdf
const char * n3054 = "7.11 Localization <locale.h>, CN3054:2022 (19) p229.c";
// Debian clang version 14.0.5-++20220610033153+c12386ae247c-
// g++ (GCC) 12.1.0 Copyright (C) 2022 Free Software Foundation, Inc.
// Edited by Dr. OGAWA Kiyoshi. Compile procedure and results record.
// C++N3054:2022 Standard Working Draft on ISO/IEC 14882(0) sample code compile list

#include "N3054.h"

// 7.11 Localization <locale.h>
//  The header <locale.h> declares two functions, one type, and defines several macros.
//  The type is struct lconv which contains members related to the formatting of numeric values. The structure shall contain at least the following members, in any order. The semantics of the members and their normal ranges are explained in 7.11.2.1. In the "C" locale, the members shall have the values specified in the comments.
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 p_cs_precedes;
char n_cs_precedes;
char p_sep_by_space;
char n_sep_by_space;
char p_sign_posn;
char n_sign_posn;
char *int_curr_symbol;
char int_frac_digits;
char int_p_cs_precedes;
char int_n_cs_precedes;
char int_p_sep_by_space;
char int_n_sep_by_space;
char int_p_sign_posn;
char int_n_sign_posn;
// "."
// ""
// ""
// ""
// ""
// ""
// ""
// ""
// ""
// CHAR_MAX
// CHAR_MAX
// CHAR_MAX
// CHAR_MAX
// CHAR_MAX
// CHAR_MAX
// CHAR_MAX
// ""
// CHAR_MAX
// CHAR_MAX
// CHAR_MAX
// CHAR_MAX
// CHAR_MAX
// CHAR_MAX
// CHAR_MAX
//  The macros defined are NULL (described in 7.21); and which expand to integer constant expressions with distinct values, suitable for use as the first argu- ment to the setlocale function.273) Additional macro definitions, beginning with the characters LC_ and an uppercase letter,274) may also be specified by the implementation.
// 7.11.1 Locale control 7.11.1.1 The setlocale function Synopsis
//  LC_ALL LC_COLLATE LC_CTYPE LC_MONETARY LC_NUMERIC LC_TIME
#include <locale.h>
char *setlocale(int category, const char *locale);
// 273)ISO/IEC 9945–2 specifies locale and charmap formats that can be used to specify locales for C. 274)See "future library directions" (7.33.7).
// Description
//  The setlocale function selects the appropriate portion of the program’s locale as specified by the category and locale arguments. The setlocale function may be used to change or query the program’s entire current locale or portions thereof. The value LC_ALL for category names the program’s entire locale; the other values for category name only a portion of the program’s locale. LC_COLLATE affects the behavior of the strcoll and strxfrm functions. LC_CTYPE affects the behavior of the character handling functions275) and the multibyte and wide character functions. LC_MONETARY affects the monetary formatting information returned by the localeconv function. LC_NUMERIC affects the decimal-point character for the formatted input/output functions and the string conversion functions, as well as the nonmonetary formatting information returned by the localeconv function. LC_TIME affects the behavior of the strftime and wcsftime functions.
//  A value of "C" for locale specifies the minimal environment for C translation; a value of "" for locale specifies the locale-specific native environment. Other implementation-defined strings may be passed as the second argument to setlocale.
//  At program startup, the equivalent of
f() {
    setlocale(LC_ALL, "C");
}
// is executed.
//  A call to the setlocale function may introduce a data race with other calls to the setlocale function or with calls to functions that are affected by the current locale. The implementation shall behave as if no library function calls the setlocale function.
// Returns
//  If a pointer to a string is given for locale and the selection can be honored, the setlocale function returns a pointer to the string associated with the specified category for the new locale. If the selection cannot be honored, the setlocale function returns a null pointer and the program’s locale is not changed.
//  A null pointer for locale causes the setlocale function to return a pointer to the string associated with the category for the program’s current locale; the program’s locale is not changed.276)
//  The pointer to string returned by the setlocale function is such that a subsequent call with that string value and its associated category will restore that part of the program’s locale. The string pointed to shall not be modified by the program. The behavior is undefined if the returned value is used after a subsequent call to the setlocale function, or after the thread which called the setlocale function to obtain the returned value has exited. Forward references: formatted input/output functions (7.23.6), multibyte/wide character conver- sion functions (7.24.7), multibyte/wide string conversion functions (7.24.8), numeric conversion functions (7.24.1), the strcoll function (7.26.4.3), the strftime function (7.29.3.5), the strxfrm function (7.26.4.5).
// 7.11.2 Numeric formatting convention inquiry 7.11.2.1 The localeconv function
// Synopsis
//  The localeconv function sets the components of an object with type struct lconv with values appropriate for the formatting of numeric quantities (monetary and otherwise) according to the rules of the current locale.
// 275)The only functions in 7.4 whose behavior is not affected by the current locale are isdigit and isxdigit.
// 276)The implementation is thus required to arrange to encode in a string the various categories due to a heterogeneous locale when category has the value LC_ALL.
//                     Description
#include <locale.h>
struct lconv *localeconv(void);
//  The members of the structure with type char * are pointers to strings, any of which (except decimal_point) can point to "", to indicate that the value is not available in the current locale or is of zero length. Apart from grouping and mon_grouping, the strings shall start and end in the initial shift state. The members with type char are nonnegative numbers, any of which can be CHAR_MAX to indicate that the value is not available in the current locale. The members include the following: char *decimal_point
// The decimal-point character used to format nonmonetary quantities.
// char *thousands_sep
// The character used to separate groups of digits before the decimal- point character in formatted nonmonetary quantities.
// char *grouping
// A string whose elements indicate the size of each group of digits in formatted nonmon- etary quantities.
// char *mon_decimal_point
// The decimal-point used to format monetary quantities.
// char *mon_thousands_sep
// The separator for groups of digits before the decimal-point in formatted monetary quantities.
// char *mon_grouping
// A string whose elements indicate the size of each group of digits in formatted monetary quantities.
// char *positive_sign
// The string used to indicate a nonnegative-valued formatted monetary quantity.
// char *negative_sign
// The string used to indicate a negative-valued formatted monetary quantity.
// char *currency_symbol
// The local currency symbol applicable to the current locale.
// char frac_digits
// The number of fractional digits (those after the decimal-point) to be displayed in a locally formatted monetary quantity.
// char p_cs_precedes
// Set to 1 or 0 if the currency_symbol respectively precedes or succeeds the value for a nonnegative locally formatted monetary quantity.
// char n_cs_precedes
// Set to 1 or 0 if the currency_symbol respectively precedes or succeeds the value for a negative locally formatted monetary quantity.
// char p_sep_by_space
// Set to a value indicating the separation of the currency_symbol, the sign string, and the value for a nonnegative locally formatted monetary quantity.
// char n_sep_by_space
// Set to a value indicating the separation of the currency_symbol, the sign string, and the value for a negative locally formatted monetary quantity.
//     char p_sign_posn
// Set to a value indicating the positioning of the positive_sign for a nonnegative locally formatted monetary quantity.
// char n_sign_posn
// Set to a value indicating the positioning of the negative_sign for a negative locally formatted monetary quantity.
// char *int_curr_symbol
// The international currency symbol applicable to the current locale. The first three characters contain the alphabetic international currency symbol in accordance with those specified in ISO 4217. The fourth character (immediately preceding the null character) is the character used to separate the international currency symbol from the monetary quantity.
// char int_frac_digits
// The number of fractional digits (those after the decimal-point) to be displayed in an internationally formatted monetary quantity.
// char int_p_cs_precedes
// Set to 1 or 0 if the int_curr_symbol respectively precedes or succeeds the value for a nonnegative internationally formatted monetary quantity.
// char int_n_cs_precedes
// Set to 1 or 0 if the int_curr_symbol respectively precedes or succeeds the value for a negative internationally formatted monetary quantity.
// char int_p_sep_by_space
// Set to a value indicating the separation of the int_curr_symbol, the sign string, and the value for a nonnegative internationally formatted monetary quantity.
// char int_n_sep_by_space
// Set to a value indicating the separation of the int_curr_symbol, the sign string, and the value for a negative internationally formatted monetary quantity.
// char int_p_sign_posn
// Set to a value indicating the positioning of the positive_sign for a nonnegative internationally formatted monetary quantity.
// char int_n_sign_posn
// Set to a value indicating the positioning of the negative_sign for a negative interna- tionally formatted monetary quantity.
// The elements of grouping and mon_grouping are interpreted according to the following:
// CHAR_MAX other No further grouping is to be performed. The previous element is to be repeatedly used for the remainder of the digits.
// The integer value is the number of digits that compose the current group. The next element is examined to determine the size of the next group of digits before the current group. values of p_sep_by_space, n_sep_by_space, int_p_sep_by_space, and
//  No space separates the currency symbol and value.
//  If the currency symbol and sign string are adjacent, a space separates them from the value; otherwise, a space separates the currency symbol from the value.
//  If the currency symbol and sign string are adjacent, a space separates them; otherwise, a space separates the sign string from the value.
//  The int_n_sep_by_space are interpreted according to the following:
// For int_p_sep_by_space and int_n_sep_by_space, the fourth character of int_curr_symbol is used instead of a space.
// The values of p_sign_posn, n_sign_posn, int_p_sign_posn, and int_n_sign_posn are inter- preted according to the following:
//  Parentheses surround the quantity and currency symbol.
//  The sign string precedes the quantity and currency symbol.
//  The sign string succeeds the quantity and currency symbol.
//  The sign string immediately precedes the currency symbol.
//  The sign string immediately succeeds the currency symbol.
//  The implementation shall behave as if no library function calls the localeconv function. Returns
//  The localeconv function returns a pointer to the filled-in object. The structure pointed to by the return value shall not be modified by the program, but may be overwritten by a subsequent call to the localeconv function. In addition, calls to the setlocale function with categories LC_ALL, LC_MONETARY, or LC_NUMERIC may overwrite the contents of the structure.
//  EXAMPLE 1 The following table illustrates rules which might well be used by four countries to format monetary quantities.
//    Local format Country Country1     1.234,56 mk Country2     L.1.234 Country3     ƒ1.234,56 Country4     SFrs.1,234.56   SFrs.1,234.56C   CHF 1,234.56   CHF 1,234.56C
//  For these four countries, the respective values for the monetary members of the structure returned by localeconv could be:
//  EXAMPLE 2 The following table illustrates how the cs_precedes, sep_by_space, and sign_posn members affect the formatted value.
int main() {
//    PR3(st.a,st.b,st.c,d);
    printf("%s\n", n3054);
    return EXIT_SUCCESS;
}

編纂・実行結果(compile and go)

bash
$ clang p229.c -std=11 -o p229l -I. -Wall
p229.c:57:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
f(){
^
p229.c:59:1: warning: non-void function does not return a value [-Wreturn-type]
}
^
2 warnings generated.
7.11 Localization <locale.h>, CN3054:2022 (19) p229.c

$ clang p229.c -std=17 -o p229l -I. -Wall
p229.c:57:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
f(){
^
p229.c:59:1: warning: non-void function does not return a value [-Wreturn-type]
}
^
2 warnings generated.
7.11 Localization <locale.h>, CN3054:2022 (19) p229.c

$ clang p229.c -std=2x -o p229l -I. -Wall
p229.c:57:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
f(){
^
p229.c:59:1: warning: non-void function does not return a value [-Wreturn-type]
}
^
2 warnings generated.
7.11 Localization <locale.h>, CN3054:2022 (19) p229.c

$ gcc p229.c -std=11 -o p229g -I. -Wall
p229.c:57:1: warning: return type defaults to 'int' [-Wimplicit-int]
   57 | f(){
      | ^
p229.c: In function 'f':
p229.c:59:1: warning: control reaches end of non-void function [-Wreturn-type]
   59 | }
      | ^
7.11 Localization <locale.h>, CN3054:2022 (19) p229.c

$ gcc p229.c -std=c17 -o p229g -I. -Wall
p229.c:57:1: warning: return type defaults to 'int' [-Wimplicit-int]
   57 | f(){
      | ^
p229.c: In function 'f':
p229.c:59:1: warning: control reaches end of non-void function [-Wreturn-type]
   59 | }
      | ^
7.11 Localization <locale.h>, CN3054:2022 (19) p229.c

$ gcc p229.c -std=c2x -o p229g -I. -Wall
p229.c:57:1: warning: return type defaults to 'int' [-Wimplicit-int]
   57 | f(){
      | ^
p229.c: In function 'f':
p229.c:59:1: warning: control reaches end of non-void function [-Wreturn-type]
   59 | }
      | ^
7.11 Localization <locale.h>, CN3054:2022 (19) p229.c

検討事項(agenda)

意味のある出力。

応用例1 MISRA C/C++

MISRA C まとめ #include

MISRA C++ 5-0-16

応用例2 CERT C/C++

SEI CERT C++ Coding Standard AA. Bibliography 確認中。

MISRA C/C++, AUTOSAR C++, CERT C/C++とC/C++工業標準をコンパイルする

自己参考資料(self reference)

関連する自己参照以外は、こちらの先頭に移転。

C言語(C++)に対する誤解、曲解、無理解、爽快。

C2011コンパイル一覧@researchmap

https://researchmap.jp/jownvh0ye-1797580/#_1797580

[C][C++]の国際規格案の例題をコンパイルするときの課題7つ。

C Puzzle Bookの有り難み5つ、C言語規格及びCコンパイラの特性を認識

dockerにclang

docker gnu(gcc/g++) and llvm(clang/clang++)

コンパイル用shell script C版(clangとgcc)とC++版(clang++とg++)

astyle 使ってみた

<この記事は個人の過去の経験に基づく個人の感想です。現在所属する組織、業務とは関係がありません。>

文書履歴(document history)

ver. 0.01 初稿  20221121
ver. 0.02 ありがとう追記 20230622

最後までおよみいただきありがとうございました。

いいね 💚、フォローをお願いします。

Thank you very much for reading to the last sentence.

Please press the like icon 💚 and follow me for your happy life.

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0