Loading lang_c_03_types...
#ifndef TYPEPROPERTIES_H #define TYPEPROPERTIES_H
void displaySize(void);
#endif // TYPEPROPERTIES_H
#include "typeProperties.h" #include <stdio.h>
void displaySize(void) { printf("\n~~~~ displaySize() ~~~~\n"); }
#include <stdio.h> #include "typeProperties.h"
int main(void) { displaySize(); return 0; }
... { taille_du_type_choisi=sizeof(nom_d_un_type); taille_du_type_de_l_expression=sizeof(une_expression); ... }
... { printf("sizeof(int) = %d\n", sizeof(int)); ... }
<stddef.h>), désignant un entier non-signé (unsigned) capable de représenter la taille d'une donnée quelconque tenant dans la mémoire de la machine✍
... { printf("sizeof(size_t) = %d\n", (int)sizeof(size_t)); printf("sizeof(int) = %d\n", (int)sizeof(int) ); ... }
<stdbool.h>est nécessaire pour ce type.
sizeof(char)==1
sizeof(char)<=sizeof(short)<=sizeof(int)<=sizeof(long)<=sizeof(long long)
sizeof(short)>=2,
sizeof(int)>=2,
sizeof(long)>=4,
sizeof(long long)>=8✍
<stdint.h>fournit des types (par l'usage de typedef) dont le nom assure le respect de la taille annoncée sur la plateforme d'exécution visée.
void intOverflow(int limit) { printf("\n~~~~ intOverflow(%d) ~~~~\n", limit); for(int i=0; i<limit; ++i) { uint16_t other=(uint16_t)i; if(other!=i) { printf("%d has been turned into %d\n", i, other); break; } } }
<limits.h>.
void tributeToChuck(void) { printf("\n~~~~ tributeToChuck() ~~~~\n"); typedef uint32_t TestInt; int loopCount=0; TestInt prev=0; for(TestInt i=0; ; ++i) { if(i<prev) { ++loopCount; printf("Chuck Norris counted to infinity - "); switch(loopCount) { case 1: { printf("once\n"); break; } case 2: { printf("twice\n"); break; } default: { printf("%d times\n", loopCount); break; } } if(loopCount>2) { printf("then the numbers broke down!\n"); break; } } prev=i; } }
void intComputation(int a, int b, int c) { printf("\n~~~~ intComputation(%d, %d, %d) ~~~~\n", a, b, c); printf("a*b/c = %d\n", a*b/c); }
... { ... for(int i=1; i<=1000000; i*=10) { intComputation(2*i, 3*i, 4*i); } ... }
... { ... int64_t wideA=a; int64_t wideB=b; int64_t wideC=c; int64_t wideResult=wideA*wideB/wideC; int result=(int)wideResult; printf("(int)(wideA*wideB/wideC) = %d\n", result); }
void intPromotion(void) { printf("\n~~~~ intPromotion() ~~~~\n"); int8_t a8=3, b8=7, c8=(int8_t)(a8+b8); printf("sizeof(c8)=%d sizeof(a8+b8)=%d\n", (int)sizeof(c8), (int)sizeof(a8+b8)); }
void realRounding(double step) { printf("\n~~~~ realRounding(%g) ~~~~\n", step); double three=3.0; for(double d=0.0; d<6.0; d+=step) { printf("d=%g\tdelta=%g\n", d, d-three); if(d==three) { printf("found three!\n"); break; } } }
void realLimits(float step) { printf("\n~~~~ realAccumulation(%g) ~~~~\n", step); printf("FLT_MIN = %g\n", FLT_MIN); printf("FLT_MAX = %g\n", FLT_MAX); printf("DBL_MIN = %g\n", DBL_MIN); printf("DBL_MAX = %g\n", DBL_MAX); float accum=0.0f; for(int i=0; ; ++i) { float prev=accum; accum+=step; if(prev==accum) { printf("stalled after %d steps (%g)\n", i, accum); break; } } }
<float.h>nous indiquent les limites des types float et double,
double computeLog(double x) { double s=(x-1.0)/(x+1.0); double t=2.0*s; double y=t; double s2=s*s; for(int k=1; ; ++k) { t*=s2; double delta=t/(2*k+1); double prev=y; y+=delta; if(prev==y) { // printf("x=%g k=%d y=%g delta=%g\n", x, k, y, delta); break; } } return y; }
//----------------------------------------------------------------------------
#ifndef TYPEPROPERTIES_H #define TYPEPROPERTIES_H
void displaySize(void);
void intOverflow(int limit);
void tributeToChuck(void);
void intComputation(int a, int b, int c);
void intPromotion(void);
void realRounding(double step);
void realLimits(float step);
double computeLog(double x);
#endif // TYPEPROPERTIES_H
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#include "typeProperties.h"
#include <stdio.h> #include <stdbool.h> #include <stdint.h> #include <limits.h> #include <float.h>
void displaySize(void) { printf("\n~~~~ displaySize() ~~~~\n"); printf("sizeof(size_t) = %d\n", (int)sizeof(size_t) ); printf("sizeof(bool) = %d\n", (int)sizeof(bool) ); printf("sizeof(char) = %d\n", (int)sizeof(char) ); printf("sizeof(short) = %d\n", (int)sizeof(short) ); printf("sizeof(int) = %d\n", (int)sizeof(int) ); printf("sizeof(long) = %d\n", (int)sizeof(long) ); printf("sizeof(long long) = %d\n", (int)sizeof(long long) ); printf("sizeof(float) = %d\n", (int)sizeof(float) ); printf("sizeof(double) = %d\n", (int)sizeof(double) ); printf("sizeof(long double) = %d\n", (int)sizeof(long double)); printf("sizeof(int8_t) = %d\n", (int)sizeof(int8_t) ); printf("sizeof(int16_t) = %d\n", (int)sizeof(int16_t) ); printf("sizeof(int32_t) = %d\n", (int)sizeof(int32_t) ); printf("sizeof(int64_t) = %d\n", (int)sizeof(int64_t) ); printf("sizeof(uint8_t) = %d\n", (int)sizeof(uint8_t) ); printf("sizeof(uint16_t) = %d\n", (int)sizeof(uint16_t) ); printf("sizeof(uint32_t) = %d\n", (int)sizeof(uint32_t) ); printf("sizeof(uint64_t) = %d\n", (int)sizeof(uint64_t) ); }
void intOverflow(int limit) { printf("\n~~~~ intOverflow(%d) ~~~~\n", limit); for(int i=0; i<limit; ++i) { // uint16_t other=(uint16_t)i; int16_t other=(int16_t)i; if(other!=i) { printf("%d has been turned into %d\n", i, other); break; } } printf("UINT16_MAX = %d\n", UINT16_MAX); printf("INT16_MIN = %d\n", INT16_MIN); printf("INT16_MAX = %d\n", INT16_MAX); printf("INT_MIN = %d\n", INT_MIN); printf("INT_MAX = %d\n", INT_MAX); printf("CHAR_MIN = %d\n", CHAR_MIN); printf("CHAR_MAX = %d\n", CHAR_MAX); }
void tributeToChuck(void) { printf("\n~~~~ tributeToChuck() ~~~~\n"); // typedef uint32_t TestInt; typedef uint16_t TestInt; int loopCount=0; TestInt prev=0; for(TestInt i=0; ; ++i) { if(i<prev) { ++loopCount; printf("Chuck Norris counted to infinity - "); switch(loopCount) { case 1: { printf("once\n"); break; } case 2: { printf("twice\n"); break; } default: { printf("%d times\n", loopCount); break; } } if(loopCount>2) { printf("then the numbers broke down!\n"); break; } } prev=i; } }
void intComputation(int a, int b, int c) { printf("\n~~~~ intComputation(%d, %d, %d) ~~~~\n", a, b, c); printf("a*b/c = %d\n", a*b/c); printf("a*(b/c) = %d\n", a*(b/c)); printf("b/c = %d\n", b/c); printf("a*b = %d\n", a*b); int64_t wideA=(int64_t)a; int64_t wideB=(int64_t)b; int64_t wideC=(int64_t)c; int64_t wideResult=wideA*wideB/wideC; int result=(int)wideResult; printf("(int)(wideA*wideB/wideC) = %d\n", result); }
void intPromotion(void) { printf("\n~~~~ intPromotion() ~~~~\n"); int8_t a8=3, b8=7, c8=(int8_t)(a8+b8); printf("sizeof(c8)=%d sizeof(a8+b8)=%d\n", (int)sizeof(c8), (int)sizeof(a8+b8)); int16_t a16=3, b16=7, c16=(int16_t)(a16+b16); printf("sizeof(c16)=%d sizeof(a16+b16)=%d\n", (int)sizeof(c16), (int)sizeof(a16+b16)); int32_t a32=3, b32=7, c32=a32+b32; printf("sizeof(c32)=%d sizeof(a32+b32)=%d\n", (int)sizeof(c32), (int)sizeof(a32+b32)); int64_t a64=3, b64=7, c64=a64+b64; printf("sizeof(c64)=%d sizeof(a64+b64)=%d\n", (int)sizeof(c64), (int)sizeof(a64+b64)); }
void realRounding(double step) { printf("\n~~~~ realRounding(%g) ~~~~\n", step); double three=3.0; for(double d=0.0; d<6.0; d+=step) { printf("d=%g\tdelta=%g\n", d, d-three); if(d==three) { printf("found three!\n"); break; } } }
void realLimits(float step) { printf("\n~~~~ realAccumulation(%g) ~~~~\n", step); printf("FLT_MIN = %g\n", FLT_MIN); printf("FLT_MAX = %g\n", FLT_MAX); printf("DBL_MIN = %g\n", DBL_MIN); printf("DBL_MAX = %g\n", DBL_MAX); float accum=0.0f; for(int i=0; ; ++i) { float prev=accum; accum+=step; if(prev==accum) { printf("stalled after %d steps (%g)\n", i, accum); break; } } }
double computeLog(double x) { double s=(x-1.0)/(x+1.0); double t=2.0*s; double y=t; double s2=s*s; for(int k=1; ; ++k) { t*=s2; double delta=t/(2*k+1); double prev=y; y+=delta; if(prev==y) { // printf("x=%g k=%d y=%g delta=%g\n", x, k, y, delta); break; } } return y; }
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#include <stdio.h> #include "typeProperties.h"
int main(void) { displaySize();
intOverflow(10000); intOverflow(100000);
tributeToChuck();
for(int i=1; i<=1000000; i*=10) { intComputation(2*i, 3*i, 4*i); }
intPromotion();
realRounding(0.375); realRounding(0.3);
realLimits(0.125f); realLimits(0.1f); realLimits(0.5f); realLimits(100.0f); realLimits(512.0f);
printf("\n"); for(double x=0.2; x<3.0; x+=0.2) { printf("computeLog(%g)=%g\n", x, computeLog(x)); }
return 0; }
//----------------------------------------------------------------------------