function

This is a discussion on function within the C Programming forums, part of the General Programming Boards category; hi all defining functions inside of main like this Code: main() { int function() { int x=5; return x; } ...

  1. #1
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127

    function

    hi all
    defining functions inside of main like this
    Code:
    main()
    {
    
           int function()
            {
                     int x=5;
                    return x;
            }
    
    
    
            printf("%d %d \n",function(),test());
    }
    it will be only local to main right?
    and my question is :
    is there is any thing wrong with that code i mean
    can it cause any kind of problesm or Undefined behaviour?
    thanks in advance

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    It's non-standard C.
    Any compiler in ANSI mode will just throw it out.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Slightly modified version of the compiler output when I compile:

    stupidcode.c:2: warning: return type defaults to `int'
    stupidcode.c: In function `main':
    stupidcode.c:5: warning: ISO C forbids nested functions
    stupidcode.c:12: warning: implicit declaration of function `printf'
    stupidcode.c:12: warning: implicit declaration of function `test'
    stupidcode.c:13: warning: control reaches end of non-void function
    (.text+0x29):stupidcode.c: undefined reference to `test'
    collect2: ld returned 1 exit status
    See anything that jumps out at you?

  4. #4
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    i am just using
    gcc fu.c -o fu
    and it works with me
    does this mean that gcc is not compiling it in ansi mode?

  5. #5
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    It is unusual for a block-structured language to prohibit you from defining functions inside other functions, but this is one of the characteristics of C. Although it isn't obvious, this helps to improve the run-time performance of C by reducing the housekeeping associated with function calls.
    i got this from the c book
    but i just can't understand why did gcc compiled it

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Try the -ansi and -pedantic switches.

  7. #7
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    gcc -Wall -ansi function.c -o function
    function.c: In function `test':
    function.c:6: warning: implicit declaration of function `function'
    function.c: At top level:
    function.c:12: warning: return type defaults to `int'
    function.c: In function `main':
    function.c:19: warning: control reaches end of non-void function
    wall and ansi options gives my me errors
    thanks

  8. #8
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    -Wall
    tells the compiler to implement 'all' Warning options. Warnings are diagnostic messages that report constructions which are not inherently erroneous but which are risky or suggest there may have been an error
    i got it now
    sorry for my stupid question but i have to ask

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM

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