c function error

This is a discussion on c function error within the C Programming forums, part of the General Programming Boards category; Code: #include<stdio.h> int main() { fun() { printf("hai\n"); } return 0; } while compiling i getting error as " error: ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    16

    c function error

    Code:
    #include<stdio.h>
    int main()
    {
    fun()
    {
    printf("hai\n");
    }
    return 0;
    }
    while compiling i getting error as " error: expected ; before { token"

    please any one give explanation on above error

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,275
    You are missing a terminating semi-colon.
    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
    Registered User
    Join Date
    Dec 2009
    Posts
    16
    Hi ,

    laselight ,i got ur point .my aim is i want to print the hai from function with int the main with out decaling the function outside.

    can anyone modify my program to print hai with in a main funcion?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,275
    Quote Originally Posted by crocker
    can anyone modify my program to print hai with in a main funcion?
    Eh? What you are asking for seems so simple that you would have done it if you did not have what looks like a call to a function named fun:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        printf("hai\n");
        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

  5. #5
    Registered User
    Join Date
    Dec 2009
    Posts
    16
    ur logic also right.

    but i want to print from a function fun() with in main.

    please can u modify my program to print hai

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,275
    Quote Originally Posted by crocker
    but i want to print from a function fun() with in main.
    In that case, you should define the function fun() to print "hai\n", then call fun() from within the main function.
    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

  7. #7
    Registered User
    Join Date
    Dec 2009
    Posts
    16
    Code:
    int fun(()
    {
    printf("hai \n");
    return 0;
    }
    
    int main()
    {
    fun();
    return 0;
    }
    i getting out put from above program.

    but i cant understanding why i didt getting output from below program

    Code:
    int main()
    {
    fun()
    {
    printf("hai\n");
    }
    return 0;
    }
    please give ur suggestions

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,275
    Quote Originally Posted by crocker
    i getting out put from above program.
    Yes, that looks right except that you should indent your code, and fun() can have a void return type since it does not appear to need an int return type. (Of course, I am assuming that you #include <stdio.h> as you did earlier, and your actual code does not have a typo error.)

    Quote Originally Posted by crocker
    but i cant understanding why i didt getting output from below program
    It is simply syntactically wrong. You are not defining fun(), you are calling it, and forgetting a terminating semi-colon. You then introduce a block of scope in which you call printf(). Note that in standard C, functions cannot be defined in the definition of other functions.
    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

  9. #9
    Registered User
    Join Date
    Dec 2009
    Posts
    16
    "Note that in standard C, functions cannot be defined in the definition of other functions." wt this mean?

    u mean in main function we cant define fun()?

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,275
    Quote Originally Posted by crocker
    u mean in main function we cant define fun()?
    Yes. You could forward declare fun() in the main function, but it is more common to place forward declarations outside of function bodies, like how functions are defined.
    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

  11. #11
    Registered User
    Join Date
    Dec 2009
    Posts
    16
    so af from our discussion , i concluded some points

    1. in c function defination is out function is defined out side of function wiher it is called ?

    am i right.

    its its wrong .please correct

  12. #12
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    You cannot define a function inside another function. You just call it there. You should also have a prototype declaration after the headers have been included.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  13. #13
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    The primary reason is that C is a not a block-structured language like Pascal, so you can't have nested function definitions.

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, 03:07 AM
  2. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  3. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM

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