====Data Types Programme====
The goal of this programme was for us to write a programme that could, when compiled on a given system, tell us certain things about the system that are not necessarily portable, such as the size and ranges of the built in data types for the local C compiler (CC).
Below is the source code...
===main.cpp===
#include
#include
#include
int main(){
bool b = false;
std::cout << " Size of bool: " << sizeof(bool) << "B" << std::endl;
std::cout << " Lower bound of bool: " << b << std::endl;
std::cout << " Upper bound of bool: " << (bool)(b - 1) << std::endl << std::endl;
signed char sc = 0; signed char sc2 = 0;
std::cout << " Size of signed char: " << sizeof(sc) << "B" << std::endl;
sc2 = (unsigned char)(sc - 1)/2 + 1;
std::cout << " Lower bound of signed char: " << (signed int)(sc2) << std::endl;
sc2 = (unsigned char)(sc - 1)/2;
std::cout << " Upper bound of signed char: " << (signed int)(sc2) << std::endl << std::endl;
unsigned char uc = 0;
std::cout << " Size of unsigned char: " << sizeof(uc) << "B" << std::endl;
std::cout << " Lower bound of unsigned char: " << (unsigned int)(uc) << std::endl;
std::cout << " Upper bound of unsigned char: " << (unsigned int)((unsigned char)(uc -1)) << std::endl << std::endl;
signed short int ssi = 0; signed short int ssi2 = 0;
std::cout << " Size of short signed int: " << sizeof(ssi) << "B" << std::endl;
ssi2 = (unsigned short int)(ssi - 1)/2 + 1;
std::cout << " Lower bound of signed short int: " << ssi2 << std::endl;
ssi2 = (unsigned short int)(ssi - 1)/2;
std::cout << " Upper bound of signed short int: " << ssi2 << std::endl << std::endl;
unsigned short int usi = 0;
std::cout << " Size of unsigned short int: " << sizeof(usi) << "B" << std::endl;
std::cout << "Lower bound of unsigned short int: " << usi << std::endl;
std::cout << "Upper bound of unsigned short int: " << usi - 1 << std::endl << std::endl;
signed int si = 0; signed int si2 = 0;
std::cout << " Size of signed int: " << sizeof(si) << "B" << std::endl;
si2 = (unsigned int)(si - 1)/2 + 1;
std::cout << " Lower bound of signed int: " << (si2) << std::endl;
si2 = (unsigned int)(si - 1)/2;
std::cout << " Upper bound of signed int: " << (si2) << std::endl << std::endl;
unsigned int ui = 0;
std::cout << " Size of unsigned int: " << sizeof(ui) << "B" << std::endl;
std::cout << " Lower bound of unsigned int: " << ui << std::endl;
std::cout << " Upper bound of unsigned int: " << ui - 1 << std::endl << std::endl;
signed long sl = 0; signed long sl2 = 0;
std::cout << " Size of signed long: " << sizeof(sl) << "B" << std::endl;
sl2 = (unsigned long)(sl - 1)/2 + 1;
std::cout << " Lower bound of signed long: " << sl2 << std::endl;
sl2 = (unsigned long)(sl -1)/2;
std::cout << " Upper bound of signed long: " << sl2 << std::endl << std::endl;
unsigned long ul = 0;
std::cout << " Size of unsigned long: " << sizeof(ul) << "B" << std::endl;
std::cout << " Lower bound of unsigned long: " << ul << std::endl;
std::cout << " Upper bound of unsigned long: " << ul - 1 << std::endl << std::endl;
signed long long sll = 0; signed long long sll2 = 0;
std::cout << " Size of signed long long: " << sizeof(sll) << "B" << std::endl;
sll2 = (unsigned long long)(sll - 1)/2 + 1;
std::cout << " Lower bound of signed long long: " << sll2 << std::endl;
sll2 = (unsigned long long)(sll - 1)/2;
std::cout << " Upper bound of signed long long: " << sll2 << std::endl << std::endl;
unsigned long long ull = 0;
std::cout << " Size of unsigned long long: " << sizeof(ull) << "B" << std::endl;
std::cout << "Lower bound of unsigned long long: " << ull << std::endl;
std::cout << "Upper bound of unsigned long long: " << ull - 1 << std::endl << std::endl;
float flt = 0;
std::cout << "Size of float: " << sizeof(flt) << std::endl;
double dbl = 0;
std::cout << "Size of double: " << sizeof(dbl) << std::endl;
return 0;
}
Below is the output, on my system
doug@slackware14:~/Data Structures/src/data_types2$ ./main
Size of bool: 1B
Lower bound of bool: 0
Upper bound of bool: 1
Size of signed char: 1B
Lower bound of signed char: -128
Upper bound of signed char: 127
Size of unsigned char: 1B
Lower bound of unsigned char: 0
Upper bound of unsigned char: 255
Size of short signed int: 2B
Lower bound of signed short int: -32768
Upper bound of signed short int: 32767
Size of unsigned short int: 2B
Lower bound of unsigned short int: 0
Upper bound of unsigned short int: -1
Size of signed int: 4B
Lower bound of signed int: -2147483648
Upper bound of signed int: 2147483647
Size of unsigned int: 4B
Lower bound of unsigned int: 0
Upper bound of unsigned int: 4294967295
Size of signed long: 8B
Lower bound of signed long: -9223372036854775808
Upper bound of signed long: 9223372036854775807
Size of unsigned long: 8B
Lower bound of unsigned long: 0
Upper bound of unsigned long: 18446744073709551615
Size of signed long long: 8B
Lower bound of signed long long: -9223372036854775808
Upper bound of signed long long: 9223372036854775807
Size of unsigned long long: 8B
Lower bound of unsigned long long: 0
Upper bound of unsigned long long: 18446744073709551615
Size of float: 4
Size of double: 8