Can I output strings using core.stdc.stdio?

Godnyx rempas at tutanota.com
Tue Dec 22 21:37:23 UTC 2020


On Tuesday, 22 December 2020 at 21:28:10 UTC, Dave P. wrote:
> 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.

Lol. Actually I just don't want to use Phobos and trying to stay 
on core. Unfortunately, my variable can't be read at compile time 
so I doesn't work. Any other ideas?


More information about the Digitalmars-d-learn mailing list