Using printf with a non-null terminated string

whoi picture whoi · Sep 22, 2010 · Viewed 54.4k times · Source

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...

Answer

DarkDust picture DarkDust · Sep 22, 2010

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.