メモ > 技術 > プログラミング言語: C/C++ > C: 基本的なプログラム
■C: 基本的なプログラム
■変数
#include <stdio.h>
int main()
{
int num = 123;
float pi = 3.1415;
char a = 'A';
char *str1 = "C言語の世界へ";
char *str2 = "ようこそ!";
printf("num = %d\n", num);
printf("num(5桁のゼロ詰め) = %05d\n", num);
printf("pi(小数点第2位まで) = %.2f\n", pi);
printf("a = %c\n", a);
printf("str1 str2 = %s %s\n", str1, str2);
return 0;
}
■条件分岐
#include <stdio.h>
int main()
{
int sample = 10;
if (sample > 5) {
printf("変数sampleの内容は5より大きいです。\n");
} else {
printf("変数sampleの内容は5より小さいです。\n");
}
printf("%s\n" , sample ? "5より大きい" : "5より小さい");
sample = 2;
switch (sample) {
case 1:
printf("sampleの内容は1です。\n");
break;
case 2:
printf("sampleの内容は2です。\n");
break;
case 3:
printf("sampleの内容は3です。\n");
break;
default:
printf("sampleの内容は1〜3以外です。\n");
}
return 0;
}
■繰り返し
#include <stdio.h>
int main()
{
int count;
for (count = 1; count <= 10; count++) {
printf("繰り返しのテスト%d。\n", count);
}
count = 1;
while (count <= 10) {
printf("繰り返しのテスト%d。\n", count);
count++;
}
return 0;
}
■配列
#include <stdio.h>
int main()
{
int ary1[3];
ary1[0] = 10;
ary1[1] = 100;
ary1[2] = 1000;
printf("%d\n%d\n%d\n", ary1[0] , ary1[1] , ary1[2]);
int ary2[2][2];
ary2[0][0] = 10;
ary2[0][1] = 100;
ary2[1][0] = 1000;
ary2[1][1] = 10000;
printf("%d\n%d\n" , ary2[0][0] , ary2[0][1]);
printf("%d\n%d\n" , ary2[1][0] , ary2[1][1]);
return 0;
}
■文字列
#include <stdio.h>
int main()
{
// 配列で文字列
char str1[5];
int count;
str1[0] = 'T';
str1[1] = 'e';
str1[2] = 's';
str1[3] = 't';
str1[4] = 0;
for (count = 0; count <= 4; count++) {
printf("%c" , str1[count]);
}
printf("\n");
printf("%s\n", str1);
// まとめて初期化
char str2[6] = {'H', 'e', 'l', 'l', 'o', '!'};
for (count = 0; count <= 5; count++) {
printf("%c",str2[count]);
}
printf("\n");
// 文字列として初期化
char str3[6] = "Hello!";
printf("%s\n",str3);
// サイズの指定を省略
char str4[] = "Hello! World.";
printf("%s\n",str4);
return 0;
}
■ポインタ
#include <stdio.h>
int main()
{
int var, *p;
var = 123;
p = &var;
printf("変数に格納されている内容 = %d\n" , var);
printf("変数のアドレス = %x\n" , &var);
printf("ポインタに格納されているアドレス = %x\n" , p);
printf("ポインタに格納されている内容 = %d\n" , *p);
return 0;
}
実行すると、以下のように表示される(プログラムのファイルは「pointer.c」とする)
$ gcc -o pointer pointer.c
$ ./pointer
変数に格納されている内容 = 123
変数のアドレス = 4de5c384
ポインタに格納されているアドレス = 4de5c384
ポインタに格納されている内容 = 123
文字列ポインタについても整理して記載する
文字列の配列
http://wisdom.sakura.ne.jp/programming/c/c19.html
文字列ポインタ
http://wisdom.sakura.ne.jp/programming/c/c23.html
■関数
#include <stdio.h>
int test1()
{
printf("test1\n");
return 1;
}
int test2()
{
printf("test2\n");
return 1;
}
int main()
{
printf("main\n");
test1();
test2();
return 0;
}
■構造体
#include <stdio.h>
struct {
char *name;
int age;
} taro, hanako;
int main()
{
taro.name = "山田太郎";
taro.age = 16;
hanako.name = "山田花子";
hanako.age = 15;
printf("名前\t\t年齢\n");
printf("%s\t%d\n", taro.name, taro.age);
printf("%s\t%d\n", hanako.name, hanako.age);
return 0;
}
構造体名に任意のものを指定できるように
#include <stdio.h>
struct MEMBER {
char *name;
int age;
};
int main()
{
struct MEMBER taro, hanako;
taro.name = "山田太郎";
taro.age = 16;
hanako.name = "山田花子";
hanako.age = 15;
printf("名前\t\t年齢\n");
printf("%s\t%d\n", taro.name, taro.age);
printf("%s\t%d\n", hanako.name, hanako.age);
return 0;
}
■プリプロセッサ
コンパイル処理に先立って行われる処理(プリプロセッサの実行後にコンパイルが行われる)
includeによってファイルを取り込んだり、defineで値を置換したりできる
#include <stdio.h>
#define LOOP 5
#define MESSAGE "テスト"
int main()
{
int count;
for (count = 1; count <= LOOP; count++) {
printf("繰り返しの%s%d。\n", MESSAGE, count);
}
return 0;
}
■メモリの動的確保
プログラムの実行中に、必要に応じてメモリを確保する
C言語では malloc() でメモリを動的に確保できる
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char *po;
po = (char *)malloc(sizeof(char) * 128);
if (po == NULL) exit(1);
strcpy(po , "Hello, World!\n");
printf("%s" , po);
return 0;
}
■make
main.c
#include <stdio.h>
#include <stdlib.h>
extern int test1(void);
extern int test2(void);
int main()
{
printf("main\n");
test1();
test2();
return 0;
}
test1.c
#include <stdio.h>
int test1()
{
printf("test1\n");
return 1;
}
test2.c
#include <stdio.h>
int test2()
{
printf("test2\n");
return 1;
}
makeを使わずにビルドする場合
毎回適切にビルドを実行する必要がある
$ gcc -c -o main.o main.c
$ gcc -c -o test1.o test1.c
$ gcc -c -o test2.o test2.c
$ gcc -o test main.o test1.o test2.o
$ ./test
makeを使ってビルドする場合
Makefileというファイルに処理条件を書いておくと、makeと書くだけで適切にビルドが実行される
$ vi Makefile
./configure;make;make installにはどんな意味がある? - ITmedia エンタープライズ
http://www.itmedia.co.jp/help/tips/linux/l0302.html
configure, make, make install とは何か - Qiita
https://qiita.com/chihiro/items/f270744d7e09c58a50a5
Makefileの書き方 - $ cat /var/log/shin
http://shin.hateblo.jp/entry/2012/05/26/231036
makeを使ってソフトウェアをビルドしてみよう:仕事で使える魔法のLAMP(8) - @IT
https://www.atmarkit.co.jp/ait/articles/1106/07/news131.html
Make - ゼロから学ぶ C++
https://rinatz.github.io/cpp-book/make-make/
ヘッダファイルを設ける場合、ファイルの分割は以下のようになる
ビルド方法やMakefileの内容などは同じ
main.c
#@range_begin(rules)
CC = gcc
CFLAGS = -g -Wall
OBJS = main.o test1.o test2.o
test: $(OBJS)
$(CC) $(CFLAGS) -o $@ $(OBJS)
main.o: main.c
$(CC) -c $(CFLAGS) -o $@ $<
test1.o: test1.c
$(CC) -c $(CFLAGS) -o $@ $<
test2.o: test2.c
$(CC) -c $(CFLAGS) -o $@ $<
#@range_end(rules)
$ make
$ ./test
#include <stdio.h>
#include <stdlib.h>
#include "test1.h"
#include "test2.h"
int
main(int argc, char **argv)
{
printf("main\n");
test1();
test2();
exit(0);
}
test1.h
#ifndef __TEST1_H_INCLUDED__
#define __TEST1_H_INCLUDED__
int test1(void);
#endif
test1.c
#include <stdio.h>
#include "test1.h"
int
test1(void)
{
printf("test1\n");
return 1;
}
test2.h
#ifndef __TEST2_H_INCLUDED__
#define __TEST2_H_INCLUDED__
int test2(void);
#endif
test2.c
#include <stdio.h>
#include "test2.h"
int
test2(void)
{
printf("test1\n");
return 1;
}