How can I print a non-null-terminated string using printf, assuming that I know the length of the string at runtime?
-
6If it's not null terminated, then by definition it's not a string. – Keith Thompson Aug 04 '14 at 03:36
-
8it's only not a _c string_, as it would be a valid string in other languages.. and though a "non-null-terminated array of char" would be more accurate, I'm pretty sure it was universally understood. – Wyllow Wulf Jul 29 '16 at 19:58
-
2Possible duplicate of [Using printf with a non-null terminated string](http://stackoverflow.com/questions/3767284/using-printf-with-a-non-null-terminated-string) – Emil Laine Apr 01 '17 at 19:06
2 Answers
printf("%.*s", length, string);
Use together with other args:
printf("integer=%d, string=%.*s, number=%f", integer, length, string, number);
// ^^^^ ^^^^^^^^^^^^^^
In C you could specify the maximum length to output with the %.123s
format. This means the output length is at most 123 chars. The 123
could be replaced by *
, so that the length will be taken from the argument of printf instead of hard-coded.
Note that this assumes the string
does not contain any interior null bytes (\0), as %.123s
only constrains the maximum length not the exact length, and strings are still treated as null-terminated.
If you want to print a non-null-terminated string with interior null, you cannot use a single printf. Use fwrite
instead:
fwrite(string, 1, length, stdout);
See @M.S.Dousti's answer for detailed explanation.
-
11note: `length` must have type `int` here, after default argument promotions. If your `length` variable is something bigger (e.g. `size_t`) you should cast it to `(int)` else your code will break on a platform where `size_t` is wider than `int`. – M.M Aug 04 '14 at 04:25
The answer provided by @KennyTM is great, but with a subtlety.
In general, if the string is non-null "terminated", but has a null character in the middle, printf("%.*s", length, string);
does not work as expected. This is because the %.*s
format string asks printf
to print a maximum of length
characters, not exactly length
characters.
I'd rather use the more general solution pointed out by @William Pursell in a comment under the OP:
fwrite(string, sizeof(char), length, stdout);
Here's a sample code:
#include <stdio.h>
int main(void) {
size_t length = 5;
char string[length];
string[0] = 'A';
string[1] = 'B';
string[2] = 0; // null character in the middle
string[3] = 'C';
string[4] = 'D';
printf("With printf: %.*s\n", length, string);
printf("With fwrite: ");
fwrite(string, sizeof(char), length, stdout);
printf("\n");
return 0;
}
Output:
With printf: AB
With fwrite: AB CD

- 254,901
- 44
- 429
- 631

- 3,346
- 6
- 35
- 53
-
2`sizeof (char)` is 1 by definition. Also, `NULL` is specifically a null *pointer* constant. – Keith Thompson Aug 04 '14 at 03:37
-
2@KeithThompson, `sizeof (char)` is more semantic. Also, `'\0'` is more semantic than `0`. But who cares ;-) – Ivan Black Jan 12 '17 at 15:58
-
My output for this is `With fwrite: AB` - it seems to be stopping at the null terminator. Compiled with gcc 4.8.5 – Matthew Moisen Feb 29 '20 at 17:08
-
@MatthewMoisen: See [Godbolt output](https://godbolt.org/z/CA-adM) for your compiler. – Sadeq Dousti Feb 29 '20 at 23:22