from ctypes import *
libc = CDLL("libc.so.6")
libc.printf("Hello %s!\n", "world")
from sys import stderr
libc.fprintf(stderr.fileno(), "Error in line %d\n", nr)
although I don't know why in the world you would want to use printf instead of native string formatting
Re:Nimrod by Example
by eric_bullington on Friday July 18, 2014 @03:58AM
(#8052095)
I presume the printf was just to show Nimrod's easy C interop.
For instance, I just need to add two lines to import C's printf and fprintf:
proc printf(frmt: CString) {.importc: "printf", nodecl, varargs, tags: [FWriteIO].}
proc fprintf(f: TFile, frmt: CString) {.importc: "fprintf", nodecl, varargs, tags: [FWriteIO].}
Now I can use these C functions seamlessly (even without parentheses):
printf "Hello %s!\n", "world"
fprintf stderr, "Error in line %d\n", $nr