Program output

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Meenu

    #1

    Program output

    I can't understand the output of the following function:

    main()
    {
    char str[]="part-time musicians are semiconductors" ;
    int a=5;
    printf(a>10?"%5 0s":"%s",str) ;
    }

  • martinaw@gmail.com

    #2
    Re: Program output

    if a is grater than 10 the print only 50 chars
    of the string, else print it all

    Comment

    • Meenu

      #3
      Re: Program output

      It seems to print the entire statement in any case

      Comment

      • Meenu

        #4
        Re: Program output

        I mean, if I make a=15 and do %20s instead of %50s, it prints the
        entire statement rather than just first 20 chars

        Comment

        • Rob Morris

          #5
          Re: Program output

          Meenu wrote:[color=blue]
          > I mean, if I make a=15 and do %20s instead of %50s, it prints the
          > entire statement rather than just first 20 chars
          >[/color]

          Well, of course - %20s means print using at least 20 chars width. Consider:

          #include <stdio.h>
          int main (void)
          {
          char s[]="String.";
          printf("s is '%20s'\n", s);
          return 0;
          }

          This outputs:
          s is ' String.'

          Perhaps the OP was also confused about the ? operator. It's like an
          abbreiviated if - then - else and can be useful in printf in forms like:
          printf("There were %d error%s\n", errors, errors == 1 ? "" : "s" );


          --
          Rob Morris: arr emm four four five (at) cam dot ac dot uk

          Comment

          • Rob Morris

            #6
            Re: Program output

            Rob Morris wrote:
            [Snip!][color=blue]
            > Perhaps the OP was also confused about the ? operator. It's like an
            > abbreiviated if - then - else and can be useful in printf in forms like:
            > printf("There were %d error%s\n", errors, errors == 1 ? "" : "s" );
            >[/color]
            .... though one might wish to use slightly better grammar (D'oh!)

            --
            Rob Morris: arr emm four four five (at) cam dot ac dot uk

            Comment

            • Old Wolf

              #7
              Re: Program output

              Meenu wrote:[color=blue]
              > I mean, if I make a=15 and do %20s instead of %50s, it prints the
              > entire statement rather than just first 20 chars[/color]

              It is considered polite on Usenet to quote parts of the
              posts you are replying to, and then write your comments
              after it, so that a reader can see what your post is about,
              without having to refer to any other posts.

              Since you didn't do this, I'll fill in: you were replying to
              your own post which went:
              [color=blue][color=green]
              >>I can't understand the output of the following function:
              >>main()
              >>{
              >> char str[]="part-time musicians are semiconductors" ;
              >> int a=5;
              >> printf(a>10?"%5 0s":"%s",str) ;
              >>}[/color][/color]

              The answer is that "%15s" means: print a MINIMUM of 15
              characters (but possibly more).
              To specify a maximum you could write:
              "%.15s" -- print a MAXIMUM of 15 characters
              "%10.20s" -- print a MINIMUM of 10 and a MAXIMUM of
              20 chars (right-justified)
              "%-10.20s" -- as above, but left-justified.

              Also your program has some other errors:
              - it must #include <stdio.h> because it calls printf
              - it should return a value from main
              - main should be declared as: int main(void)

              Comment

              Working...