Types C
Après "endianness" encore un petit programme C permettant de déterminer la taille des types standards C.
Un programme qui affiche les informations sur la taille des entiers (pour les compilateurs conformes C99) :
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
volatile int char_min = CHAR_MIN;
int main(void)
{
/* _Bool */
(void)printf("Size of Boolean type is %d byte(s)\n",
(int)sizeof(_Bool));
/* Character type */
(void)printf("Number of bits in a Character= %d\n",
CHAR_BIT);
(void)printf("Default char is ");
if (char_min < 0)
(void)printf("signed\n");
else if (char_min == 0)
(void)printf("unsigned\n");
else
(void)printf("non-standard\n");
/* Integer types */
(void)printf("Size of Char types:\n"
"\t- char type= %d octet(s)\n"
"\t- signed char min= %d\n"
"\t- signed char max= %d\n"
"\t- unsigned char max= %u\n",
(int)sizeof(char), SCHAR_MIN, SCHAR_MAX, (unsigned int)UCHAR_MAX);
(void)printf("Size of Short types:\n"
"\t- short int type= %d octets\n"
"\t- signed short min= %d\n"
"\t- signed short max= %d\n"
"\t- unsigned short max= %u\n",
(int)sizeof(short), SHRT_MIN, SHRT_MAX, (unsigned int)USHRT_MAX);
(void)printf("Size of Int types:\n"
"\t- int type= %d octets\n"
"\t- signed int min= %d\n"
"\t- signed int max= %d\n"
"\t- unsigned int max= %u\n",
(int)sizeof(int), INT_MIN, INT_MAX, (unsigned int)UINT_MAX);
(void)printf("Size of Long types:\n"
"\t- long type= %d octets\n"
"\t- signed int min= %ld\n"
"\t- signed int max= %ld\n"
"\t- unsigned int max= %lu\n",
(int)sizeof(long), LONG_MIN, LONG_MAX, ULONG_MAX);
(void)printf("Size of Long Long types:\n"
"\t- long long type= %d octets\n"
"\t- signed long long min= %lld\n"
"\t- signed long long max= %lld\n"
"\t- unsigned long long max= %llu\n",
(int)sizeof(long long), LLONG_MIN, LLONG_MAX, ULLONG_MAX);
return EXIT_SUCCESS;
}
Si votre compilateur se plaint de “_Bool” ou “long long” lors de la compilation, alors il ne supporte pas les types C99.
Ci-dessous un programme compatible avec tout les compilateurs :
#include <stdio.h>
#include <limits.h>
volatile int char_min = CHAR_MIN;
int main(void)
{
/* Character type */
printf("Number of bits in a Character= %d\n",
CHAR_BIT);
printf("Default char is ");
if (char_min < 0)
printf("signed\n");
else if (char_min == 0)
printf("unsigned\n");
else
printf("non-standard\n");
/* Integer types */
printf("Size of Char types:\n"
"\t- char type= %d octet(s)\n"
"\t- signed char min= %d\n"
"\t- signed char max= %d\n"
"\t- unsigned char max= %u\n",
(int)sizeof(char), SCHAR_MIN, SCHAR_MAX, (unsigned int)UCHAR_MAX);
printf("Size of Short types:\n"
"\t- short int type= %d octets\n"
"\t- signed short min= %d\n"
"\t- signed short max= %d\n"
"\t- unsigned short max= %u\n",
(int)sizeof(short), SHRT_MIN, SHRT_MAX, (unsigned int)USHRT_MAX);
printf("Size of Int types:\n"
"\t- int type= %d octets\n"
"\t- signed int min= %d\n"
"\t- signed int max= %d\n"
"\t- unsigned int max= %u\n",
(int)sizeof(int), INT_MIN, INT_MAX, (unsigned int)UINT_MAX);
printf("Size of Long types:\n"
"\t- long type= %d octets\n"
"\t- signed int min= %ld\n"
"\t- signed int max= %ld\n"
"\t- unsigned int max= %lu\n",
(int)sizeof(long), LONG_MIN, LONG_MAX, ULONG_MAX);
return 0;
}
Et pout les types “Float” on pourra rajouter le bloc suivant :
#include <float.h>
/* Float types */
(void)printf("Size of Float types:\n"
"\t- float type= %d octets\n"
"\t- signed float min= %e\n"
"\t- signed float max= %e\n"
"\t- float epsilon= %e\n",
(int)sizeof(float), FLT_MIN, FLT_MAX, FLT_EPSILON);
(void)printf("Size of Double types:\n"
"\t- double type= %d octets\n"
"\t- signed double min= %le\n"
"\t- signed double max= %le\n"
"\t- double espsilon= %le\n",
(int)sizeof(double), DBL_MIN, DBL_MAX, DBL_EPSILON);
(void)printf("Size of Long Double types:\n"
"\t- long double type= %d octets\n",
(int)sizeof(long double));