Can I output strings using core.stdc.stdio?

Dave P. dave287091 at gmail.com
Tue Dec 22 21:28:10 UTC 2020


On Tuesday, 22 December 2020 at 21:10:59 UTC, Godnyx wrote:
> Is there a way? If not then how std.stdio does it?

I assume you’re asking this because you don’t have access to 
std.stdio (such as using betterC).

The way to do it is to use the %.*s specifier in printf.

For example:

void print_string(string text){
     printf(“%.*s\n”, cast(int)text.length, text.ptr);
}

The ‘.N' in front of the ’s’ says to not print more than N 
characters from the char*. using a ‘*’ says that the actual 
number of characters will be passed as an argument to printf 
instead of a hardcoded number. This is specified to be an int, so 
we have to cast the length of the string to int when calling 
printf. Finally, we need to pass the pointer to the actual 
character data, thus the text.ptr.




More information about the Digitalmars-d-learn mailing list