in Programming in C retagged by
417 views
0 votes
0 votes

How Format specifiers in C works?

If the Code is :

#include <stdio.h>

int main(void) {
	// your code goes here
	char *s= "First Name";
	int x= 12345;
	printf("%-15s%c\n", "First Name", ':');
	printf("%-15s%c\n", "Last Name", ':');
	printf("%-15s%c\n", "Age", ':');
	printf("%10.3f\n", (float)x);
	
	
	return 0;
}

then for the 3rd print statement, I get the output of : 

_12345.000 (dash being a space here. )

I get it that the .3 part is controlling the precision and hence the 3 zeros, but due to the 10 its length should be 10 minimum so should get more spaces. 

code is here also :https://ideone.com/Mo5jwf

in Programming in C retagged by
by
417 views

4 Comments

Anyone can just explain how I'm getting only 1 space. ?
0
0
because meaning of "%10.3f" is, it will print10 digit float number with including decimal upto 3 digits.

like x = 12345

now result is _12345.000(total digits are 10(including '.' point also)).
1
1
Oh the Decimal point is also counted as a digit. so the minimum length is actually for the number of characters to be printed?
0
0
yes correct.
0
0

Please log in or register to answer this question.

Related questions