サンプルプログラム1-3
1-3)sample_103 (データ型のサイズ・最大値・最小値)
データ型(char, short, int, long) のバイトサイズ、signed ,unsignedの最小・最大をヘッダ「limits.h」から表示します。
データ型(float, double、long double) のバイトサイズ、最小・最大を「float.h」から表示します。
参考文献(プログラミング言語C、K&R)の演習2-1と「C言語による はじめてのプログラミングレッスン3」(技術評論社、谷尻豊寿・かおり)を参考にして作成しました。
ここで使用したファイルはこちら。
@ソース・プログラム
// -------------------------------------------------
// sample_103.c
// 2019/03/25 Kimio Nakamura
// -------------------------------------------------
#include <stdio.h>
#include <limits.h>
#include <float.h>
// ===============================================================
// Name : int main(void)
// Usage : メイン関数
// ・char, short, int, long のバイトサイズ、signed ,unsignedの
// 最小・最大をヘッダ「limits.h」から表示する。
// ・float, double、long double のバイトサイズ、最小・最大を
// 「float.h」から表示する
// Parameter:
// Return :
// ===============================================================
int main(void)
{
// char サイズ
char c;
int byte_c;
// short サイズ
short s;
int byte_s;
// int サイズ
int i;
int byte_i;
// long サイズ
long l;
int byte_l;
float f;
int byte_f;
double d;
int byte_d;
long double ld;
int byte_ld;
byte_c = sizeof(c);
byte_s = sizeof(s);
byte_i = sizeof(i);
byte_l = sizeof(l);
byte_f = sizeof(f);
byte_d = sizeof(d);
byte_ld = sizeof(ld);
printf("type size signed unsigned\n");
printf("---------------------------------------------------------------------\n");
printf("char %2d %12d - %12d %12u - %12u\n",byte_c, SCHAR_MIN,
SCHAR_MAX , 0, UCHAR_MAX);
printf("short %2d %12d - %12d %12u - %12u\n",byte_s, SHRT_MIN, SHRT_MAX ,
0, USHRT_MAX);
printf("int %2d %12ld - %12ld %12lu - %12lu\n",byte_i,INT_MIN, INT_MAX ,
0L ,UINT_MAX);
printf("long %2d %12ld - %12ld %12lu - %12lu\n",byte_l,LONG_MIN, LONG_MAX,
0L ,ULONG_MAX );
printf("---------------------------------------------------------------------\n\n");
printf(" type size\n");
printf("---------------------------------------------------------------------\n");
printf(" float %2d %e - %e\n",byte_f, FLT_MIN, FLT_MAX );
printf(" double %2d %e - %e\n",byte_d, DBL_MIN, DBL_MAX );
printf("long double %2d %Le - %Le\n",byte_ld, LDBL_MIN, LDBL_MAX );
printf("---------------------------------------------------------------------\n");
return 0;
}
Aコンパイル結果(BCC32C)

B実行結果

 |