use strict; use warnings; use Inline 'C'; print greeting('Ingy'); __END__ See https://metacpan.org/dist/Inline-C/view/lib/Inline/C/Cookbook.pod and the example for "greeting" subroutine. "I would urge you to stay away from mallocing your own buffer. Just use Perl's built in memory management. In other words, just create a new Perl string scalar. The function newSVpv does just that. And newSVpvf includes sprintf functionality. The other problem is getting rid of this new scalar. How will the ref count get decremented after we pass the scalar back? Perl also provides a function called sv_2mortal. Mortal variables die when the context goes out of scope. In other words, Perl will wait until the new scalar gets passed back and then decrement the ref count for you, thereby making it eligible for garbage collection. See perldoc perlguts. In this example the sv_2mortal call gets done under the hood by XS, because we declared the return type to be SV*." __C__ SV* greeting(SV* sv_name) { return (newSVpvf("Hello %s!\n", SvPV(sv_name, PL_na))); }