0
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"?

2
  • The "b" indicates a byte-string. printf expects a string of (single, ASCII) bytes, not unicode points (which make up the default strings, i.e., non-"b" strings, in Python).
    – 9769953
    Commented Mar 24, 2021 at 11:56
  • Okay, Thanks a lot Commented Mar 24, 2021 at 12:37

1 Answer 1

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.