Nested Functions in C
Question: Is it possible to define functions within other functions in C ?
Answer:
Modern C (C 11 or later):
Yes, you can create nested functions using lambdas. Lambdas allow you to define anonymous functions that can capture local variables within their scope.
int main() { auto print_message = [](std::string message) { std::cout << message << "\n"; }; print_message("Hello!"); }
C 98 and C 03:
In C 98 and C 03, directly defining functions within functions is not supported. However, you can use the following technique:
int main() { struct X { static void a() {} }; X::a(); }
While this allows you to create functions inside functions, it's considered a workaround and should be used sparingly due to its potential for obscurity in code comprehension.
The above is the detailed content of Can C Functions Be Nested?. For more information, please follow other related articles on the PHP Chinese website!