20070712

Display Integer Type Information Standard C++ Compilers.

Το παρακάτω πρόγραμμα εμφανίζει στην οθόνη τα όρια των ακέραιων τύπων στην C++:
#include <iostream>
#include <climits>

using namespace std;

volatile int char_min = CHAR_MIN;

int main(void) {
cout << "Size of boolean type is " << sizeof(bool)
<< " byte(s)" << "\n\n";

cout << "Number of bits in a character: "
<< CHAR_BIT << '\n';
cout << "Size of character types is "
<< sizeof(char)
<< " byte" << '\n';
cout << "Signed char min: "
<< SCHAR_MIN << " max: "
<< SCHAR_MAX << '\n';
cout << "Unsigned char min: 0 max: "
<< UCHAR_MAX << '\n';

cout << "Default char is ";

if (char_min < 0)
cout << "signed";
else if (char_min == 0)
cout << "unsigned";
else
cout << "non-standard";
cout << "\n\n";

cout << "Size of short int types is "
<< sizeof(short) << " bytes"
<< '\n';
cout << "Signed short min: "
<< SHRT_MIN << " max: "
<< SHRT_MAX << '\n';
cout << "Unsigned short min: 0 max: "
<< USHRT_MAX << "\n\n";

cout << "Size of int types is "
<< sizeof(int) << " bytes"
<< '\n';
cout << "Signed int min: "
<< INT_MIN << " max: "
<< INT_MAX << '\n';
cout << "Unsigned int min: 0 max: "
<< UINT_MAX << "\n\n";

cout << "Size of long int types is "
<< sizeof(long) << " bytes"
<< '\n';
cout << "Signed long min: "
<< LONG_MIN << " max: "
<< LONG_MAX << '\n';
cout << "Unsigned long min: 0 max: "
<< ULONG_MAX << endl;

getchar();
return 0;
}
Η εκτέλεση του παραπάνω προγράμματος στο Visual Studio .NET 2005 βγάζει τα παρακάτω αποτελέσματα:

Size of boolean type is 1 byte(s)

Number of bits in a character: 8
Size of character types is 1 byte
Signed char min: -128 max: 127
Unsigned char min: 0 max: 255
Default char is signed

Size of short int types is 2 bytes
Signed short min: -32768 max: 32767
Unsigned short min: 0 max: 65535

Size of int types is 4 bytes
Signed int min: -2147483648 max: 2147483647
Unsigned int min: 0 max: 4294967295

Size of long int types is 4 bytes
Signed long min: -2147483648 max: 2147483647
Unsigned long min: 0 max: 4294967295

Δεν υπάρχουν σχόλια:

Αρχειοθήκη ιστολογίου

eggs.in.art (my non-technical blog)