Using printf with a non-null terminated string

CStringFormatPrintf

C Problem Overview


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

C Solutions


Solution 1 - C

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.

Solution 2 - C

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.

Solution 3 - C

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)!

Solution 4 - C

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.

Solution 5 - C

printf("%.5s", pointerToNonNullTerminatedString);

The string length will be 5.

Solution 6 - C

#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;

}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionwhoiView Question on Stackoverflow
Solution 1 - CDarkDustView Answer on Stackoverflow
Solution 2 - CTobuView Answer on Stackoverflow
Solution 3 - CPedro SousaView Answer on Stackoverflow
Solution 4 - CTodd FreedView Answer on Stackoverflow
Solution 5 - CcodeDomView Answer on Stackoverflow
Solution 6 - Cuser379888View Answer on Stackoverflow