1

printf doesn't output anything

google colab cell:

%%writefile my_functions.c
#include <stdio.h>
#include <stdlib.h>
void my_void_func() {
printf("my_void_func called from C!\n");
}

compiling:

!gcc -shared -fPIC -o my_functions.so my_functions.c

here goes python part :

import ctypes
libc = ctypes.CDLL("libc.so.6")
my_lib = ctypes.CDLL("./my_functions.so") 
my_lib.my_void_func.restype = None
my_lib.my_void_func()
10
  • To clarify: You're trying to define a function my_void_func in C, and then load that library into Python and call the function from there? Also, you're declaring libc but not doing anything with it - is that intentional? Commented Sep 4 at 15:13
  • yes, defining a function my_void_func in C, compiling code with gcc into shared file .so , and then calling function from python . Commented Sep 4 at 15:22
  • 1
    I found similar question on Juputer forum which suggests to use module wurlitzer to catch and redirect output from printf() but sometime it works for me and sometimes not. It is answer from 2021 and maybe it needs something more. When calling printf by ctypes, jupyter does not show output from printf - Notebook - Jupyter Community Forum. I tested it only with local Jupyter Lab Commented Sep 4 at 16:20
  • 1
    Capturing C-level stdout/stderr with wurlitzer | notebook.community Commented Sep 4 at 16:28
  • 1
    Try taking a look at stackoverflow.com/questions/34035643/… and return the string from the C function to python, so that python can then return it to google. from a quick look, google colab wants the value returned from the python script, not output from the script. Commented Sep 4 at 20:40

1 Answer 1

2

(Note - OP responded that this is not the solution. Leaving it here for the record of what didn't work.)

The STDOUT stream in C (to which printf sends its output) is buffered; the buffer's contents get sent to the output when the buffer fills up, when the buffer is manually flushed, when the associated stdio file object is closed, or, in a standard C program, when the main program exits. Try adding fflush(STDOUT) after the printf().

Sign up to request clarification or add additional context in comments.

6 Comments

I saw many other posts that fflush(STDOUT) should help , but no result. It doesn't output anything into cell after python code
printf is not buffered. The standard output stream is buffered. However, if it is directed to the terminal, it should be line-buffered or unbuffered, in which case output ending with '\n' should be sent to the terminal immediately.
you're right about stream-vs-printf. i did not assume that the "google colab cell" ran in a pty.
@EricPostpischil But I don't think stdout is connected to a terminal in this case, it's probably being piped to something in Jupyter.
Nonetheless, the information in this answer is incorrect about printf, so it should be corrected. Additionally, OP’s subsequent update reports that fflush does not help, so this answer is not the solution for the problem.
I tried different suggestions that had no result : fflush(stdout); disable buffering at the start: setbuf(stdout, NULL); or setvbuf(stdout, NULL, _IONBF, 0); , adding a newline character (\n).

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.