Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between printf() and writeln() in D

Tags:

d

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?

like image 817
Niklas Avatar asked Feb 09 '23 13:02

Niklas


2 Answers

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.

like image 55
Adam D. Ruppe Avatar answered Feb 11 '23 02:02

Adam D. Ruppe


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

like image 39
Colonel Thirty Two Avatar answered Feb 11 '23 02:02

Colonel Thirty Two