ソースファイルに書き込む字句は,:
コメント
プリプロセッサ
宣言
式
文
関数の定義
char 型は文字 (character) を扱うために用意された型である.
char.c
1 2 3 4 5 6 7 8 9 10 11 12 13 | #include <stdio.h>
int main(void){
char c1 = 'A';
char c2 = '\101';
char c3 = '\x41';
printf("(char c1) %c (%d, %o, %x)\n", c1, c1, c1, c1);
printf("(char c2) %c (%d, %o, %x)\n", c2, c2, c2, c2);
printf("(char c3) %c (%d, %o, %x)\n", c3, c3, c3, c3);
return 0;
}
|
char.c の実行結果は:
[cactus:~/code_c/c_tuts]% ./char
(char c1) A (65, 101, 41)
(char c2) A (65, 101, 41)
(char c3) A (65, 101, 41)
型によって扱える整数の範囲が異なる.
扱える整数の範囲は,:
char, short (short int), int, long (long int), long long (long long int)
の順に大きくなる.
short_int_long.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include <stdio.h>
int main(void){
int a = 65, b = 0101, c = 0x41;
long l = 65L, m = 0101L, n = 0x41L; // l = 65l でも OK
printf("\t%%d\t%%o\t%%x\n");
printf("(a)\t%d\t%o\t%x\n", a, a, a);
printf("(b)\t%d\t%o\t%x\n", b, b, b);
printf("(c)\t%d\t%o\t%x\n", c, c, c);
printf("\t%%ld\t%%lo\t%%lx\n");
printf("(l)\t%ld\t%lo\t%lx\n", l, l, l);
printf("(m)\t%ld\t%lo\t%lx\n", m, m, m);
printf("(n)\t%ld\t%lo\t%lx\n", n, n, n);
return 0;
}
|
short_int_long.c の実行結果は:
[cactus:~/code_c/c_tuts]% ./short_int_long
%d %o %x
(a) 65 101 41
(b) 65 101 41
(c) 65 101 41
%ld %lo %lx
(l) 65 101 41
(m) 65 101 41
(n) 65 101 41
float_double_longdouble.c
1 2 3 4 5 6 7 8 9 10 11 12 13 | #include <stdio.h>
int main(void){
float x = 3.14F;
double y = 1E-5;
long double z = 2.345L;
printf("x %f, %E\n", x, x);
printf("y %f, %E\n", y, y);
printf("z %Lf, %LE\n", z, z);
return 0;
}
|
float_double_longdouble.c の実行結果は:
[cactus:~/code_c/c_tuts]% ./float_double_longdouble
x 3.140000, 3.140000E+00
y 0.000010, 1.000000E-05
z 2.345000, 2.345000E+00
float.c
1 2 3 4 5 6 7 8 9 10 11 12 13 | #include <stdio.h>
#include <float.h>
int main(void){
printf("\tb\te\t\tp\tMIN\t\tMAX\n");
printf("\t\tMIN\tMAX\n");
printf("float\t%d\t%d\t%d\t%d\t%E\t%E\n",
FLT_RADIX, FLT_MIN_EXP, FLT_MAX_EXP, FLT_MANT_DIG, FLT_MIN, FLT_MAX);
printf("double\t%d\t%d\t%d\t%d\t%E\t%E\n",
FLT_RADIX, DBL_MIN_EXP, DBL_MAX_EXP, DBL_MANT_DIG, DBL_MIN, DBL_MAX);
return 0;
}
|
float.c の実行結果は
b e p MIN MAX
MIN MAX
float 2 -125 128 24 1.175494E-38 3.402823E+38
double 2 -1021 1024 53 2.225074E-308 1.797693E+308
enum(列挙体, enumeration):
enum タグ{const1, const2, ...};
enum タグ 変数名 = 列挙定数;
enum.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #include <stdio.h>
int main(void){
enum weekday{
sun,
mon,
tue,
wed,
thu,
fri,
sat
}day = tue;
printf("weekday = %d\n", day);
return 0;
}
|
enum.c の実行結果は:
[cactus:~/code_c/c_tuts]% ./enum
weekday = 2
enum2.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #include <stdio.h>
int main(void){
enum weekday{
sun,
mon,
tue,
wed,
thu,
fri,
sat
};
enum weekday day = tue;
printf("weekday = %d\n", day);
return 0;
}
|
enum2.c の実行結果は:
[cactus:~/code_c/c_tuts]% ./enum2
weekday = 2
タグを外した宣言も可能である:
enum{const1, const2, ...} 変数名 = 列挙定数;
この場合は, 変数名は enum{const1, const2, ...} 型である.
enum3.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #include <stdio.h>
int main(void){
enum{
sun,
mon,
tue,
wed,
thu,
fri,
sat
}weekday = tue;
printf("weekday = %d\n", weekday);
return 0;
}
|
enum3.c の実行結果は:
[cactus:~/code_c/c_tuts]% ./enum3
weekday = 2
ただ単に, 整数の定数の作成にも利用できる:
enum{const1, const2, ...};
enum4.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include <stdio.h>
int main(void){
enum{
a, // a = 0
b, // b = 1
c, // c = 2
d = 5, // d = 5
e, // e = 6
f // f = 7
};
printf("c + e = %d\n", c + e);
return 0;
}
|
enum4.c の実行結果は:
[cactus:~/code_c/c_tuts]% ./enum4
c + e = 8
sizeof 演算子:
sizeof(変数), sizeof 変数
sizeof.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #include <stdio.h>
int main(void){
printf("Type\t\tByte(s)\tBit(s)\n");
printf( "char\t\t%zd\t%zd\n", sizeof(char), 8*sizeof(char) );
printf( "short\t\t%zd\t%zd\n", sizeof(short), 8*sizeof(short) );
printf( "int\t\t%zd\t%zd\n", sizeof(int), 8*sizeof(int) );
printf( "long\t\t%zd\t%zd\n", sizeof(long), 8*sizeof(long) );
printf( "long long\t%zd\t%zd\n", sizeof(long long), 8*sizeof(long long) );
printf( "float\t\t%zd\t%zd\n", sizeof(float), 8*sizeof(float) );
printf( "double\t\t%zd\t%zd\n", sizeof(double), 8*sizeof(double) );
printf( "long double\t%zd\t%zd\n", sizeof(long double), 8*sizeof(long double) );
/*
仕様 (C99) では, size_t 型の出力には z を付けること.
例:%zd
*/
return 0;
}
|
sizeof.c の実行結果は,
Type Byte(s) Bit(s)
char 1 8
short 2 16
int 4 32
long 8 64
long long 8 64
float 4 32
double 8 64
long double 16 128
limit.c
1 2 3 4 5 6 7 8 9 10 11 12 13 | #include <stdio.h>
#include <limits.h>
int main(void){
printf("TYPE\t\tMIN\t\t\tMAX\n");
printf("char\t\t%d\t\t\t%d\n", CHAR_MIN, CHAR_MAX);
printf("short\t\t%d\t\t\t%d\n", SHRT_MIN, SHRT_MAX);
printf("int\t\t%d\t\t%d\n", INT_MIN, INT_MAX);
printf("long\t\t%ld\t%ld\n", LONG_MIN, LONG_MAX);
printf("long long\t%lld\t%lld\n", LLONG_MIN, LLONG_MAX);
return 0;
}
|
limit.c の実行結果は,
TYPE MIN MAX
char -128 127
short -32768 32767
int -2147483648 2147483647
long -9223372036854775808 9223372036854775807
long long -9223372036854775808 9223372036854775807