int Type

This page was translated by a robot.

The type intstores an integer value, which nowadays is usually encoded with 32 bits. The type intis considered the standard integer type in C and C++.

Details

On inttoday's systems, an is encoded as a 4-byte integer, i.e. with 32 bits. However, this sizing is not guaranteed and is provided on this page as a guide only.

In the past, intso-called type modifiers like short, longor long longwere added to the type to specify the number of bits to be used for the integer. Today, these modifiers are used less and less, since many integer types with more precise information on the number of bits are available from the C99 standard with the stdintlibrary .

In today's systems, a is equivalent intto the longtype . The specification longoften only exists for reasons of compatibility with older code or as a distinguishing feature in protocol or file format descriptions.

A inttype can be modified with either signedor . unsignedDepending on the case, a stores inteither negative as well as positive values, or explicitly only positive ones. If the modifier is omitted, it is signedassumed by the compiler by default. Whether the modifier comes before or after the type doesn't matter.

int           a;
signed int    b;
int signed    c;
unsigned int  d;
int unsigned  e;

A special feature of the inttype is that it is the standard type for untyped declarations. This comes into play, for example, with the (not recommended) use of the old style parameters or with type promotion . However, due to the default assumption, it is also possible to declare one intjust by specifying signedor :unsigned:

signed        f;
unsigned      g;

Positive values ​​are stored as a binary integer . The negative values ​​of a are usually encoded signed intusing two's complement on today's computers..

The maximum and minimum possible values ​​of a intare defined in the limitslibrary .

If the value range of one is exceeded by an arithmetic operation int, this is referred to as an overflow . On today's processors, a signed integer suddenly flips from the positive to the negative range, and in the case of unsigned integers, the number 0(zero) suddenly follows the maximum value. This is called a wrap around . Similarly, falling below the value range is referred to as underflow and the wrap-around happens vice versa. However, there is no guarantee that this effect will occur on every system.

If integers are written as fixed values ​​in the source code , they are automatically treated as such by the compiler int. In contrast to long- and long long-values, -values ​​do intnot require an additional suffix.

To explicitly unsignedmark a fixed value as , the suffix ucan be used.

Today's compilers usually issue warnings when fixed values ​​are too large for one intand should be marked as a long- or long long-value .

Contrary to all other integer types with modifiers, no additional modifier is needed to intoutput printfor scanfread in one:


123
int s = 123;
printf("%d\n", s);

Arithmetische Umwandlung

Bei Zuweisungen, Initialisierungen oder Casts zu einem int-Typ werden durch den Compiler gegebenfalls arithmetische Umwandlungen durchgeführt. Im folgenden sind die Umwandlungsregeln für den int-Typ beschrieben. Bei unsicheren Umwandlungen gibt ein Compiler üblicherweise eine Warnung aus, welche jedoch bei Bedarf mittels eines expliziten Casts unterdrückt werden kann.

Bei der Umwandlung eines Fliesskomma-Typs in einen int werden die Nachkommastellen schlicht abgeschnitten (Rundung hin zu Null).

123
-123
printf("%d\n", (signed   int) 123.456);
printf("%d\n", (signed   int)-123.456);

Hat die umzuwandelnde Fliesskommazahl keinen Platz in einem int, so ist das Resultat undefiniert. Auf heutigen Systemen wird häufig der maximal, beziehungsweise der minimal mögliche Wert angenommen.

2147483647
-2147483648
4294967295
0
printf("%d\n", (signed   int) 1e20);
printf("%d\n", (signed   int)-1e20);
printf("%u\n", (unsigned int) 1e20);
printf("%u\n", (unsigned int)-1e20);

Das Resultat kann je nach System unterschiedlich ausfallen. Die hier angegebenen Werte entsprechen den Werten auf dem System des Autors.

Bei der Umwandlung eines Integer-Typs in einen int bleiben in den wichtigsten Fällen der Wert sowie das Vorzeichen exakt erhalten:





123
-123
123
-123
unsigned  short      s1 =  123;
signed    short      s2 = -123;
unsigned  long long  L1 =  123;
signed    long long  L2 = -123;
printf("%u\n", (unsigned int) s1);
printf("%d\n", (signed   int) s2);
printf("%u\n", (unsigned int) L1);
printf("%d\n", (signed   int) L2);

Die Umwandlung eines Integer-Wertes in einen int, welcher nicht im Werteumfang enthalten ist, ist nicht definiert. Auf heutigen Systemen passiert jedoch normalerweise folgendes: Wenn der zuzuweisende Wert gleichviele oder mehr Bits besitzt, dann werden all diejenigen niederwertigsten Bits des zuzuweisenden Wertes bitgenau kopiert, welche im int-Typ Platz finden. Falls der zuzuweisende Wert weniger Bitstellen aufweist, werden die verfügbaren Bitstellen an die niederwertigsten Bitstellen des int kopiert. Die nicht-definierten höherwertigen Bit-Stellen werden folgendermassen aufgefüllt: Wenn der Typ des zuzuweisenden Wertes vorzeichenlos ist, wird an alle nicht-definierten Bit-Stellen das Bit 0 kopiert. Wenn der Typ des zuzuweisenden Wertes vorzeichenbehaftet ist, wird das höchstwertige Bit der verfügbaren Bitstellen an sämtliche höherwertige Bit-Stellen kopiert.

Durch diese Regeln können folgende Fälle zu falschen Werten führen: Eine vorzeichenbehaftete Zahl kann nicht korrekt in einen vorzeichenlosen Typ umgewandelt werden. Eine zu grosse vorzeichenlose Zahl kann bei der Umwandlung in einen vorzeichenbehafteten Typ zu negativen Werten führen. Bei der Umwandlung von (vorzeichenbehafteten oder vorzeichenlosen) Zahlen höherer Mächtigkeit (insbesondere bei long und long long) kann durch die Kürzung auf weniger Bits der Wert verfälscht werden oder gar zu falschem Vorzeichen führen.

4294967173
-294967296
1705032704
-589934592
printf("%u\n", (unsigned int) -123);
printf("%d\n", (signed   int)  4000000000);
printf("%d\n", (signed   int)  6000000000LL);
printf("%d\n", (signed   int)  8000000000LL);

Ein Javascript-Programm zur Veranschaulichung der Effekte beim Abschneiden von Bits ist beim Zahlensystem-Umrechner zu finden.