user1393258
user1393258

Reputation:

python and cdll in the ctypes. cannot use the printf

I'm a beginner in learning Python. The version of python I'm using is 3.2.1.1 I'm trying to learn the ctypes by following the tutorial from docs.python.org

in the intereactive prompt,

import ctypes
libc = cdll.msvcrt
printf = libc.printf
printf("%d", 42)

it should return the value 42 but in my case, it returns 0. So what's the problem? Thank very much.


Now after I add a >>>from ctypes import cdll, the result became shows this

>>>from ctypes import *
>>>libc = cdll.msvcrt 
>>>printf = libc.printf 
>>>printf("%d", 42)
Traceback (most recent call last):
  File "<stdin>", line1, in <module>
TypeError: 'CDLL' object is not callable

Upvotes: 3

Views: 1373

Answers (1)

kichik
kichik

Reputation: 34704

Actually, it should return 2 as the number of bytes written to stdout. And it seems to work fine on my Windows installation (after I add the missing from ctypes import cdll). Are you using Windows? msvcrt is a Windows only DLL.

Upvotes: 2

Related Questions