2014-01-15 11 views
14

Ich erhalte Warnungen kompilieren, aber ich weiß nicht, wie es zu beheben:'% d' erwartet Argument vom Typ 'int', aber Argument 2 hat Typ 'long unsigned int' [-Wformat =]

'%d' expects argument of type 'int', but argument 2 has type 'long unsigned int' [ 

Das Programm läuft gut, aber ich immer noch die Kompilierung Warnungen erhalten:

/* Sizeof.c--Program to tell byte size of the C variable */ 
#include <stdio.h> 

int main(void) { 
    printf("\nA Char is %d bytes", sizeof(char)); 
    printf("\nAn int is %d bytes", sizeof(int)); 
    printf("\nA short is %d bytes", sizeof(short)); 
    printf("\nA long is %d bytes", sizeof(long)); 
    printf("\nA long long is %d bytes\n", sizeof(long long)); 
    printf("\nAn unsigned Char is %d bytes", sizeof(unsigned char)); 
    printf("\nAn unsigned int is %d bytes", sizeof(unsigned int)); 
    printf("\nAn unsigned short is %d bytes", sizeof(unsigned short)); 
    printf("\nAn unsigned long is %d bytes", sizeof(unsigned long)); 
    printf("\nAn unsigned long long is %d bytes\n", 
      sizeof(unsigned long long)); 
    printf("\nfloat is %d bytes", sizeof(float)); 
    printf("\nA double is %d bytes\n", sizeof(double)); 
    printf("\nA long double is %d bytes\n", sizeof(long double)); 

    return 0; 

} 

Antwort

23

sizeof kehren size_t Sie benötigen %zu für das Format-String zu verwenden, anstatt %d. Die Art der unsigned integer von size_t kann variieren (abhängig von der Plattform) und möglicherweise nicht lange unsigned int überall, die im Entwurf des C99 Standardteil 6.5.3.4Der Operator sizeof Absatz bedeckt ist:

The value of the result is implementation-defined, and its type (an unsigned integer type) is size_t, defined in (and other headers).

beachten Sie auch, dass die falsche Formatangabe für printf mit undefiniertem Verhalten, das 7.19.6.1 in Abschnitt bedeckt ist die fprintf Funktion, die auch printf bedeckt w ith zu Formatbezeich respektieren sagt:

If a conversion specification is invalid, the behavior is undefined.248) If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined.

aktualisieren

Visual Studiodoes not support the z format specifier:

The hh, j, z, and t length prefixes are not supported.

die richtige Formatangabe in diesem Fall würde %Iu sein.

+0

In GCC 4.8.1 unter Windows bekomme ich einen Fehler: "unbekanntes Konvertierungstyp Zeichen 'z' im Format" beim Drucken% zu. –

+0

@CzarekTomczak aktualisierte Antwort, wahrscheinlich verwandt. –

+0

Danke Shafik. Leider ist nichts davon plattformübergreifend. Ich muss size_t auf (unsigned long) setzen, damit der Code sowohl unter Linux als auch unter Windows funktioniert. Unter Linux wird bei der Verwendung von% Iu (I als Integer) der Fehler "Format '% u' erwartet das Argument vom Typ 'unsigned int'". –

5

Der Compiler warnt Sie, dass Sie einen Genauigkeitsverlust erleiden können. Das heißt, der Formatspezifizierer, mit dem Sie eine sizeof, %d drucken, ist nicht in der Lage, den gesamten Bereich von size_t zu drucken. Ändern Sie %d zu %zu und Ihre Warnung wird weggehen.

0

Ich hatte das gleiche Problem in Linux. Das gleiche Programm läuft ohne Fehler in Windows (bedeutet '% d' hat ohne Fehler funktioniert), aber für Linux musste ich alle '% d' durch '% lu' ersetzen, um das Programm zu starten.

Verwandte Themen