Confusion in c program?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AnagJohari
    New Member
    • May 2010
    • 96

    #16
    Originally posted by donbock
    Are you familiar with the notion of constructing a program from several source files?

    A function that you want to use in more than one source file needs to be a global function. You will want to have a prototype for that function in each source file that calls it; and that prototype must appear first, before the function is called. You could type the prototype into each source file, but that is tedious. Instead, it is more common to put all of the prototypes for your global functions into a header file and then include that header in each source file.

    A function that is only used in one source file need not be global. My personal policy is to make all such functions local (that is, static). It is also my practice to have prototypes for all the static functions near the top of my source file.

    Code:
    program.h:
        struct emp {
            ...
            };
    
        int func1(void);
        struct emp func2(int a);
    
    
    file1.c:
        #include "program.h"
        static int func3(int a);
    
        int func1(void) {
            int val;
            ...
            val = func3(4);
            ...
            }
    
        static int func3(int a) {
            struct emp w;
            ...
            vv = func2(a);
            ...
            }
    
    
    file2.c:
        #include "program.h"
    
        ...
        struct emp func2(int a) {
            int value;
            ...
            value = func1();
            ...
            }
    tnx i m getting ur pointttttt .... can u plz see my orginal Question which i send
    in that program prototype is not defined but programs work . plz clear my that thing also.....

    by the way i m not familler construction a prog from several source file . but i get ur point.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #17
      struct emp MyFunction(int a, int b)
      {
      code here
      return(struct emp aa}
      }

      int main()
      { struct emp bb;
      bb = MyFunction(3,4) ;
      }
      This will work if struct emp is declared before MyFunction.

      You only need the function prototype if MyFunction is being called in another .c file.

      BTW: Multiple files in C/C++ programs is a way of life. You need to know how to do this.

      Comment

      • AnagJohari
        New Member
        • May 2010
        • 96

        #18
        Originally posted by weaknessforcats
        This will work if struct emp is declared before MyFunction.

        You only need the function prototype if MyFunction is being called in another .c file.

        BTW: Multiple files in C/C++ programs is a way of life. You need to know how to do this.
        ur mean if i write the following code"
        Code:
        struct emp MyFunction(int ,int);
        before ur" MyFunction"
        then its work
        The question remain same this is the prototype of an function.we have to declare the prototype to run this program correctly.....
        but in my original question program run correctly without defining the prototype
        why?///////////

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #19
          That has already been answered in posts 5 and 7 but to summarise
          • Since the definition appears before main, where you call the function and definitions automatically provide the function prototype the function has been prototyped before it was called.
          • This is C therefore it will use a default type of int if no type is specified.


          Not also that since this is C a prototype is not actually required although it is best practice and a lot of modern compilers issue a warning if you call a function without a prototype.

          Comment

          • AnagJohari
            New Member
            • May 2010
            • 96

            #20
            Originally posted by Banfa
            That has already been answered in posts 5 and 7 but to summarise
            • Since the definition appears before main, where you call the function and definitions automatically provide the function prototype the function has been prototyped before it was called.
            • This is C therefore it will use a default type of int if no type is specified.


            Not also that since this is C a prototype is not actually required although it is best practice and a lot of modern compilers issue a warning if you call a function without a prototype.
            sorry i have to send one more reply about this topic.....
            u say that when we give the defnition of an function outside the main
            & call that function in main it automaticaly take the prototype of an function.
            but in case of return type of struct type varibale . i give the defnition of a function outside the main & call it in main then i produce an error
            thats the prob i face...it shuold work but donot...
            than you

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #21
              Let's see your code that not working. Please post it.

              Comment

              • AnagJohari
                New Member
                • May 2010
                • 96

                #22
                Originally posted by weaknessforcats
                Let's see your code that not working. Please post it.
                Code:
                struct emp MyFunction(int a, int b)  
                {    
                    code here 
                  return(struct emp aa}  
                } 
                  
                int main()  
                { struct emp bb; 
                    bb = MyFunction(3,4);  
                }
                donot add functiom prototype just use the defnition of an function & run
                it will not run

                Comment

                • donbock
                  Recognized Expert Top Contributor
                  • Mar 2008
                  • 2427

                  #23
                  Code:
                  struct emp MyFunction(int a, int b)   
                  {     
                      code here  
                    return(struct emp aa}   
                  }
                  What's wrong here?
                  • You need to declare struct emp before this point.
                  • Of course, 'code here' won't work. Presumably you replace this with legitimate C code.
                  • The return statement is wrong. Presumably somewhere in your program you declare variable aa as a struct emp. If so, then all you need to do is return aa.
                  • The return statement ends with a close-brace rather than a close-parenthesis.

                  What do you mean by "it will not run"? Do you get compiler errors? If so, please reproduce them so we can help you. Do you get runtime errors? If so, please reproduce them so we can help you. Does the program give the wrong result? If so, please describe the output you get and the output you expect. Don't make us guess.

                  Comment

                  Working...