Meenu said:
I mean, if I make a=15 and do %20s instead of %50s, it prints the
entire statement rather than just first 20 chars
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:
I can't understand the output of the following function:
main()
{
char str[]="part-time musicians are semiconductors";
int a=5;
printf(a>10?"%50s":"%s",str);
}
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)