Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I print a non-null-terminated string using printf?

Tags:

c

string

printf

How can I print a non-null-terminated string using printf, assuming that I know the length of the string at runtime?

like image 494
Mike Avatar asked Jan 26 '10 06:01

Mike


People also ask

Does printf need null terminate?

printf does nothing related to null terminate expect its own print processing. It only accepts char* as input.

What happens if a string is not null-terminated?

Many library functions accept a string or wide string argument with the constraint that the string they receive is properly null-terminated. Passing a character sequence or wide character sequence that is not null-terminated to such a function can result in accessing memory that is outside the bounds of the object.

Does printf print null character?

The null character is only used to signify the end of a string, and should not be printed. More than one call to printf() can contribute to the same 'formatted line'.

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.


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.

like image 85
kennytm Avatar answered Oct 12 '22 02:10

kennytm


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
like image 39
M.S. Dousti Avatar answered Oct 12 '22 03:10

M.S. Dousti