Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

Changes between Version 1 and Version 2 of PrintfExample/D2

Show
Ignore:
Author:
Andrej08 (IP: 78.2.53.69)
Timestamp:
09/09/10 00:42:17 (14 years ago)
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • PrintfExample/D2

    v1 v2  
    33''Part of'' TutorialFundamentals 
    44 
    5 It's better to use writef these days, but here's a printf example just for the heck of it. 
     5You should prefer using writef, but in case you want to use printf you should be aware of a few corner cases. 
     6Here's the classic printf example (you have to import ''core.stdc.stdio'' to use printf): 
    67 
    78{{{ 
    910import core.stdc.stdio; 
    1011 
    11 int main(string[] args
     12void main(
    1213{ 
    1314    printf("Hello World\n"); 
    14     return 0; 
    1515} 
    1616}}} 
    1717 
    18 Here's the same thing with writef: 
     18Here's the equivalent code using writef: 
    1919 
    2020{{{ 
    2222import std.stdio; 
    2323 
    24 int main(string[] args
     24void main(
    2525{ 
    2626    writef("Hello World\n"); 
    27     return 0; 
    2827} 
    2928}}} 
    3029 
    31 However when using the ''%s'' formatting token with printf you must be careful to use it with the embedded length qualifier. This is because the difference between C++ and D is that C++ strings are zero-terminated character arrays referenced by an address, but in D they are a dynamic array object which is really an eight-byte structure containing length and pointer.  
    32  
    33 Note however that a string '''literal''' in D ( for example, the first argument to printf ) is a null-terminated string. 
     30When using the '''%s''' formatting token with printf you must be careful to use it with the embedded length qualifier: '''%.*s'''.  This is because the difference between C and D is that C strings are zero-terminated character arrays referenced by an address, but in D they are a dynamic array object which is really an eight-byte structure containing a length and pointer: 
    3431 
    3532{{{ 
    3734import core.stdc.stdio; 
    3835 
    39 int main(string[] args
     36void main(
    4037{ 
    41     printf("%.*s\n", args[0])
    42     //~ printf("%s\n", args[0]);  // <<-- This will fail. 
    43     return 0; 
     38    string mystring = "hello"
     39    printf("%.*s\n", mystring); 
     40    //~ printf("%s\n", mystring);  // <<-- This would fail in runtime. 
    4441} 
    4542}}} 
     43 
     44The printf's '''%.*s''' will print until the length is reached or an embedded 0 is encountered, so D strings with embedded 0's will only print up to the first 0.  
     45 
     46When you absolutely must pass a string variable to a C function (such as printf, or maybe an external c function), you can use the '''std.string.toStringz''' function. However the string you pass to ''toStringz'' should not have an embedded zero, or you will have problems: 
     47 
     48{{{ 
     49#!d 
     50import core.stdc.stdio; 
     51import std.string; 
     52 
     53void main() 
     54{ 
     55    string mystring = "hi\n"; 
     56    string oops = "I have an embedded \0, oops!"; 
     57     
     58    printf("%s", toStringz("Hello, world!\n")); 
     59    printf("%s", toStringz(mystring)); 
     60    printf("%s", toStringz(oops)); 
     61} 
     62}}} 
     63 
     64Output: 
     65{{{ 
     66Hello, world! 
     67hi 
     68I have an embedded 
     69}}}