「C言語/標準ライブラリ/ctype.h」の版間の差分

削除された内容 追加された内容
Ef3 (トーク | 投稿記録)
DL化
タグ: 2017年版ソースエディター
Ef3 (トーク | 投稿記録)
cleanup
タグ: 2017年版ソースエディター
1 行
{{Nav}}
 
ヘッダーファイル <ctype.h> は、文字の分類やマッピングに役立ついくつかの関数を宣言しています<ref name="jtc1-sc22-wg14-n1570-7.4.1">{{cite book
== 概要 ==
| url = http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf
文字操作の関数を使用するためには、
| title = N1570 Committee Draft — April 12, 2011 ISO/IEC 9899:201x
ctype.hというヘッダファイルを組み込む必要がある。
| page=200, §7.4 ''Character handling <ctype.h>''
<ref>『JISX3010:2003』p.135「7.4 文字操作<ctype.h>」</ref>
| publisher = ISO/IEC}}</ref><ref>『JISX3010:2003』p.135「7.4 文字操作<ctype.h>」</ref>。
 
ここで宣言する関数はのすべての引数はintで、その値はunsigned charとして表現可能であるか、またはマクロEOFの値と等しくなければなりません(それ以外の値の場合、動作は未定義( '''undefined''' )です)。
これらの関数の動作は、現在のロケールの影響を受けます。
"C" ロケールでない場合にのみロケール固有の側面を持つ関数を以下に示します。
 
;印刷文字:表示装置の1つの印刷位置を占めるローカル固有の文字の集合の一つ
;制御文字:印刷文字ではないローカル固有の文字の集合の一つ
すべての文字と数字は印刷文字です。
 
