Forum: Compiler & IDEs Variable mit printf formatieren, wie?


von formatstring (Gast)


Lesenswert?

Moin,

ich möchte ein unsigned long int ausgeben, mit einer definierten Anzahl 
an Stellen, fehlende Stellen sollen links mit nullen aufgefüllt werden.

also z.B: val= 261254 ; Ausgabe soll so aussehen: 00261254

mit %08li als formatstring funktioniert's nicht obwohl das doch richtig 
ist, oder?

Hat jemand nen Tip für mich?

Thanks

von Andre F (Gast)


Lesenswert?

Hy,
so müsste es klappen.

unsigned long int us=261254;
printf("Ausgabe: %08u \n\r",us);

Ich hoffe das ist so mit dem long ok.


mfg.: Andre´

von printf (Gast)


Lesenswert?

http://www.hmug.org/man/3/printf.php

o   An optional length modifier, that specifies the size of the 
argument.
    The following length modifiers are valid for the d, i, n, o, u, x, 
or
    X conversion:

    Modifier          d, i           o, u, x, X            n
    hh                signed char    unsigned char         signed char *
    h                 short          unsigned short        short *
    l (ell)           long           unsigned long         long *
    ll (ell ell)      long long      unsigned long long    long long *
    j                 intmax_t       uintmax_t             intmax_t *
    t                 ptrdiff_t      (see note)            ptrdiff_t *
    z                 (see note)     size_t                (see note)
    q (deprecated)    quad_t         u_quad_t              quad_t *

    Note: the t modifier, when applied to a o, u, x, or X conversion,
    indicates that the argument is of an unsigned type equivalent in 
size
    to a ptrdiff_t.  The z modifier, when applied to a d or i 
conversion,
    indicates that the argument is of a signed type equivalent in size 
to
    a size_t.  Similarly, when applied to an n conversion, it indicates
    that the argument is a pointer to a signed type equivalent in size 
to
    a size_t.

    The following length modifier is valid for the a, A, e, E, f, F, g,
    or G conversion:

    Modifier    a, A, e, E, f, F, g, G
    l (ell)     double (ignored, same behavior as without it)
    L           long double

    The following length modifier is valid for the c or s conversion:

    Modifier    c         s
    l (ell)     wint_t    wchar_t *

    The AltiVec Technology Programming Interface Manual also defines 
five
    additional length modifiers which can be used (in place of the con-
    ventional length modifiers) for the printing of AltiVec vectors:
    v           Treat the argument as a vector value, unit length will 
be
                determined by the conversion specifier (default = 16
                8-bit units for all integer conversions, 4 32-bit units
                for floating point conversions).
    vh, hv      Treat the argument as a vector of 8 16-bit units.
    vl, lv      Treat the argument as a vector of 4 32-bit units.

    NOTE: The vector length specifiers are AltiVec only extensions onto
    the printf() specification.  Behaviour of these values for printf()
    is only defined for operating systems conforming to the AltiVec 
Tech-
    nology Programming Interface Manual.  (At time of writing this
    includes only Mac OS X 10.2 and later.)

von Jörg W. (dl8dtl) (Moderator) Benutzerseite


Lesenswert?

Schlauer Herr printf (bist du eigentlich stolz auf deinen Namen?),
wenn schon URLs, dann bitte die passenden.  Nicht alles ist in der
avr-libc wirklich implementiert, sondern es ist eine Untermenge.

http://www.nongnu.org/avr-libc/user-manual/group__avr__stdio.html#ga3b98c0d17b35642c0f3e4649092b9f1

von Andre F (Gast)


Lesenswert?

Meins müsste eigentlich Fknktionieren... gestestet mit gcc (k.A. welche 
version) im DevC++.

von formatstring (Gast)


Lesenswert?

Danke an alle! Meine erste Version funktioniert, ist ja identisch mit 
der von Andre F., ich habe im MakeFile die falsche printf lib gelinkt, 
die minimalistic, dachte die wäre umfangreicher als der Standard, mein 
Fehler das ich das Manual nicht gelesen habe.....( ;-) @ Jörg Wunsch).

von Günter R. (galileo14)


Lesenswert?

@Andre F:

> printf("Ausgabe: %08u \n\r",us);

Müßte es nicht

printf("Ausgabe: %08lu \n\r",us);

heißen?

von formatstring (Gast)


Lesenswert?

@günter R.

ist richtig, der gcc generiert aber nur eine Warnung, läuft also durch.

von formatstring (Gast)


Lesenswert?

"ist richtig" bedeutet natürlich Günter hat Recht:

printf("Ausgabe: %08lu \n\r",us);

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.