Problem in finding the output of c programe?

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

    Problem in finding the output of c programe?

    ]hello guys,
    i write a c programe
    Code:
    main()
    {
    extern int fun(float);
    int a;
    a=fun(3.14)
    printf("%d",a);
    getch();
    }int fun(aa) // ------>prob here if i write "float aa" in place of aa then it works otherwise error
    float=aa;//---------> if i write this statement before the int fun(aa) then this programe also show an error.not getting  "aa" a float type
    {
    return((int)aa);
    }
    my prob is if i write the statement "float aa ;" that meanss aa is a variable of float type & i pass this value in a function its not work shown a error.
    can u plz tell the reason why it creates a problem plz explain in detail.?
    Last edited by Banfa; May 12 '10, 08:27 AM. Reason: Added [code]...[/code] tags
  • newb16
    Contributor
    • Jul 2008
    • 687

    #2
    if i write the statement "float aa ;" that meanss aa is a variable of float type & i pass this value in a function
    No. If you write
    Code:
    float aa;
    int fun(float aa)
    {
      return aa;
    }
    the 'aa' in the function argument list has nothing to do with global variable aa, and within the function its argument 'aa' is used and not the global variable.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      Code:
      float=aa;
      This is just plain wrong. I'm going to assume the equals sign is a typo in your post and isn't really in the code.

      Code:
      main();
         {
         extern int fun(float);
         ...
         }
      
      int fun(aa)
         float aa;
         {
         ...
         }
      Line 3 is a function prototype for fun, but it is located within main. This is legal, but it is contrary to typical C style conventions. Most people will put all of their function prototypes near the front of the source file before any functions are defined. Why do you include the keyword extern?

      The definition of fun on lines 7-8 is an "old-style" definition. Don't do that!

      Comment

      • AnagJohari
        New Member
        • May 2010
        • 96

        #4
        Originally posted by donbock
        Code:
        float=aa;
        This is just plain wrong. I'm going to assume the equals sign is a typo in your post and isn't really in the code.

        Code:
        main();
           {
           extern int fun(float);
           ...
           }
        
        int fun(aa)
           float aa;
           {
           ...
           }
        Line 3 is a function prototype for fun, but it is located within main. This is legal, but it is contrary to typical C style conventions. Most people will put all of their function prototypes near the front of the source file before any functions are defined. Why do you include the keyword extern?

        The definition of fun on lines 7-8 is an "old-style" definition. Don't do that!
        if i donot mention the returntype of an function in fun(aa)
        then i takes by default int return type is it true?/////////////
        by the way i practice on finding the output of an programe thatswhy i extern keyword i mentioned ............

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          You should use a "new-style" function definition. It really isn't that new -- this style has been in use since 1989. I'll use library function malloc as an example.

          Old-style function definition:
          Code:
          void *malloc(size)
             size_t size;
             {
             ...
             }
          New-style function definition:
          Code:
          void *malloc(size_t size)
             {
             ...
             }
          Function prototype:
          Code:
          void *malloc(size_t size);
          Notice how similar the function prototype and new-style function definition are.

          Comment

          • AnagJohari
            New Member
            • May 2010
            • 96

            #6
            Originally posted by donbock
            You should use a "new-style" function definition. It really isn't that new -- this style has been in use since 1989. I'll use library function malloc as an example.

            Old-style function definition:
            Code:
            void *malloc(size)
               size_t size;
               {
               ...
               }
            New-style function definition:
            Code:
            void *malloc(size_t size)
               {
               ...
               }
            Function prototype:
            Code:
            void *malloc(size_t size);
            Notice how similar the function prototype and new-style function definition are.
            but in india most of the books mention such sort of way to define the function.
            actually first time i see this method of defining the function as well as declaring the function.

            Comment

            Working...