Höchstwert von int

Gibt es einen Code, um den Maximalwert einer ganzen Zahl (je nach Compiler) in C/C++ zu ermitteln, wie die Funktion "Integer.MaxValue" in Java?

Lösung

In C++:

#include 

dann verwenden Sie

int imin = std::numeric_limits::min(); // minimum value
int imax = std::numeric_limits::max();

std::numeric_limits" ist ein Vorlagentyp, der mit anderen Typen instanziiert werden kann:

float fmin = std::numeric_limits::min(); // minimum positive value
float fmax = std::numeric_limits::max();

In C:

#include 

dann verwenden Sie

int imin = INT_MIN; // minimum value
int imax = INT_MAX;

oder

#include 

float fmin = FLT_MIN;  // minimum positive value
double dmin = DBL_MIN; // minimum positive value

float fmax = FLT_MAX;
double dmax = DBL_MAX;
Kommentare (6)

#include 
#include 
using namespace std;

int main() {
  cout 
Kommentare (8)

Warum nicht ein Stück Code schreiben wie:


int  max_neg = ~(1 
Kommentare (1)