What is the difference between printf("Hello, world!")
and writeln("Hello, world!")
in the D programming language?
I noticed that writeln()
breaks at the end by it self, while printf()
does not. Is that the only difference?
printf
is calling the C function and thus works by C typing rules. Notably, you must get the format string right or you will get nonsense. For example, passing an int
when you specified %s
instead of %d
will likely crash your program.
writef
in D is aware of the types you pass and will thus automatically do the right thing in most cases, or throw an exception when it is impossible instead of corrupting your memory.
writefln
is writef
that automatically adds a new line to the end too.
printf
, like in C, accepts a format string. (ex. printf("number = %d", 123)
prints "number = 123")
writeln
will convert each argument to a string and print them one after the other, and then print a newline. (ex. writeln("number = ", 123)
prints "number = 123")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With