Prototype Declaration

This is a discussion on Prototype Declaration within the C Programming forums, part of the General Programming Boards category; 1...The given code has no prototype declaration for the function 'printer', but it still compiles. Is the prototype of a ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    7

    Prototype Declaration

    1...The given code has no prototype declaration for the function 'printer', but it still compiles. Is the prototype of a function optional?

    2...The result on running is a huge real number with many zeros?

    3...The code doesn't build if i change the parameter of 'printer' to 'float num'?
    (though the current code compiles fine)
    Code:
    #include <stdio.h>
    
    int main() {
       // int printer(double);     -----------> i removed the prototype declaration
        printer(10);
        return 0;
    }
    int printer(double num){
        printf("Hello!   %f  ",num);
        return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,165
    Quote Originally Posted by rrahulvverma
    1...The given code has no prototype declaration for the function 'printer', but it still compiles. Is the prototype of a function optional?

    2...The result on running is a huge real number with many zeros?
    At the call site, the return type and parameter types will be assumed to default to int, which in this case (and more generally) is not what you want.

    Quote Originally Posted by rrahulvverma
    3...The code doesn't build if i change the parameter of 'printer' to 'float num'?
    (though the current code compiles fine)
    I cannot say that I am very clear on why this is so, but rather than search and tell you the exact reason, the simple solution is: don't do it, i.e., ensure that a declaration of the function is available at the point the function is called.
    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
    May 2009
    Posts
    2,747
    Function prototypes are supposed to be before the functions that use them; Never inside any function like main.

    Tim S.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,165
    Quote Originally Posted by stahta01
    Function prototypes are supposed to be before the functions that use them; Never inside any function like main.
    Actually, unless the function declaration is also a function definition, a function declaration is permitted within the body of a 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

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,762
    Are 1,2,3 your quiz questions which you're expected to answer "yes" or "no" to?
    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.

  6. #6
    Registered User
    Join Date
    Jul 2010
    Posts
    7
    Quote Originally Posted by Salem View Post
    Are 1,2,3 your quiz questions which you're expected to answer "yes" or "no" to?
    No!

    (and actually, except for my first 'quiz' question, a "yes/no" answer for the other ones might sound a little stupid, i guess)

    Anyways, your sense of humor is appreciated!

  7. #7
    Registered User
    Join Date
    Jul 2010
    Posts
    7
    Thanks 'laserlight'!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 08-16-2010, 11:00 AM
  2. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 11:17 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. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM

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