Call C functions in Rust by declaring them in an extern "C" block and invoking them within an unsafe block.
Use the extern "C" block to declare the C function signature, then wrap the call in an unsafe block to execute it.
extern "C" {
fn c_function_name(arg: i32) -> i32;
}
unsafe {
let result = c_function_name(42);
}
This tells the compiler to use the C calling convention and disables safety checks for the function call.
Using extern "C" functions in Unsafe Rust lets your code talk to functions written in C. You must declare the function's name and inputs so Rust knows how to call it. Because Rust cannot check if the C function is safe, you must explicitly mark the call as unsafe to proceed.