You can use an asterisk (*
) to pass the width specifier/precision to printf()
, rather than hard coding it into the format string, i.e.
void f(const char *str, int str_len)
{
printf("%.*s\n", str_len, str);
}
More Related Contents:
- Please explain the difference in the printfs below
- char c[]=”Albus”; printf(“%c”, c); [closed]
- Correct format specifier for double in printf
- How do you format an unsigned long long int using printf?
- What is the purpose of the h and hh modifiers for printf?
- How to printf “unsigned long” in C?
- Why is printf with a single argument (without conversion specifiers) deprecated?
- What precisely does the %g printf specifier mean?
- Why does printf not flush after the call unless a newline is in the format string?
- Printing leading 0’s in C
- How should I print types like off_t and size_t?
- printf format specifiers for uint32_t and size_t
- printf adds extra `FFFFFF` to hex print from a char array [duplicate]
- Read no more than size of string with scanf()
- What is the argument for printf that formats a long?
- Platform independent size_t Format specifiers in c?
- using printf to print out floating values
- Whitespace before %c specification in the format specifier of scanf function in C
- How to use “zd” specifier with `printf()`?
- Why scanf(“%d”, […]) does not consume ‘\n’? while scanf(“%c”) does?
- Passing too many arguments to printf
- printf with “%d” of numbers starting with 0 (ex “0102”) giving unexpected answer (ex ‘”66″)
- Wrong format specifiers in scanf (or) printf
- Using %f to print an integer variable
- Using printf with a non-null terminated string
- What primitive data type is time_t? [duplicate]
- Does C have a string type? [closed]
- Is it possible to print out only a certain section of a C-string, without making a separate substring?
- Printf long long int in C with GCC?
- cross-platform printing of 64-bit integers with printf