{"id":2555,"date":"2024-05-10T07:23:20","date_gmt":"2024-05-10T07:23:20","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=2555"},"modified":"2024-05-10T07:23:20","modified_gmt":"2024-05-10T07:23:20","slug":"nested-functions-in-c","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/nested-functions-in-c\/","title":{"rendered":"Nested Functions In C"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#nested-functions-in-c\">Nested Functions In C<\/a><\/li><li ><a href=\"#nested-function-and-lexical-scoping\">Nested Function And Lexical Scoping <\/a><\/li><li ><a href=\"#example-1-to-illustrate-the-nested-function-in-c\"> Example 1:  To illustrate the Nested Function in C<\/a><\/li><li ><a href=\"#example-2-nested-function-with-the-help-of-gcc-extension\">Example 2: Nested Function with the help of GCC extension<\/a><\/li><li ><a href=\"#faq-nested-functions-in-c\">FAQ- Nested functions in C<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"nested-functions-in-c\">Nested Functions In C<\/h2>\n\n\n\n<p>In C programming, we can talk about something called &#8220;nested functions.&#8221; It&#8217;s like having a smaller function inside a bigger one. However, in C, these nested functions work a bit differently than in some other languages. They can&#8217;t do everything you might expect, and they have some rules to follow. This article will explain what nested functions in C are all about and how they work.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"nested-function-and-lexical-scoping\">Nested Function And Lexical Scoping <\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Nested Function<\/strong>: In programming, a nested function refers to a function defined inside another function. In some programming languages, like Python or JavaScript, nested functions can access variables from their containing (parent) function. However, in C, true nested functions are not supported. You can declare a function inside another function, but it won&#8217;t have access to the local variables of the containing function.<\/li>\n\n\n\n<li><strong>Lexical Scoping<\/strong>: Lexical scoping, also known as static scoping, is a way to determine the scope (visibility) of variables in a program. In lexical scoping, the scope of a variable is determined by its location in the source code, and it can access variables from its containing function. While C does have lexical scoping for variables, it doesn&#8217;t extend this feature to nested functions.<\/li>\n<\/ol>\n\n\n\n<p> In C, you can declare a function inside another function, but this is not the same as true nested functions with lexical scoping. The inner function in C can only access global variables from the containing module, not the local variables of the containing function. This limitation is due to how C handles variable scope.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-1-to-illustrate-the-nested-function-in-c\"> Example 1:  To illustrate the Nested Function in C<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C program to illustrate the \n\/\/ concept of Nested function. \n#include &lt;stdio.h&gt; \nint main(void) \n{ \n    printf(\"Main\"); \n    int fun() \n    { \n        printf(\"fun\"); \n  \n        \/\/ defining view() function inside fun() function. \n        int view() \n        { \n            printf(\"view\"); \n        } \n        return 1; \n    } \n    view(); \n} <\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Compile time error: undefined reference to `view'\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-2-nested-function-with-the-help-of-gcc-extension\">Example 2: Nested Function with the help of GCC extension<\/h2>\n\n\n\n<p>In GCC (GNU C Compiler), there is an extension that allows you to declare nested functions. However, to use this extension, you need to start the declarations of these nested functions with the &#8220;auto&#8221; keyword as a prefix. This extension enables you to define functions inside other functions, which is not a standard feature in C but can be useful in certain programming scenarios.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\/\/ C program of nested function \n\/\/ with the help of gcc extension \n#include &lt;stdio.h&gt; \nint main(void) \n{ \n    auto int view(); \/\/ declare function with auto keyword \n    view(); \/\/ calling function \n    printf(\"Main\\n\"); \n  \n    int view() \n    { \n        printf(\"View\\n\"); \n        return 1; \n    } \n  \n    printf(\"Skillvertex\"); \n    return 0; \n} <\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>view\nMain\nSkillvertex<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"faq-nested-functions-in-c\">FAQ- Nested functions in C<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1696834826902\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. How to use Nested functions in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. In standard C, you cannot define a function within another function. While you can declare a function within a function in C, it is not considered a true nested function as it doesn&#8217;t have access to the local variables of the containing function.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1696834841728\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2.What is a Nested Function with an example?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. A nested function is like a function within a function. You can have them in your program, and what makes them special is that they can work with and change the variables that belong to their parent functions. This makes them different from other types of functions.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1696834848783\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. What is Nested structure in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. In C programming, a nested structure is a structure that is declared within another structure. It allows you to create complex data structures by combining multiple structures. \u00a0Syntax: struct name_1<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Nested Functions In C In C programming, we can talk about something called &#8220;nested functions.&#8221; It&#8217;s like having a smaller function inside a bigger one. However, in C, these nested functions work a bit differently than in some other languages. They can&#8217;t do everything you might expect, and they have some rules to follow. This &#8230; <a title=\"Nested Functions In C\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/nested-functions-in-c\/\" aria-label=\"More on Nested Functions In C\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":5355,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[430],"class_list":["post-2555","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-nested-functions-in-c","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2555","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=2555"}],"version-history":[{"count":11,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2555\/revisions"}],"predecessor-version":[{"id":10655,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2555\/revisions\/10655"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/5355"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=2555"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=2555"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=2555"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}