How the following C program works?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • itiger
    New Member
    • Sep 2010
    • 12

    How the following C program works?

    Code:
    # include <stdio.h>
    void main()
    {
       char str[] = "Zingle Bell Zingle Bell";
       printf("%.#s %.2s", str, str);
    }
    output:
    #s Zi

    (how the '%s' type specifier works in this case.)
    Last edited by Nepomuk; Sep 3 '10, 11:42 PM. Reason: Please use [CODE] tags
  • whodgson
    Contributor
    • Jan 2007
    • 542

    #2
    I cannot find any reference meaning for %.#s or why fprint would output #s which may be printed as a result of undefined behavior. %.2s is applying two places of precision to string str and so prints Zi bearing in mind that a character is treated like an int.

    Comment

    Working...