Loose-Info.com
Last Update 2023/04/15
TOP - 各種テスト - C言語 - 型

テスト概要

その1
整数型

その2
浮動小数点型

その3
型char

その4
型void


その1

整数型

sample.c
#include <stdio.h> #include <stdlib.h> #include <limits.h> int main(void) { printf("各種整数型および範囲(出力は実装依存値)\n\n"); _Bool b_max = 1; printf("_Bool\n"); printf(" max = %d\n", b_max); printf(" sizeof(_Bool) = %ld\n\n", sizeof(_Bool)); signed char schar_max = SCHAR_MAX; signed char schar_min = SCHAR_MIN; printf("signed char\n"); printf(" max = %d\n", schar_max); printf(" min = %d\n", schar_min); printf(" sizeof(signed char) = %ld\n\n", sizeof(signed char)); unsigned char uschar_max = UCHAR_MAX; printf("unsigned char\n"); printf(" max = %d\n", uschar_max); printf(" sizeof(unsigned char) = %ld\n\n", sizeof(unsigned char)); signed short int sint_max = SHRT_MAX; signed short int sint_min = SHRT_MIN; printf("signed short int\n"); printf(" max = %d\n", sint_max); printf(" min = %d\n", sint_min); printf(" sizeof(signed short int) = %ld\n", sizeof(signed short int)); printf(" sizeof(short) = %ld\n", sizeof(short)); printf(" sizeof(short int) = %ld\n", sizeof(short int)); printf(" sizeof(signed short) = %ld\n\n", sizeof(signed short)); unsigned short int usint_max = USHRT_MAX; printf("unsigned short int\n"); printf(" max = %d\n", usint_max); printf(" sizeof(unsigned short int) = %ld\n", sizeof(unsigned short int)); printf(" sizeof(unsigned short) = %ld\n\n", sizeof(unsigned short)); signed int int_max = INT_MIN; signed int int_min = INT_MAX; printf("signed int\n"); printf(" max = %d\n", int_max); printf(" min = %d\n", int_min); printf(" sizeof(signed int) = %ld\n", sizeof(signed int)); printf(" sizeof(int) = %ld\n", sizeof(int)); printf(" sizeof(signed) = %ld\n\n", sizeof(signed)); unsigned int usgint_max = UINT_MAX; printf("unsigned int\n"); printf(" max = %u\n", usgint_max); printf(" sizeof(unsigned int) = %ld\n", sizeof(unsigned int)); printf(" sizeof(unsigned) = %ld\n\n", sizeof(unsigned)); signed long int slint_max = LONG_MAX; signed long int slint_min = LONG_MIN; printf("signed long int\n"); printf(" max = %ld\n", slint_max); printf(" min = %ld\n", slint_min); printf(" sizeof(signed long int) = %ld\n", sizeof(signed long int)); printf(" sizeof(long) = %ld\n", sizeof(long)); printf(" sizeof(long int) = %ld\n", sizeof(long int)); printf(" sizeof(signed long) = %ld\n\n", sizeof(signed long)); unsigned long int usglint_max = ULONG_MAX; printf("unsigned long int\n"); printf(" max = %lu\n", usglint_max); printf(" sizeof(unsigned long int) = %ld\n", sizeof(unsigned long int)); printf(" sizeof(unsigned long) = %ld\n\n", sizeof(unsigned long)); signed long long int sllint_max = LLONG_MAX; signed long long int sllint_min = LLONG_MIN; printf("signed long long int\n"); printf(" max = %lld\n", sllint_max); printf(" min = %lld\n", sllint_min); printf(" sizeof(signed long long int) = %ld\n", sizeof(signed long long int)); printf(" sizeof(long long) = %ld\n", sizeof(long long)); printf(" sizeof(long long int) = %ld\n", sizeof(long long int)); printf(" sizeof(signed long long) = %ld\n\n", sizeof(signed long long)); unsigned long long int usgllint_max = ULLONG_MAX; printf("unsigned long long int\n"); printf(" max = %llu\n", usgllint_max); printf(" sizeof(unsigned long long int) = %ld\n", sizeof(unsigned long long int)); printf(" sizeof(unsigned long long) = %ld\n\n", sizeof(unsigned long long)); return EXIT_SUCCESS; }

実行結果
$ gcc -Wall sample.c $ ./a.out 各種整数型および範囲(出力は実装依存値) _Bool max = 1 sizeof(_Bool) = 1 signed char max = 127 min = -128 sizeof(signed char) = 1 unsigned char max = 255 sizeof(unsigned char) = 1 signed short int max = 32767 min = -32768 sizeof(signed short int) = 2 sizeof(short) = 2 sizeof(short int) = 2 sizeof(signed short) = 2 unsigned short int max = 65535 sizeof(unsigned short int) = 2 sizeof(unsigned short) = 2 signed int max = -2147483648 min = 2147483647 sizeof(signed int) = 4 sizeof(int) = 4 sizeof(signed) = 4 unsigned int max = 4294967295 sizeof(unsigned int) = 4 sizeof(unsigned) = 4 signed long int max = 9223372036854775807 min = -9223372036854775808 sizeof(signed long int) = 8 sizeof(long) = 8 sizeof(long int) = 8 sizeof(signed long) = 8 unsigned long int max = 18446744073709551615 sizeof(unsigned long int) = 8 sizeof(unsigned long) = 8 signed long long int max = 9223372036854775807 min = -9223372036854775808 sizeof(signed long long int) = 8 sizeof(long long) = 8 sizeof(long long int) = 8 sizeof(signed long long) = 8 unsigned long long int max = 18446744073709551615 sizeof(unsigned long long int) = 8 sizeof(unsigned long long) = 8

