0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

C言語のmake時に構造体のサイズをチェック

Posted at
  1. 構造体の定義: まず、チェックしたい構造体を定義します。
  2. サイズを出力するコードの追加: プログラム内で、sizeof演算子を使用して構造体のサイズを取得し、それを出力するコードを追加します。
  3. Makefileの編集: makeを使用してビルドする際、サイズチェックのためのターゲットをMakefileに追加することができます。
#include <stdio.h>
#include <stdlib.h>

typedef struct {
    int a;
    double b;
    char c;
} MyStruct;

#define EXPECTED_SIZE 24

int main() {
    size_t size = sizeof(MyStruct);
    if (size != EXPECTED_SIZE) {
        fprintf(stderr, "Error: Size of MyStruct is %lu, but expected %d\n", size, EXPECTED_SIZE);
        return EXIT_FAILURE; // 非ゼロのステータスコードで終了
    }
    printf("Size of MyStruct: %lu\n", size);
    return EXIT_SUCCESS; // ゼロのステータスコードで終了
}
all: check_size

check_size:
    gcc -o check_size size_check.c
    ./check_size
0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?