Confusion in c program?

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

    Confusion in c program?

    Code:
    struct emp
    {
    char name[20];
    int age;
    };
    fun(int aa)
    {
    int bb;
    bb=aa*aa;
    return(bb);
    }
    main()
    {
    int cc=fun(15);
    printf("%d",cc);
    }
    I have a prob in that if i donot define the prototype of an function why this program runs correctly plz explain........ ...
  • Dheeraj Joshi
    Recognized Expert Top Contributor
    • Jul 2009
    • 1129

    #2
    You need to mention the return type of the function.

    Code:
    int fun(int);
    Whether your prototype looks like above?
    BTW, Do you get expected output?

    Regards
    Dheeraj Joshi

    Comment

    • AnagJohari
      New Member
      • May 2010
      • 96

      #3
      Originally posted by dheerajjoshim
      You need to mention the return type of the function.

      Code:
      int fun(int);
      Whether your prototype looks like above?
      BTW, Do you get expected output?

      Regards
      Dheeraj Joshi
      ya i get the expected output . u can check urself also....

      Comment

      • Dheeraj Joshi
        Recognized Expert Top Contributor
        • Jul 2009
        • 1129

        #4
        How do you define function prototype?
        In your code i can not see any prototyping.

        Regards
        Dheeraj Joshi

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          A function definition effectively declares the function prototype as well.

          Since you only call the function after it is declared there is no need to a prototype the function as well.

          Comment

          • AnagJohari
            New Member
            • May 2010
            • 96

            #6
            Originally posted by Banfa
            A function definition effectively declares the function prototype as well.

            Since you only call the function after it is declared there is no need to a prototype the function as well.
            but i did not mention the return type of an function why its work properly

            Comment

            • myusernotyours
              New Member
              • Nov 2007
              • 188

              #7
              It works because where there is no type, C defaults to int. So your function is similar to one that explicitly declares an int return type.

              This behaviour is actually removed from the current C standard but compilers still support it for the sake of old code. It should not be relied on in new code.

              Actually you could even have a variable like this...
              Code:
              mytyplessvar = 0; //This will actually be treated as an int.
              Regards,

              Alex.

              Comment

              • AnagJohari
                New Member
                • May 2010
                • 96

                #8
                Originally posted by myusernotyours
                It works because where there is no type, C defaults to int. So your function is similar to one that explicitly declares an int return type.

                This behaviour is actually removed from the current C standard but compilers still support it for the sake of old code. It should not be relied on in new code.

                Actually you could even have a variable like this...
                Code:
                mytyplessvar = 0; //This will actually be treated as an int.
                Regards,

                Alex.
                but about the prototype its not declare in that program but program works correctly

                Comment

                • weaknessforcats
                  Recognized Expert Expert
                  • Mar 2007
                  • 9214

                  #9
                  OK here goes.

                  This is an integer:

                  Code:
                  int value;
                  Now you can say:

                  Code:
                  value = 10;
                  No problem so far. However, let's assume you have a second implementation file and you want to use the value integer in that other file. Since the integer is outside the file, you code:

                  Code:
                  extern int value;
                  Now you can say:

                  Code:
                  value = 20;
                  and the compiler will mark value as external and this lets the linker locate the actual integer and make this code use the integer in the other file.

                  Now just do the same with a function:

                  Code:
                  int MyFunction(int a, int b)
                  {
                        int answer;
                        /* do some calculation here */
                        answer = a + b;
                        return answer;
                  }
                  
                  int main()
                  {
                      int result = MyFunction(3,4);
                  }
                  This works fine because the compiler has seen the function. If this function happens to be in another implementation file then, like the int, you have to tell the compiler that the function is outside this file:

                  Code:
                  extern int MyFunction(int a, int b);
                  
                  int main()
                  {
                      int result = MyFunction(3,4);
                  }
                  and the compiler lets the linker find the function to make the call. The extern is a default for functions and you need not supply it. When you leave it off you have:

                  Code:
                  int MyFunction(int a, int b);
                  
                  int main()
                  {
                      int result = MyFunction(3,4);
                  }
                  and that is what is called a function prototype.

                  Comment

                  • AnagJohari
                    New Member
                    • May 2010
                    • 96

                    #10
                    Originally posted by weaknessforcats
                    OK here goes.

                    This is an integer:

                    Code:
                    int value;
                    Now you can say:

                    Code:
                    value = 10;
                    No problem so far. However, let's assume you have a second implementation file and you want to use the value integer in that other file. Since the integer is outside the file, you code:

                    Code:
                    extern int value;
                    Now you can say:

                    Code:
                    value = 20;
                    and the compiler will mark value as external and this lets the linker locate the actual integer and make this code use the integer in the other file.

                    Now just do the same with a function:

                    Code:
                    int MyFunction(int a, int b)
                    {
                          int answer;
                          /* do some calculation here */
                          answer = a + b;
                          return answer;
                    }
                    
                    int main()
                    {
                        int result = MyFunction(3,4);
                    }
                    This works fine because the compiler has seen the function. If this function happens to be in another implementation file then, like the int, you have to tell the compiler that the function is outside this file:

                    Code:
                    extern int MyFunction(int a, int b);
                    
                    int main()
                    {
                        int result = MyFunction(3,4);
                    }
                    and the compiler lets the linker find the function to make the call. The extern is a default for functions and you need not supply it. When you leave it off you have:

                    Code:
                    int MyFunction(int a, int b);
                    
                    int main()
                    {
                        int result = MyFunction(3,4);
                    }
                    and that is what is called a function prototype.
                    if the return type is struct then we have to define the prototype whether i use this function the same file or in diffrent file is it true.......

                    weaknessforcat

                    Comment

                    • weaknessforcats
                      Recognized Expert Expert
                      • Mar 2007
                      • 9214

                      #11
                      That is false.

                      If a function returns a struct, then the struct must be declared before you use it or you get a compile error. This has nothing to do with where the code for the function is.

                      This is OK on a header file:

                      Code:
                      struct Data
                      {
                           int a;
                           int b;
                      };
                      
                      Data MyFunction( int a, int b);  /* a prototype */
                      You just follow the rule that you can refer to nothing that hasn't been previously declared or you will get a compile time error.

                      In the above case, the function is coded in another file. In that other file you must again declare the struct. Usually, by #including the same header file again. Each implementation file is separately compiled so each file must have a complete set of declarations.

                      Comment

                      • donbock
                        Recognized Expert Top Contributor
                        • Mar 2008
                        • 2427

                        #12
                        Life will be simpler if you get in the habit of always providing function prototypes for all of your functions. Prototype for global functions should be in a header file. Prototypes for local (static) functions should be near the top of the source file.

                        True, there are situations where the prototype is not strictly needed, but I would rather spend scarce brain cells on more important issues than trying to figure out if I can get away with omitting a particular function prototype.

                        Comment

                        • AnagJohari
                          New Member
                          • May 2010
                          • 96

                          #13
                          Originally posted by donbock
                          Life will be simpler if you get in the habit of always providing function prototypes for all of your functions. Prototype for global functions should be in a header file. Prototypes for local (static) functions should be near the top of the source file.

                          True, there are situations where the prototype is not strictly needed, but I would rather spend scarce brain cells on more important issues than trying to figure out if I can get away with omitting a particular function prototype.
                          so the 7 lines u mention in the code its define in header file.
                          ur means if i define the prototype of an function in header files
                          & this header file i use in other file
                          so in that file we have to define the prototype of an function again & after that, we give the defnition of that function......
                          so what is the use of header file if each & every thing we have to define again.. which we already define in a header file.

                          suppose i write the following code
                          Code:
                          struct emp 
                          { 
                          char name[20]; 
                          int age; 
                          }; 
                          emp fun(emp aa) 
                          { 
                          return(aa);
                           
                          } 
                          main() 
                          { 
                          emp bb,dd;
                          bb={"anang",23};
                          dd= fun(bb);
                          printf("%s %d",dd.name,dd.age); 
                          
                          }
                          ur mean this prongram works without defining the prototype of an function ...
                          but my question remain as its in case of int type function why we donot need to define the prototype of an function...

                          Comment

                          • donbock
                            Recognized Expert Top Contributor
                            • Mar 2008
                            • 2427

                            #14
                            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();
                                    ...
                                    }

                            Comment

                            • AnagJohari
                              New Member
                              • May 2010
                              • 96

                              #15
                              Originally posted by weaknessforcats
                              OK here goes.

                              This is an integer:

                              Code:
                              int value;
                              Now you can say:

                              Code:
                              value = 10;
                              No problem so far. However, let's assume you have a second implementation file and you want to use the value integer in that other file. Since the integer is outside the file, you code:

                              Code:
                              extern int value;
                              Now you can say:

                              Code:
                              value = 20;
                              and the compiler will mark value as external and this lets the linker locate the actual integer and make this code use the integer in the other file.

                              Now just do the same with a function:

                              Code:
                              int MyFunction(int a, int b)
                              {
                                    int answer;
                                    /* do some calculation here */
                                    answer = a + b;
                                    return answer;
                              }
                              
                              int main()
                              {
                                  int result = MyFunction(3,4);
                              }
                              This works fine because the compiler has seen the function. If this function happens to be in another implementation file then, like the int, you have to tell the compiler that the function is outside this file:

                              Code:
                              extern int MyFunction(int a, int b);
                              
                              int main()
                              {
                                  int result = MyFunction(3,4);
                              }
                              and the compiler lets the linker find the function to make the call. The extern is a default for functions and you need not supply it. When you leave it off you have:

                              Code:
                              int MyFunction(int a, int b);
                              
                              int main()
                              {
                                  int result = MyFunction(3,4);
                              }
                              and that is what is called a function prototype.
                              if i write the following code
                              Code:
                              struct emp MyFunction(int a, int b) 
                              {   
                                  code here
                                return(struct emp aa} 
                              }
                                
                              int main() 
                              { struct emp bb;
                                  bb = MyFunction(3,4); 
                              }
                              this is will work ? we donot define the function prototype
                              according to u compiller already see the function....
                              here we talk about single source file...

                              Comment

                              Working...