How to Call C Functions from Rust

Call C functions in Rust by declaring them with extern "C" and invoking them inside an unsafe block.

You call C functions from Rust by declaring the external function with extern "C" and then invoking it within an unsafe block. This tells the Rust compiler to use C calling conventions and disables safety checks for the interaction.

extern "C" {
    fn printf(format: *const i8, ...) -> i32;
}

fn main() {
    unsafe {
        let c_str = b"Hello\0";
        printf(c_str.as_ptr() as *const i8);
    }
}