Called the loop of c from rust, but could not display the function of the loop

Hello everyone, i use rust call c loop(for or while), use the printf display something, but loop can’t display the contents of printf.
c code:

    #include <stdio.h>
    #include <unistd.h>

    void print_c() {
      int s = 3;
      while (1) {
        printf("%d\n", s);
        sleep(1);
      }
    }

However, when using C++, it can be successfully called. Is there any restriction on the use?

c++ code:

#include <iostream>
#include <stdint.h>
#include <unistd.h>

extern "C" {
  int print_it(int32_t num) {
    while (1) {
        std::cout << num << std::endl;
        sleep(1);
    }
  }
}

Add rust code,call c/c++ lib function.
C is packaged as a shared library, named cthread. C++ is named cppthread.

rust call c lib:

#[link(name = "cthread")]
extern {
    fn print_c();
}


fn main() {
    unsafe { print_c() };
}

rust call c++ lib:

#[link(name = "cppthread")]
extern {
    fn print_it();
}


fn main() {
    unsafe { print_it() };
}

I’m not quite sure what you’re asking here. If you want to use Rust to call a C function while inside a loop, that’s quite easy:

use std::os::raw::c_char;
use std::ffi::CString;

extern "C" {
    fn printf(fmt: *const c_char, ...);
}

fn main() {
    let format_string = CString::new("%d\n")
        .expect("Shouldn't contain null bytes");

    for i in 0..10 {
        unsafe {
            printf(format_string.as_ptr(), i);
        }
        
    }
}

(playground)

For more advanced use cases, you probably want to check out existing documentation and guides regarding FFI.

I mean to call c’s loop function from rust using #link(name= “c-lib”) and ffi. But c loop function can’t printf out the content, however c++ is OK.