Hello,
See comments for details.
// *** Begin of Main.cpp ***
#include <stdio.h>
typedef char int8;
typedef short int int16;
typedef int int32;
typedef long long int64;
typedef unsigned char uint8;
typedef unsigned short uint16;
typedef unsigned int uint32;
typedef unsigned long long uint64;
/*
Skybuck Flying says GoodBye to Delphi.
Skybuck Flying says Hello to C/C++
Back to School with Skybuck Flying:
Lesson 1, Integer basics.
Conclusions:
Out of range detection for int8 ok.
Out of range detection for uint8 ok.
Out of range detection for int16 ok.
Out of range detection for uint16 ok.
Out of range detection for int32 BAD.
Out of range detection for uint32 ok.
Out of range detection for int64 BAD.
Out of range detection for uint64 ok.
Legal range for int8 ok.
Legal range for uint8 ok.
Legal range for int16 ok.
Legal range for uint16 ok.
Legal range for int32 BAD.
Legal range for uint32 ok.
Legal range for int64 BAD.
Legal range for uint64 ok.
Range detection is done at compile, warnings are displayed in the output
window.
No runtime checking.
Out of range situations can produce multiple warnings.
Trying to set a valid maximum negative value for int32 and int64 also
gives
problems/warnings.
Platform tested intel architecture 32.
12 out of 16 tests passed.
4 out of 16 tests failed.
Score:
7.5 out of 10.
Microsoft's Visual Studio 2008 Professional Edition programmers get to go
to
next lesson with Skybuck :)
*/
int main()
{
printf("Program Started \n\n");
int8 vMinInt8;
int8 vMaxInt8;
uint8 vMinUint8;
uint8 vMaxUint8;
int16 vMinInt16;
int16 vMaxInt16;
uint16 vMinUint16;
uint16 vMaxUint16;
int32 vMinInt32;
int32 vMaxInt32;
uint32 vMinUint32;
uint32 vMaxUint32;
int64 vMinInt64;
int64 vMaxInt64;
uint64 vMinUint64;
uint64 vMaxUint64;
// test int8
printf("sizeof(int8): %d \n", sizeof(int8) );
vMinInt8 = -128;
printf("vMinInt8: %d \n", vMinInt8 );
vMaxInt8 = 127;
printf("vMaxInt8: %d \n\n", vMaxInt8 );
// test uint8
printf("sizeof(uint8): %d \n", sizeof(uint8) );
vMinUint8 = 0; // -1 would give warning: signed/unsigned mismatch
printf("vMinUint8: %u \n", vMinUint8 );
vMaxUint8 = 255; // 256 would give warnings: truncation from int to
uint8, and truncation of constant value
printf("vMaxUint8: %u \n\n", vMaxUint8 );
// test int16
printf("sizeof(int16): %d \n", sizeof(int16) );
vMinInt16 = -32768; // -32769 would give warning: truncation from int
to
int16
printf("vMinInt16: %d \n", vMinInt16 );
vMaxInt16 = 32767; // 32768 would give warning: truncation of constant
value
printf("vMaxInt16: %d \n\n", vMaxInt16 );
// test uint16
printf("sizeof(uint16): %d \n", sizeof(uint16) );
vMinUint16 = 0; // -1 would give warning: signed/unsigned mismatch
printf("vMinUint16: %u \n", vMinUint16 );
vMaxUint16 = 65535; // 65536 would give warnings: truncation from int
to
uint16, and truncation of constant value
printf("vMaxUint16: %u \n\n", vMaxUint16 );
// test int32
printf("sizeof(int32): %d \n", sizeof(int32) );
// removing the minus solves the warning for now.
vMinInt32 = 2147483648; // -2147483648 still gives warning: unary
minus
operator applied to unsigned type, result still unsigned
// little problem with negation probably
printf("vMinInt32: %d \n", vMinInt32 );
vMaxInt32 = 2147483647; // 2147483648 or any other out of range value
gives no warning what so ever ?!?!
printf("vMaxInt32: %d \n\n", vMaxInt32 );
// test uint32
printf("sizeof(uint32): %d \n", sizeof(uint32) );
vMinUint32 = 0; // -1 would give warning: conversion from int to
uint32,
signed/unsigned mismatch
printf("vMinUint32: %u \n", vMinUint32 );
vMaxUint32 = 4294967295; // 4294967296 would give warning: truncation
from __int64 to uint32, and truncation of constant value.
printf("vMaxUint32: %u \n\n", vMaxUint32 );
// test int64
printf("sizeof(int64): %ld \n", sizeof(int64) );
// problem zone for maximum negative value and beyond:
// vMinInt64 = -9223372036854775809; // would give warning: unary minus
operator applied to unsigned type, result still unsigned
vMinInt64 = 9223372036854775808; // solves the warning and becomes
negative
printf("vMinInt64: %lld \n", vMinInt64 );
vMaxInt64 = 9223372036854775807; // 9223372036854775808 would give no
warning ?!?!
printf("vMaxInt64: %lld \n\n", vMaxInt64 );
// test uint64
printf("sizeof(uint32): %d \n", sizeof(uint64) );
vMinUint64 = 0; // -1 would give warning: conversion from 'int' to
'uint64', signed/unsigned mismatch
printf("vMinUint64: %llu \n", vMinUint64 );
vMaxUint64 = 18446744073709551615; // 18446744073709551616 would give
warning: constant too big
printf("vMaxUint64: %llu \n\n", vMaxUint64 );
printf("Program finished \n");
}
// *** End of Main.cpp ***
Bye,
Skybuck.


|