Variable mit printf formatieren, wie?

Gast #509752
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
Gast #509787
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.)
Gast #510512
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).

Antwort schreiben

Hinweis: der ursprüngliche Beitrag ist mehr als 6 Monate alt. Bitte hier nur auf die ursprüngliche Frage antworten, für neue Fragen einen neuen Beitrag erstellen.

Formatierung
  • [c]C-Code[/c]
  • [code]Code in anderen Sprachen, ASCII-Zeichnungen[/code]
  • [math]Formel in LaTeX-Syntax[/math]
  • [[Titel]] – Link zu einem Artikel
  • Verweis auf einen anderen Beitrag: Rechtsklick auf dessen Beitrags-ID (#8078628), „Adresse kopieren“, in den Text einfügen

Mehr zur Formatierung

Längeren Sourcecode als Anhang, nicht in den Text. JPG nur für Fotos und Scans; Zeichnungen und Screenshots als PNG oder GIF. Siehe Bildformate.

Schon registriert? Anmelden

Wird nicht angezeigt. Wir schicken dir einen Link; sobald du damit bestätigst, erscheint dein Beitrag.

Mit dem Abschicken legst du einen kostenlosen Account an und erkennst die Nutzungsbedingungen und die Datenschutzerklärung an.