はじめに
初級編の前編と後編で、基礎的な文法についてまとめました。
それらを前提とした内容にするため、まだ見てない方はぜひチェックしてみてください☺️
ポインタ
int a = 10;
int* p = &a; // aのアドレスをpに格納
printf("%d\n", *p); // 10(ポインタが指す値)
printf("%p\n", p); // アドレスを表示
*p = 20; // ポインタ経由で値を変更
printf("%d\n", a); // 20
配列とポインタ
int arr[] = {1, 2, 3, 4, 5};
int* p = arr; // 配列の先頭アドレス
for (int i = 0; i < 5; i++) {
printf("%d ", *(p + i));
}
文字列操作
#include
char str1[50] = "Hello";
char str2[] = "World";
printf("%lu\n", strlen(str1)); // 5
strcat(str1, " ");
strcat(str1, str2);
printf("%s\n", str1); // Hello World
ファイル入出力
#include
FILE* fp = fopen("test.txt", "w");
if (fp != NULL) {
fprintf(fp, "Hello, File!\n");
fclose(fp);
}
さいごに
中級編の内容、特にポインタはC言語の特徴的な機能です。しっかり理解しておきましょう☺️
それでは、今回はここまで。ありがとうございました😊