Click here to Skip to main content
15,913,925 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
what will happen when the length of the string literal is less than the length the specified in format specifier as shown

What I have tried:

#include<stdio.h>
int main()
{
printf("%.5s","abc");
return 0;
}
Posted
Updated 23-Apr-21 19:04pm

1 solution

The width specifier after teh decimal point is a maximum number of characters to print, not a minimum.

It will print the string up to the length of the string or the width specifier, whichever is the shortest.
SInce your string has 3 characters before the null terminator, and the width is specified as 5, it will print the whole string.
 
Share this answer
 
Comments
Dave Kreskowiak 24-Apr-21 1:31am    
Why do I get the feeling this guy is posting his homework questions?
OriginalGriff 24-Apr-21 1:58am    
I know what you mean, it is possible - but they are good questions: kudos either to the OP or his teacher! :D
Dave Kreskowiak 24-Apr-21 2:01am    
I'll 'em that much. They are good questions, getting into the details of how printf works. I prefer questions like this as they convey a need for understanding how the functions work instead of just saying "do it like this" without knowing why or what it means.
_-_-_-me 25-Apr-21 1:24am    
These are not homework questions.
_-_-_-me 25-Apr-21 1:26am    
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900