年頭から始まったこの連載も最終回となりました。ここまでのご愛読ありがとうございます。最終回の今回は、
配列
C言語では、
char array[10];
のように宣言します。この例ではchar型の、
C言語では文字列はchar型の配列です。また、
また、\n
の次に\0
という特殊文字を入れて、
#include <stdio.h>
int
main()
{
char buf[32];
buf[0] = 'H';
buf[1] = 'e';
buf[2] = 'l';
buf[3] = 'l';
buf[4] = 'o';
buf[5] = ' ';
buf[6] = 'W';
buf[7] = 'o';
buf[8] = 'r';
buf[9] = 'l';
buf[10] = 'd';
buf[11] = '\n';
buf[12] = '\0';
printf(buf);
return 0;
}
ポインタ
C言語では、
char *p;
と宣言します。p自体はchar型へのポインタであり、
リストA3.
#include <stdio.h>
int
main()
{
char *p;
p = "Hello World\n";
printf(p);
return 0;
}
関数の呼び出し
プログラム中の一定の処理をまとめて、
hello.
#include <stdio.h>
static void
pr(char *p)
{
printf(p);
}
int
main()
{
pr("Hello World\n");
return 0;
}
構造体
構造体を使えば、
struct s_example {
int i;
char *p;
};
共用体
C言語には、
union u_example {
unsigned char c[4];
unsigned long l;
};
makeコマンドを単独で使う
プログラムをコンパイルするたびにgccのコマンドラインを入力するのは大変です。makeというコマンドを使えば、
$ make hello gcc hello.c -o hello $
最適化オプションを付けたい場合は、
$ CFLAGS=-O2 $ export CFLAGS $ make hello gcc -O2 hello.c -o hello $
ここで紹介した例は、
Makefileを記述すれば、