Thread: Just tell me why this happens.

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    12

    Just tell me why this happens.

    Code:
    #include<stdio.h>
    #include<conio.h>
    void get()
    {
    printf("Hello\n");
    }
    main()
    {
    clrscr();
    get(10,20);
    getch();
    return 0;
    }
    
    When a call is made to the get fn with (any number of) auguments it works. Why is that happening? Help me out please.

  2. #2
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    Because you defined get with an undefined number of arguments. Try void get(void).

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    12
    The same gives errors in c++.Is that the default type of augument in c is void and c++ is not void?

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by lydiapeter
    The same gives errors in c++.Is that the default type of augument in c is void and c++ is not void?
    I think so (I could be wrong)... specifying no arguments when you create your function "get" as you have done for C means that the function accepts an undefined number of arguments (as already mentioned by Brian). In C++ however, specifying no arguments means that the function does not accept any arguments at all and you will get errors if you try to call the function with any arguments.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    C has declarations and prototypes. They are seperate things. C++ has declarations and prototypes too, however they are the same thing.

    In C: T fn() is a declaration. It provides no information on the parameters and that basicaly means anything goes. T fn(void) in C is a declaration, and a prototype. All prototypes are declarations, not all declarations are prototypes. This is a prototype because it provides type information for the parameters (in this case no parameters).

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    With void get(), you are neglecting to provide information about the arguments, so the compiler accepts any number. void get(void) tells the compiler that this function takes no arguments.

    On the same line, you should declare main like this:
    Code:
    int main(void) {
        /* ... */
        return 0;
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed