Thread: Prototype Declaration

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  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, 10:00 AM
  2. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10: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, 06:39 AM