c - Scope of a function when defined within another function -


what happen when function defined within function? difference between code 1 , code 2?

code 1:

#include<stdio.h>  void m();  void main() {     m();     void m()     {         printf("hi");     } } 

code 2:

#include <stdio.h>  void m();  void main() {     void m()     {         printf("hi");     }      m(); } 

when compiled code 1 in gcc compiler, got linkage error. when compiled code 2, didn't error. got output "hi". want know how compilation done, when write function definition within function. know when write function definitions not within function, wherever function definition, not error when call function. for example see below code:


code 3:

#include <stdio.h> void m(); void main() {     m(); }  void m() {     printf("hi"); }  

in code 3,even though called function before definition, didn't show error , got output. why not happening code 1.

neither c nor c++ allows define function within function. seems using special language extension of compiler gcc allows define local functions inside functions.

in first program function m declared in file scope. definition searched compiler in file scope. when function called inside main file scope declaration visible. compiler did not find file scope definition issued error.

in second program there declaration of function m in file scope. inside function main there declaration of function m has block scope , hides declaration file scope. take account definition declaration. call of m refers function m declared inside main , has block scope. compiler has function definition , can call it.

in third program satisfies c standard inside main call of m refers function m declared in file scope , there definition of function in file scope. there no problem.


Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -