C言語環境構築

This commit is contained in:
ry.yamafuji 2025-12-07 19:11:11 +09:00
parent 2e84f898eb
commit 8802b4fa27
4 changed files with 39 additions and 2 deletions

View File

@ -2,7 +2,7 @@ CC = gcc
CFLAGS = -Wall -Wextra -O2 CFLAGS = -Wall -Wextra -O2
TARGET = dist/hello TARGET = dist/hello
SRC = src/hello.c SRC = src/c/hello.c
$(TARGET): $(SRC) $(TARGET): $(SRC)
$(CC) $(CFLAGS) $(SRC) -o $(TARGET) $(CC) $(CFLAGS) $(SRC) -o $(TARGET)

View File

@ -1 +1,4 @@
# C言語について # C / C++言語について
* コンパイラ: GCC / G++
* ビルド管理: Makefile

34
src/c/cp.c Normal file
View File

@ -0,0 +1,34 @@
#include <stdio.h>
int main(int argc, char *argv[]) {
// 引数のチェック 3以外ならエラー
if (argc != 3) {
fprintf(stderr, "Usage: %s src dest\n", argv[0]);
return 1;
}
// ファイルのオープン
FILE *src = fopen(argv[1], "rb");
FILE *dst = fopen(argv[2], "wb");
// オープン失敗時のエラーチェック
if (!src || !dst) {
perror("File open error");
return 1;
}
// ファイルのコピー 4096バイトずつ書き込む
char buf[4096];
size_t n;
// 読み込みと書き込みのループ
while ((n = fread(buf, 1, sizeof(buf), src)) > 0) {
fwrite(buf, 1, n, dst);
}
fclose(src);
fclose(dst);
return 0;
}