from ctypes import *
libc = CDLL("/lib/x86_64-linux-gnu/libc/so/6")
libc.printf(b"Hello world")
I was studying the library ctypes
in Python. In above printf
function why do we use b
before "Hello world"
?
The b"..."
notation represents a byte string. Byte strings are immutable are appropriate for passing to const char *
C function parameters, or char *
if the function is known not to write to the string. Use ctypes.create_string_buffer()
if you need a writeable buffer.
printf
expects a string of (single, ASCII) bytes, not unicode points (which make up the default strings, i.e., non-"b" strings, in Python).