143

Suppose you have a string which is NOT null terminated and you know its exact size, so how can you print that string with printf in C? I recall such a method but I can not find out now...

5

6 Answers 6

221

There is a possibility with printf, it goes like this:

printf("%.*s", stringLength, pointerToString);

No need to copy anything, no need to modify the original string or buffer.

Sign up to request clarification or add additional context in comments.

6 Comments

@Pmod: Not necessarily if the buffer is not exposed to the outside world. It's also very useful to just print parts of a string (which may be null terminated, of course). If you really want to see this in action, have a look at the OpenSER/Kamailio SIP proxy where they avoid copying stuff due to this technique a lot (also using sprintf).
I don't have anything against printing a part of NULL-terminated string in a way you described. I also do in that way in my progs. But I am against using NON-NULL-terminated strings to pretend that they're terminated. Someone may some day str(n)cpy it or expose outside without noticing this specific.
@pmod: to your concerns, I typically declare an array of uint8_t instead of char, as an attempt to make it clear that it is not a null-terminated string.
For me it's very useful when I receive a non-null terminated string from an API (some windows API do this!) and have to return it in a 'reasonable' way. So: long life to %.*s (or %.*S which will also make the conversion UNICODE <-> SINGLE-BYTE for you! ;) )
@user1424739: In your case printf will print up to 11 characters or until it encounters NULL, whichever comes first; in your example NULL comes first. Specifying a maximum length does not make NULL lose its "end-of-string" meaning for printf.
|
38

Here is an explanation of how %.*s works, and where it's specified.

The conversion specifications in a printf template string have the general form:

% [ param-no $] flags width [ . precision ] type conversion

or

% [ param-no $] flags width . * [ param-no $] type conversion

The second form is for getting the precision from the argument list:

You can also specify a precision of ‘*’. This means that the next argument in the argument list (before the actual value to be printed) is used as the precision. The value must be an int, and is ignored if it is negative.

— Output conversion syntax in the glibc manual

For %s string formatting, precision has a special meaning:

A precision can be specified to indicate the maximum number of characters to write; otherwise characters in the string up to but not including the terminating null character are written to the output stream.

— Other output conversions in the glibc manual

Other useful variants:

  • "%*.*s", maxlen, maxlen, val will right-justify, inserting spaces before;
  • "%-*.*s", maxlen, maxlen, val will left-justify.

1 Comment

If I understand correctly, the following would pad the output yet still prevent a string overflow? "%-*.*s", padding, str_view.size(), str_view.data()
28

You can use an fwrite() to stdout!

fwrite(your_string, sizeof(char), number_of_chars, stdout);

This way you will output the first chars (number defined in number_of_chars variable ) to a file, in this case to stdout (the standard output, your screen)!

3 Comments

Very helpful when you want to inspect a long buffer containing strings and zeros!
This is the only way to print a string that contains \0. Thanks!
For now (and maybe so on), sizeof(char) is always 1.
19

printf("%.*s", length, string) will NOT work.

This means to print UP TO length bytes OR a null byte, whichever comes first. If your non-null-terminated array-of-char contains null bytes BEFORE the length, printf will stop on those, and not continue.

2 Comments

And how is this an answer to the OP's question?
if it's not null-terminated, then null is a valid character for the string to contain. this still thinks the array is null-terminated, it just treats it as a longer array that it's sub-selecting from - which means that if you have a string with nulls in it, this will cause problems.
4
printf("%.5s", pointerToNonNullTerminatedString);

The string length will be 5.

Comments

1
#include<string.h> 
int main()
{
/*suppose a string str which is not null terminated and n is its length*/
 int i;
 for(i=0;i<n;i++)
 {
 printf("%c",str[i]);
 }
 return 0;
}

I edited the code,heres another way:

#include<stdio.h>
int main()
{
printf ("%.5s","fahaduddin");/*if 5 is the number of bytes to be printed and fahaduddin is the string.*/

return 0;

}

5 Comments

Very bad performance due to lots of unnecessary byte reads (which come with a performance penalty if the the byte is not on a word-aligned address on most CPUs) and also the formatting parsing and applying is done for each and every character. Don't do that :-) See my answer for the solution.
@DarkDust: only a pathological machine would penalize byte reads not aligned to word boundaries. Are you thinking of word reads not aligned to word boundaries? Or some ancient mips crap or something?
@R..: If you consider x86 brain-damaged and out-dated, I absolutely agree. Because x86 does have a penalty for reading and write non-word aligned memory. So does ARM. See for example this question or this question. The thing is (if I understood that correctly) that data is fetched in word-size chunks from memory and getting the correct byte is another micro-op. No big deal, but in a huge loop it might make a difference.
@DarkDust: you are completely wrong about that for byte reads. Why don't you go do a benchmark? x86 has completely atomic byte operations and always had. It does not fetch word-size chunks (except at the cache level, which fetches much larger chunks and alignment is irrelevant, but I'm talking about already-cached data).
@DarkDust: PS3 does not support unaligned byte read or write on SPU. In fact it does not even support scalar types, there are only vector, that must be aligned. The compiler emulate them. And many ARM processor does not support byte read or write, but only perform word read or write.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.