次に文字操作の関数を表にまとめた。
{|class="wikitable"
|+ 文字操作関数の一覧
!形式!!機能
!形式!!機能<ref>'( )'の中は参考(locale によって変わりうるものもある)</ref>
|-
!colspan=2|文字種分類関数!!文字が以下のとき真、それ以外の時は偽を返す
|-
|[[#isalnum関数|int isalnum(int c);]]||英数字('A'~'Z', 'a'~'z', '0'~'9')
35 ⟶ 45行目:
|[[#isxdigit関数|int isxdigit(int c);]]||16進数字('0'~'9', 'A'~'F', 'a'~'f')
|-
!colspan=2|大文字小文字変換関数!!
|-
|[[#tolower関数|int tolower(int c);]]||文字cを小文字に変換した値を返す。
|-
|[[#toupper関数|int toupper(int c);]]||文字cを大文字に変換した値を返す。
|-
|}
 
== 文字種分類関数 ==
文字種分類関数( ''Character classification functions'' )は、引数 <var>c</var> の値が関数の説明にある値と一致する場合に限り、0以外の値(真)を返します<ref name="jtc1-sc22-wg14-n1570-7.4.1">{{cite book
<ref>『JISX3010:2003』p.136「7.4.1 文字種分類関数」</ref>
| url = http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf
| title = N1570 Committee Draft — April 12, 2011 ISO/IEC 9899:201x
| page=200, §7.4.1 ''Character classification functions''
| publisher = ISO/IEC}}</ref><ref>『JISX3010:2003』p.136「7.4.1 文字種分類関数」</ref>。
 
=== isalnum関数 ===
63 ⟶ 76行目:
#include <ctype.h>
 
int main(void) {
{
int c;
printf("文字を入力してください:");
int c = getchar();
72 ⟶ 83行目:
else
printf("%cは英数字ではありません。", c);
}
</syntaxhighlight>
90 ⟶ 100行目:
; 例
: <syntaxhighlight lang=c>
#include <ctype.h>
#include <stdio.h>
#include <ctype.h>
 
int main(void) {
printf("文字を入力してください:");
{
int c = getchar();
if (isalnum(c))
printf("文字を入力してください:");
printf("%cは英数字です。", c);
int c = getchar();
else
if (isalpha(c))
printf("%cは英字ではありません。", c);
else
printf("%cは英文字ではありません。", c);
}
</syntaxhighlight>
 
 
=== isblank関数 ===
149 ⟶ 156行目:
; 例
: <syntaxhighlight lang=c>
#include <ctype.h>
#include <stdio.h>
#include <ctype.h>
 
int main(void) {
int c;
{
printf("文字を入力してください:");
int c;
int c = getchar();
printf("文字を入力してください:");
if (isctrl(c))
int c = getchar();
printf("%cは制御文字です。", c);
if (isctrl(c))
else
printf("%cは制御文字です。", c);
printf("%cは制御文字ではありません。", c);
else
printf("%cは制御文字ではありません。", c);
}
</syntaxhighlight>
179 ⟶ 184行目:
; 例
: <syntaxhighlight lang=c>
#include <ctype.h>
#include <stdio.h>
#include <ctype.h>
 
int main(void) {
printf("文字を入力してください:");
{
int c = getchar();
printf("文字を入力してください:");
if (isdigit(c))
int c = getchar();
printf("%cは数字です。", c);
if (isdigit(c))
else
printf("%cは数字です。", c);
printf("%cは数字ではありません。", c);
else
printf("%cは数字ではありません。", c);
}
</syntaxhighlight>
208 ⟶ 211行目:
; 例
: <syntaxhighlight lang=c>
#include <ctype.h>
#include <stdio.h>
#include <ctype.h>
 
int main(void) {
 
{
printf("文字を入力してください:");
int c;
int c = getchar();
printf("文字を入力してください:");
if (isgraph(c))
int c = getchar();
printf("%cは表示文字(空白を除く)です。", c);
if (isgraph(c))
else
printf("%cは表示文字(空白を除く)です。", c);
printf("%cは表示文字(空白を除く)ではありません。", c);
else
printf("%cは表示文字(空白を除く)ではありません。", c);
}
 
</syntaxhighlight>
 
238 ⟶ 240行目:
; 例
: <syntaxhighlight lang=c>
#include <ctype.h>
#include <stdio.h>
#include <ctype.h>
 
int main(void) {
 
{
printf("文字を入力してください:");
int c;
int c = getchar();
printf("文字を入力してください:");
if (islower(c))
int c = getchar();
printf("%cは英小文字です。", c);
if (islower(c))
else
printf("%cは英小文字です。", c);
printf("%cは英小文字ではありません。", c);
else
printf("%cは英小文字ではありません。", c);
}
</syntaxhighlight>
268 行
; 例
: <syntaxhighlight lang=c>
#include <ctype.h>
#include <stdio.h>
#include <ctype.h>
 
int main(void) {
printf("文字を入力してください:");
{
int c = getchar();
printf("文字を入力してください:");
if (isprint(c))
int c = getchar();
printf("%cは表示文字(空白を含む)です。", c);
if (isprint(c))
else
printf("%cは表示文字(空白を含む)です。", c);
printf("%cは表示文字(空白を含む)ではありません。", c);
else
printf("%cは表示文字(空白を含む)ではありません。", c);
}
</syntaxhighlight>
297 ⟶ 295行目:
; 例
: <syntaxhighlight lang=c>
#include <ctype.h>
#include <stdio.h>
#include <ctype.h>
 
int main(void) {
 
{
printf("文字を入力してください:");
int c;
int c = getchar();
printf("文字を入力してください:");
if (ispunct(c))
int c = getchar();
printf("%cは区切り文字です。", c);
if (ispunct(c))
else
printf("%cは区切り文字です。", c);
printf("%cは区切り文字ではありません。", c);
else
printf("%cは区切り文字ではありません。", c);
}
</syntaxhighlight>
327 ⟶ 323行目:
; 例
: <syntaxhighlight lang=c>
#include <ctype.h>
#include <stdio.h>
#include <ctype.h>
 
int main(void) {
printf("文字を入力してください:");
{
int c = getchar();
if (isspace(c))
printf("文字を入力してください:");
printf("%cは標準空白類文字です。", c);
int c = getchar();
else
if (isspace(c))
printf("%cは標準空白類文字ではありません。", c);
else
printf("%cは標準空白類文字ではありません。", c);
}
</syntaxhighlight>
357 ⟶ 350行目:
; 例
: <syntaxhighlight lang=c>
#include <stdio.h>
#include <ctype.h>
#include <stdio.h>
 
int main(void) {
printf("文字を入力してください:");
{
int c = getchar();
printf("文字を入力してください:");
if (isupper(c))
int c = getchar();
printf("%cは英大文字です。", c);
if (isupper(c))
else
printf("%cは英大文字です。", c);
printf("%cは英大文字ではありません。", c);
else
printf("%cは英大文字ではありません。", c);
}
</syntaxhighlight>
386 ⟶ 377行目:
; 例
: <syntaxhighlight lang=c>
#include <ctype.h>
#include <stdio.h>
#include <ctype.h>
 
int main(void) {
printf("文字を入力してください:");
{
int c = getchar();
printf("文字を入力してください:");
if (isxdigit(c))
int c = getchar();
printf("%cは16進数字です。", c);
if (isxdigit(c))
else
printf("%cは16進数字です。", c);
printf("%cは16進数字ではありません。", c);
else
printf("%cは16進数字ではありません。", c);
}
</syntaxhighlight>
 
== 大文字小文字変換関数 ==
大文字小文字変換関数( ''Character case mapping functions'' )は、変換条件に合致する文字に置き換える。
<ref>『JISX3010:2003』p.138「7.4.2 大文字小文字変換関数」</ref>
変換条件に合致する文字は、''Locate'' によっては複数が該当する可能性があるが、同じ ''Locale'' の場合、常に同じ値を返す<ref name="jtc1-sc22-wg14-n1570-7.4.2">{{cite book
| url = http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf
| title = N1570 Committee Draft — April 12, 2011 ISO/IEC 9899:201x
| page=170, §7.4.2 ''Character case mapping functions''
| publisher = ISO/IEC}}</ref><ref>『JISX3010:2003』p.138「7.4.2 大文字小文字変換関数」</ref>。
 
=== tolower関数 ===
421 ⟶ 415行目:
#include <ctype.h>
 
int main(void) {
printf("文字を入力してください:");
{
int c = getchar();
printf("文字を入力してください:");
printf("%cは小文字にすると%cです。", c, tolower(c));
int c = getchar();
printf("%cは小文字にすると%cです。", c, tolower(c));
}
</syntaxhighlight>
443 ⟶ 436行目:
; 例
: <syntaxhighlight lang=c>
#include <ctype.h>
#include <stdio.h>
#include <ctype.h>
 
int main(void) {
printf("文字を入力してください:");
{
int c = getchar();
printf("文字を入力してください:");
printf("%cは大文字にすると%cです。", c, toupper(c));
int c = getchar();
printf("%cは大文字にすると%cです。", c, toupper(c));
}
</syntaxhighlight>
466 ⟶ 458行目:
* 日本工業標準調査会(当時、現:日本産業標準調査会)『JISX3010 プログラム言語C』2003年12月20日改正(ISO/IEC 9899:1999,Programming languages―C及びISO/IEC 9899 Technical Corrigendum 1:2001の和訳)
 
[[Category:C言語|ひようゆんらいふらり そうさ]]