Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

printf string, variable length item

Tags:

c

format

printf

#define SIZE 9 int number=5; char letters[SIZE]; /* this wont be null-terminated */ ...   char fmt_string[20]; sprintf(fmt_string, "%%d %%%ds", SIZE); /* fmt_string = "%d %9d"... or it should be */  printf(fmt_string, number, letters); 

Is there a better way to do this?

like image 726
William Entriken Avatar asked May 09 '11 03:05

William Entriken


People also ask

How do I print a string length of a variable?

#define SIZE 9 int number=5; char letters[SIZE]; /* this wont be null-terminated */ ... char fmt_string[20]; sprintf(fmt_string, "%%d %%%ds", SIZE); /* fmt_string = "%d %9d"... or it should be */ printf(fmt_string, number, letters);

How do I left justify using printf?

printf allows formatting with width specifiers. For example, printf( "%-30s %s\n", "Starting initialization...", "Ok." ); You would use a negative width specifier to indicate left-justification because the default is to use right-justification.

Can you print a string with printf?

We can print the string using %s format specifier in printf function. It will print the string from the given starting address to the null '\0' character. String name itself the starting address of the string. So, if we give string name it will print the entire string.

Can you use the precision specifier to limit the number of characters printed from a string?

I learned recently that you can control the number of characters that printf will show for a string using a precision specifier (assuming your printf implementation supports this).


2 Answers

There is no need to construct a special format string. printf allows you to specify the precision using a parameter (that precedes the value) if you use a .* as the precision in the format tag.

For example:

printf ("%d %.*s", number, SIZE, letters); 

Note: there is a distinction between width (which is a minimum field width) and precision (which gives the maximum number of characters to be printed). %*s specifies the width, %.s specifies the precision. (and you can also use %*.* but then you need two parameters, one for the width one for the precision)

See also the printf man page (man 3 printf under Linux) and especially the sections on field width and precision:

Instead of a decimal digit string one may write "*" or "*m$" (for some decimal integer m) to specify that the precision is given in the next argument, or in the m-th argument, respectively, which must be of type int.

like image 67
Trent Avatar answered Sep 22 '22 06:09

Trent


A somewhat unknown function is asprintf. The first parameter is a **char. This function will malloc space for the string so you don't have to do the bookkeeping. Remember to free the string when done.

char *fmt_string;  asprintf(&fmt_string, "%%d %%%ds", SIZE); printf(fmt_string, number, letters); free(fmt_string); 

is an example of use.

like image 35
No One in Particular Avatar answered Sep 20 '22 06:09

No One in Particular