John Leuenhagen John Leuenhagen - 6 months ago 66
C++ Question

Write function inline (not inline function) C/C++

I was wondering if I could write something like a function inline, or maybe more like a block with a return statement. Here's an example of what I'm thinking:

int main(int argc, char* argv[])
printf("result is '%s'\n",
char buffer[100];
//Do some code here to determine string
return buffer;

return 0;


What you're looking is lambda function.

Note that lambda functions have been introduced since C++11, so you should have a compatible compiler (almost all recent compiler support them, nowadays).

This is just a little example:

#include <string>
#include <iostream>

int main(int argc, char* argv[]) {
  // Define a closure - note use 'auto' in order to auto-determine the type.
  auto my_lambda = []() -> std::string {
    return std::string("This is a string");

  std::cout << "Result: " << my_lambda() << std::endl;

  return 0;

Additional notes: this is C++. C does not have that kind of stuff.