function declared inside other

This is a discussion on function declared inside other within the C Programming forums, part of the General Programming Boards category; here's a code Code: #include <stdio.h> void f2(void); int main(void) { void f1(void); f1(); f2(); getch(); return 0; } void ...

  1. #1
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804

    function declared inside other

    here's a code

    Code:
    #include <stdio.h>
    void f2(void);
    int main(void)
    {
    	void f1(void);
    	f1();
                    f2();
    	getch();
    	return 0;
    }
    void f2(void)
    {
    	void f3(void);
    	f1();
    }
    void f1(void)
    {
    	printf("BEN10");
    	f3();
    }
    
    void f3(void)
    {
    	printf("DESTINY");
    }
    the output of the code is
    BEN10DESTINYBEN10DESTINY
    how can this be coz i'm declaring f1() inside main and calling it in f2(),same for f3().i'm declaring f3() inside f2() and calling it in f1().why is this code not giving any error?
    in my opinion as f2() is declared outside of all functions thus it can be used anywhere but f1() and f3() are declared inside specific functions, so they can be used in that function only?
    correct me if i'm wrong anywhere.
    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,265
    You can, but typically should not, call functions before declaring them.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    i don't get you

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,265
    Quote Originally Posted by BEN10
    i don't get you
    It's okay, I do not entirely get the implicit function declaration rules for C either, and as far as I know that feature is not part of C99.

    Try compiling with the maximum warning level for your compiler and take note of the warning messages generated.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by BEN10 View Post
    i don't get you
    The compiler will allow you to call functions that aren't known (or put another way: without a prototype). But it's recommended that you have prototypes for all functions before using the function. Failure to do this may lead to hard to discover bugs in the code (and certainly will give warnings if warnings are enabled, which is always a good reasin in itself - you should compile your code with warnings).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by matsp View Post
    The compiler will allow you to call functions that aren't known (or put another way: without a prototype).
    Mats
    in the same code if i define f2() after f1() then the compiler gives error.
    Code:
    Error	3	error C2371: 'f3' : redefinition; different basic types
    also how to set my compiler with maximum warning level.i'm using visual studio 2005.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,001
    Project properties -> C/C++ -> General -> Warning Level -> Set to Level 4.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > I do not entirely get the implicit function declaration rules for C either
    AFAIK, they default to "int xxx()" which is, "int xxx(int arg1, int arg2, ..., int argN)" for C89 at least.

  9. #9
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by zacs7 View Post
    "int xxx()" which is, "int xxx(int arg1, int arg2, ..., int argN)" for C89 at least.
    Are you sure that arguments should have this type?

    The following works fine...

    Code:
    #include <stdio.h>
    
    struct sample { int a; double b;};
    
    int main ()
    {
    	double x = 0.5;
    	int y = 7;
    	const char* z = "test";
    	struct sample  w = {5,7.8};
    
    	f(x,y,z,w);
    
    	return 0;
    }
    
    int f(double x,int y,const char* z,struct sample  w )
    {
    	printf("x = %.2f, y = %d, z= %s, w = {%d,%.2f}\n", x,y,z,w.a,w.b);
    	return 0;
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  10. #10
    Registered User
    Join Date
    Apr 2009
    Location
    Turkey
    Posts
    12
    As far as I know ANSI C does not allow such declerations.


    Quote Originally Posted by BEN10 View Post
    here's a code

    Code:
    #include <stdio.h>
    void f2(void);
    int main(void)
    {
    	void f1(void);
    	f1();
                    f2();
    	getch();
    	return 0;
    }
    void f2(void)
    {
    	void f3(void);
    	f1();
    }
    void f1(void)
    {
    	printf("BEN10");
    	f3();
    }
    
    void f3(void)
    {
    	printf("DESTINY");
    }
    the output of the code is
    BEN10DESTINYBEN10DESTINY
    how can this be coz i'm declaring f1() inside main and calling it in f2(),same for f3().i'm declaring f3() inside f2() and calling it in f1().why is this code not giving any error?
    in my opinion as f2() is declared outside of all functions thus it can be used anywhere but f1() and f3() are declared inside specific functions, so they can be used in that function only?
    correct me if i'm wrong anywhere.
    Thanks

  11. #11
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,057
    If it's any consolation to you, the code doesn't compile on Unix (at least with the compiler I have).

  12. #12
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    so what i understood till now is that any function's scope is from the point it is declared till the end of the file.i mean if i declare any function(say f1()) inside any other function(say f2()) then i can use f1() from the point of declaration to the end of the file whether i call it in some other function's definition.if i call f1() from any other fucntion defined before f2() then it'll be an error.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,265
    Quote Originally Posted by BEN10
    so what i understood till now is that any function's scope is from the point it is declared till the end of the file.
    Then what about getch()? You call getch() without including the appropriate header that declares it.

    zacs7 reminded me about the default int rule, so consider:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        f1();
        f2();
        return 0;
    }
    
    int f2(void)
    {
        f1();
        return 0;
    }
    
    int f1(void)
    {
        printf("BEN10");
        f3();
        return 0;
    }
    
    int f3(void)
    {
        printf("DESTINY");
        return 0;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by laserlight View Post
    Then what about getch()? You call getch() without including the appropriate header that declares it.

    zacs7 reminded me about the default int rule, so consider:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        f1();
        f2();
        return 0;
    }
    
    int f2(void)
    {
        f1();
        return 0;
    }
    
    int f1(void)
    {
        printf("BEN10");
        f3();
        return 0;
    }
    
    int f3(void)
    {
        printf("DESTINY");
        return 0;
    }
    yes.i was also about to ask the same question that how the program runs without including the proper header for getch(),which i think is conio.h.and also why you have changed the prototype to int for every function.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,001
    The compiler would assume a default prototype of
    int fnc(...)
    So no type checking on the arguments and the return type is int.
    (If a proper declaration is not found before call.)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer to a class inside the WindowsProcedure function
    By like_no_other in forum Windows Programming
    Replies: 3
    Last Post: 06-01-2009, 01:52 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-30-2005, 12:21 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21