その2

浮動小数点型

sample.c
#include <stdio.h> #include <stdlib.h> #include <float.h> int main(void) { printf("各種浮動小数点型および範囲(出力は実装依存値)\n\n"); float f_max = FLT_MAX; float f_min = FLT_MIN; printf("float\n"); printf(" max = %e\n", f_max); printf(" min = %e\n", f_min); printf(" sizeof(float) = %ld\n\n", sizeof(float)); double d_max = DBL_MAX; double d_min = DBL_MIN; printf("double\n"); printf(" max = %e\n", d_max); printf(" min = %e\n", d_min); printf(" sizeof(double) = %ld\n\n", sizeof(double)); long double l_max = LDBL_MAX; long double l_min = LDBL_MIN; printf("long double\n"); printf(" max = %Le\n", l_max); printf(" min = %Le\n", l_min); printf(" sizeof(long double) = %ld\n\n", sizeof(long double)); return EXIT_SUCCESS; }

実行結果
$ gcc -Wall sample.c $ ./a.out 各種浮動小数点型および範囲(出力は実装依存値) float max = 3.402823e+38 min = 1.175494e-38 sizeof(float) = 4 double max = 1.797693e+308 min = 2.225074e-308 sizeof(double) = 8 long double max = 1.189731e+4932 min = 3.362103e-4932 sizeof(long double) = 16

その3

型char

sample.c
#include <stdio.h> #include <stdlib.h> #include <limits.h> int main(void) { printf("型charおよび範囲(出力は実装依存値)\n\n"); char char_max = CHAR_MAX; char char_min = CHAR_MIN; printf("char\n"); printf(" max = %d\n", char_max); printf(" min = %d\n", char_min); printf(" sizeof(char) = %ld\n\n", sizeof(char)); return EXIT_SUCCESS; }

実行結果
$ ./a.out 型charおよび範囲(出力は実装依存値) char max = 127 min = -128 sizeof(char) = 1

その4

型void

sample.c
#include <stdio.h> #include <stdlib.h> /* 引数、戻り値が共に型voidである関数 */ void sampfunc1(void) { printf("sampfunc1\n"); } int sampfunc2() { printf("sampfunc2\n"); return 2; } int main(void) { /* 引数を伴い戻り値を利用する関数の呼び出し */ printf("sampfunc1 = %d\n", sampfunc1(1)); printf("sampfunc2 = %d\n", sampfunc2(2)); return EXIT_SUCCESS; }

実行結果
$ gcc -Wall sample.c <--- サンプルコードをコンパイル sample.c: In function ‘main’: sample.c:20:36: error: too many arguments to function ‘sampfunc1’ <--- 引数の数に関するエラー 20 | printf("sampfunc1 = %d\n", sampfunc1(1)); | ^~~~~~~~~ sample.c:5:6: note: declared here 5 | void sampfunc1(void) | ^~~~~~~~~ sample.c:20:36: error: invalid use of void expression <--- void式の不正使用に関するエラー 20 | printf("sampfunc1 = %d\n", sampfunc1(1)); | ^~~~~~~~~~~~

sample2.c
#include <stdio.h> #include <stdlib.h> int main(void) { unsigned int uint1 = 0xFFFFFFFF; unsigned int uint2 = 0x10FFFF10; unsigned int *p1_uint1 = &uint1; unsigned int *p1_uint2 = &uint2; /* 型キャスト無しでvoidへのポインタに変換 */ void *p_void = p1_uint2; /* 型キャスト無しでunsigned charへのポインタに変換 */ unsigned char *p2_uc1 = p1_uint1; /* 型キャスト無しでunsigned charへのポインタに変換 */ unsigned char *p2_uc2 = p_void; printf("%X %X\n", *p2_uc1, *p2_uc2); for (int i=0; i<4; i++) { /* voidポインタを使用した演算 */ p2_uc2 = p_void + i; printf("%X ", *p2_uc2); } printf("\n"); return EXIT_SUCCESS; }

実行結果
$ gcc -Wall sample2.c sample2.c: In function ‘main’: sample2.c:16:33: warning: initialization of ‘unsigned char *’ from incompatible pointer type ‘unsigned int *’ [-Wincompatible-pointer-types] 16 | unsigned char *p2_uc1 = p1_uint1; <--- voidポインタを介さない場合の互換性の無いポインタに関する警告 | ^~~~~~~~ $ ./a.out FF 10 10 FF FF 10

実行環境

GNU bash, version 5.1.16
GCC-12.2.0
GNU C Library 2.36
GNU Binutils 2.39


コード例・出力内容中の表記

・実行例中の太字表記部分は、コマンドなどの入力された文字列を示します。
・「」や「...」の着色省略表記は、 実際のソースコードや出力内容などを省略加工した部分を示します。