では前回までの流れ(関数リストと丁寧な入門説明)を引き継ぎ、
以下の関数群についても「初心者でも理解できる形式」で続きとしてまとめます。
(構成は:見出し → 目的 → 使い方 → サンプルコード → 補足説明)
<font color="green">メモリ操作関数</font>
🧩 memset(メモリ初期化)
指定した値で、指定範囲のメモリを埋める関数です。
#include <string.h>
#include <stdio.h>
int main() {
char buf10;
memset(buf, 0x00, sizeof(buf)); // 配列をすべて0で初期化
printf("buf0 = %d\n", buf0); // 0 が出力される
return 0;
}
🔸よく使う用途:
構造体や配列の初期化(memset(&a, 0, sizeof(a)); など)
⚠️ 注意:
sizeof(w) などで確保サイズを正しく指定しないと、他の領域を破壊します。
🧩 memcpy(メモリコピー)
指定されたバイト数分のメモリをコピーします。
#include <string.h>
#include <stdio.h>
int main() {
char src[] = "Hello";
char dst10;
memcpy(dst, src, strlen(src) + 1); // +1で終端'\0'もコピー
printf("%s\n", dst);
return 0;
}
💡strcpy は文字列専用、memcpy は任意のデータ型でも使用可能。
🧩 memcmp(メモリ比較)
2つのメモリ領域をバイト単位で比較します。
#include <string.h>
#include <stdio.h>
int main() {
char a[] = "ABC";
char b[] = "ABD";
int result = memcmp(a, b, 3);
if (result == 0)
printf("同じ\n");
else
printf("異なる\n");
return 0;
}
🔹戻り値:
0 → 同じ
正 → a > b
負 → a < b
<font color="green">文字列・環境変数関数</font>
🧩 strcpy(文字列コピー)
※誤記「srtcpy」→「strcpy」です。
#include <string.h>
#include <stdio.h>
int main() {
char src[] = "C Language";
char dst20;
strcpy(dst, src);
printf("%s\n", dst);
return 0;
}
⚠️ 安全に使う場合は strncpy または strcpy_s を推奨。
🧩 strlen(文字列長取得)
#include <string.h>
#include <stdio.h>
int main() {
char str[] = "ABC";
printf("長さ=%zu\n", strlen(str)); // 3
return 0;
}
💡 戻り値は「終端文字 \0 を除いた長さ」です。
🧩 getenv(環境変数を取得)
#include <stdlib.h>
#include <stdio.h>
int main() {
char *path = getenv("PATH");
if (path != NULL)
printf("PATH=%s\n", path);
else
printf("PATHが見つかりません\n");
return 0;
}
💡 OS の設定値を取得できます。
例:USER, HOME, TEMP, PATH など。
🧩 atoi(文字列 → 整数変換)
※誤記「atoI」→「atoi」です。
#include <stdlib.h>
#include <stdio.h>
int main() {
char numStr[] = "123";
int n = atoi(numStr);
printf("%d\n", n);
return 0;
}
⚠️ 不正文字列(例:"abc")の場合は 0 を返す点に注意。
<font color="green">ソート関数</font>
🧩 qsort(汎用クイックソート)
#include <stdio.h>
#include <stdlib.h>
int compare(const void *a, const void *b) {
return (*(int*)a - *(int*)b);
}
int main() {
int arr[] = {5, 1, 4, 2, 3};
size_t n = sizeof(arr) / sizeof(arr0);
qsort(arr, n, sizeof(int), compare);
for (int i = 0; i < n; i++)
printf("%d ", arri);
return 0;
}
💡 ポインタを使った汎用ソートで、構造体配列にも応用可能。
<font color="green">ファイル関数</font>
🧩 fgets(ファイルから1行読み込み)
#include <stdio.h>
int main() {
char line128;
FILE *fp = fopen("sample.txt", "r");
if (fp != NULL) {
while (fgets(line, sizeof(line), fp) != NULL)
printf("%s", line);
fclose(fp);
}
return 0;
}
💡 fgets は \n も含めて読み込む。
🧩 fseek / ftell(ファイル位置操作)
#include <stdio.h>
int main() {
FILE *fp = fopen("test.txt", "r");
if (fp == NULL) return 1;
fseek(fp, 0, SEEK_END); // ファイル末尾へ
long size = ftell(fp); // 現在位置=サイズ
printf("ファイルサイズ = %ld バイト\n", size);
fclose(fp);
return 0;
}
💡
fseek … ファイルポインタの位置を変更
ftell … 現在の位置を取得
<font color="green">フォーマット出力関数</font>
🧩 printf / sprintf
#include <stdio.h>
int main() {
int y=25, m=10, d=15, h=21;
char buf32;
printf("今日の日付: %02d/%02d\n", m, d);
sprintf(buf, "%02d%02d%02d%02d", y, m, d, h);
printf("文字列化=%s\n", buf);
return 0;
}
💡 sprintf は結果を文字列として保存できる。
⚠️ バッファオーバーフロー対策として snprintf を推奨。
<font color="green">syslog(システムログ出力)</font>
#include <syslog.h>
#include <stdio.h>
int main() {
openlog("MyApp", LOG_PID, LOG_USER);
syslog(LOG_INFO | LOG_AUTH, "アプリケーション起動");
closelog();
return 0;
}
💡 Linux / BSD系で使用。
Windows環境では EventLog が相当機能。
<font color="green">アドレスマクロ</font>
🧩 __FILE__ / __LINE__
現在のソースファイル名と行番号を自動で展開します。
#include <stdio.h>
int main() {
printf("位置情報: %s:%d\n", __FILE__, __LINE__);
return 0;
}
💡 デバッグログなどで使うと便利。
例:printf("%s:%d エラー\n", __FILE__, __LINE__);
次の章では、これらを組み合わせた
「メモリ・ファイル・ログを連携する小型ユーティリティ例」 を紹介する形に続けましょうか?