Archive

Posts Tagged ‘C++’

climits Symbolic Constants

July 26, 2012 Leave a comment

There can be situations from a code snippet needs to start from the first allowable value of variable of a particular type and goes until the final value that the type support. This kind of scenarios are frequent in embedded programming at least, which I am familiar with.

In C++ there are some symbolic constants available through which one can determine the minimum and max value of particular data type. These constants are a part of climits header files.

The following code snippet demonstrates this functionality

void showCLimitsSymbolicConstants(){
    std::cout<<"CHAR_BIT: "<<CHAR_BIT<<"\n";    //Number of bits in a char
    std::cout<<"CHAR_MIN: "<<CHAR_MIN<<"\tCHAR_MAX: "<<CHAR_MAX<<"\n";    //Min and maximum char value
    std::cout<<"SCHAR_MAX: "<<SCHAR_MAX<<"\tSCHAR_MIN: "<<SCHAR_MIN<<"\n"; //Max and Min signed char value
    std::cout<<"UCHAR_MAX: "<<UCHAR_MAX<<"\n"; //Max unsigned char value
    std::cout<<"SHRT_MAX: "<<SHRT_MAX<<"\tSHRT_MIN: "<<SHRT_MIN<<"\n";  //Max & min short value
    std::cout<<"USHRT_MAX: "<<USHRT_MAX<<"\n"; //max unsigned short value
    std::cout<<"INT_MAX: "<<INT_MAX<<"\tINT_MIN: "<<INT_MIN<<"\n"; //max and min signed int value
    std::cout<<"UINT_MAX: "<<UINT_MAX<<"\n"; //max unsigned int value
    std::cout<<"LONG_MAX: "<<LONG_MAX<<"\tLONG_MIN: "<<LONG_MIN<<"\n"; //max and min long  value
    std::cout<<"ULONG_MAX: "<<ULONG_MAX<<"\n";//max unsigned long value
    std::cout<<"LLONG_MAX: "<<LLONG_MAX<<"\tLLONG_MIN: "<<LLONG_MIN<<"\n";//max and min long long  value
    std::cout<<"ULLONG_MAX: "<<ULLONG_MAX<<"\n";//max unsigned long long value
    std::cin.get();
}
Advertisements
Categories: C++ Tags: ,