文字検査/変換マクロ

isalnum(c)文字cが英数字(A〜Z、a〜z、0〜9)なら真
isalpha(c)文字cが英文字(A〜Z、a〜z)なら真
isascii(c)文字cがASCIIコード(0x00〜0x7f)なら真
iscntrl(c)文字cが制御文字(0x00〜0x1f、0x7f)なら真
isdigit(c)文字cが数字(0〜9)なら真
isgraph(c)文字cが空白を除く印字可能文字(0x21〜0x7e)なら真
islower(c)文字cが小文字(a〜z)なら真
isprint(c)文字cが印字可能文字(0x20〜0x7e)なら真
ispunct(c)文字cが句読点(0x21〜0x2f、0x30〜0x40、0x5b〜0x60、0x7b〜0x7e)なら真
isspace(c)文字cが空白、タブ、復帰、改行、垂直タブ、改頁(0x09〜0x0d、0x20)なら真
isupper(c)文字cが大文字(A〜Z)なら真
isxdigit(c)文字cが16進表示文字(0〜9、A〜F、a〜f)なら真
tolower(c)文字cが大文字なら小文字に変換した値を返す
toupper(c)文字cが小文字なら大文字に変換した値を返す


サンプル

#include <stdio.h>
#include <ctype.h>

void main()
{
 int c;

 for ( c=0; c<=127; c++ ) printf( "%d:%c ", c, c );
 puts( "\n" );
 puts( "isalnumの結果" );
 for ( c=0; c<=127; c++ ) if ( isalnum(c) ) printf( "%d:%c ", c, c );
 puts( "\n" );
 puts( "isalphaの結果" );
 for ( c=0; c<=127; c++ ) if ( isalpha(c) ) printf( "%d:%c ", c, c );
 puts( "\n" );
 puts( "isasciiの結果" );
 for ( c=0; c<=127; c++ ) if ( isascii(c) ) printf( "%d:%c ", c, c );
 puts( "\n" );
 puts( "iscntrlの結果" );
 for ( c=0; c<=127; c++ ) if ( iscntrl(c) ) printf( "%d:%c ", c, c );
 puts( "\n" );
 puts( "isdigitの結果" );
 for ( c=0; c<=127; c++ ) if ( isdigit(c) ) printf( "%d:%c ", c, c );
 puts( "\n" );
 puts( "isgraphの結果" );
 for ( c=0; c<=127; c++ ) if ( isgraph(c) ) printf( "%d:%c ", c, c );
 puts( "\n" );
 puts( "islowerの結果" );
 for ( c=0; c<=127; c++ ) if ( islower(c) ) printf( "%d:%c ", c, c );
 puts( "\n" );
 puts( "isprintの結果" );
 for ( c=0; c<=127; c++ ) if ( isprint(c) ) printf( "%d:%c ", c, c );
 puts( "\n" );
 puts( "ispunctの結果" );
 for ( c=0; c<=127; c++ ) if ( ispunct(c) ) printf( "%d:%c ", c, c );
 puts( "\n" );
 puts( "isspaceの結果" );
 for ( c=0; c<=127; c++ ) if ( isspace(c) ) printf( "%d:%c ", c, c );
 puts( "\n" );
 puts( "isupperの結果" );
 for ( c=0; c<=127; c++ ) if ( isupper(c) ) printf( "%d:%c ", c, c );
 puts( "\n" );
 puts( "isxdigitの結果" );
 for ( c=0; c<=127; c++ ) if ( isxdigit(c) ) printf( "%d:%c ", c, c );
 puts( "\n" );
 puts( "tolowerの結果\n" );
 for ( c=0; c<=127; c++ ) if ( isupper(c) ) printf( "%d:%c ", c, tolower(c) );
 puts( "\n" );
 puts( "toupperの結果\n" );
 for ( c=0; c<=127; c++ ) if ( islower(c) ) printf( "%d:%c ", c, toupper(c) );
 puts( "\n" );
}

C > 標準関数 > 文字検査/変換マクロ | comments (0) | trackbacks (0)

必要なヘッダファイル一覧

入出力stdio.h
一般stdlib.h
文字列string.h
時間time.h
文字ctype.h
数学math.h
ジャンプsetjmp.h
シグナルsignal.h
診断assert.h
C > 標準関数 > ヘッダ | comments (0) | trackbacks (0)

#define



#deifne PI 3.14159L
#define RAD(x) ( (x) * PI / 180.0 )


・マクロパラメータはカッコを付ける
・マクロ全体にカッコを付ける
・マクロパラメータにインクリメント/デクリメントは避ける
C > プリプロセッサ | comments (0) | trackbacks (0)

scanf

サンプル

#include <stdio.h>
void main()
{
 double d;
 while( scanf( "%lf", &d ) != EOF )
  printf( "%f\n", d );
}


double型の場合、printfは%f、scanfは%lfである。
float型の場合はどちらも%fである。
C > 標準関数 > コンソール入出力関数 | comments (0) | trackbacks (0)

gets

連続1行入力の基本パターン

#include <stdio.h>
void main()
{
 char s[256];
 while ( ( gets( s ) ) != NULL )
  puts( s );
}


getsによる入力の終了はNULLで知る事ができる。
C > 標準関数 > コンソール入出力関数 | comments (0) | trackbacks (0)

getchar

連続1文字入力の基本パターン

#include <stdio.h>
void main()
{
 int c;
 while ( ( c = getchar() ) != EOF )
  putchar( c );
}


getcharによる入力の終了はEOFで知る事ができる。
C > 標準関数 > コンソール入出力関数 | comments (0) | trackbacks (0)