3
5

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 3 years have passed since last update.

C言語、C++言語で型のサイズを確認する方法

Last updated at Posted at 2017-11-01

背景

CやC++でFPGAやハードウェアを制御する場合、まずは自分の環境でデータの型を確認した方が良い。そういう場合のちょっとしたコードです。

サンプルコード

どこでもよいので、下記を挿入する。

  double *dbl_pnt;
  int    *int_pnt;
  long    *long_pnt;
  long long  *longlong_pnt;
  printf("> Basic data type:\n");
  printf("  sizeof (char)     = %d\n", sizeof(char));
  printf("  sizeof (uchar)    = %d\n", sizeof(unsigned char));
  printf("  sizeof (short)    = %d\n", sizeof(short));
  printf("  sizeof (ushort)   = %d\n", sizeof(unsigned short));
  printf("  sizeof (int)      = %d\n", sizeof(int));
  printf("  sizeof (*int)     = %d\n", sizeof(int_pnt));
  printf("  sizeof (float)    = %d\n", sizeof(float));
  printf("  sizeof (double)   = %d\n", sizeof(double));
  printf("  sizeof (*double)  = %d\n", sizeof(dbl_pnt));

  printf("  sizeof (long)      = %d\n", sizeof(long));
  printf("  sizeof (*long)     = %d\n", sizeof(long_pnt));

  printf("  sizeof (long long )      = %d\n", sizeof(long long ));
  printf("  sizeof (*long long )     = %d\n", sizeof(longlong_pnt));

出力結果は、

> Basic data type:
  sizeof (char)     = 1
  sizeof (uchar)    = 1
  sizeof (short)    = 2
  sizeof (ushort)   = 2
  sizeof (int)      = 4
  sizeof (*int)     = 8
  sizeof (float)    = 4
  sizeof (double)   = 8
  sizeof (*double)  = 8
  sizeof (long)      = 8
  sizeof (*long)     = 8
  sizeof (long long )      = 8
  sizeof (*long long )     = 8

上記の結果は、下記の環境で取得された。

> gcc -v                                  
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 7.3.0 (clang-703.0.31)
Target: x86_64-apple-darwin15.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

参考資料

  • 3次元磁気流体シミュレーションコード pluto の main.c
3
5
6

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
3
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?