Thread: subroutine in C

  1. #16
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by Elysia View Post
    Sure, if you only provide a definition, then you must use void in the parameter list.
    Not so. In a definition, an unnamed void argument, and an empty argument list both mean the same thing.

    So this will not compile with a "too many arguments" error:
    Code:
    int func(){
      return 0;
    }
    
    int main(){
      func(1);
    }
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  2. #17
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,786
    Quote Originally Posted by King Mir View Post
    Not so. In a definition, an unnamed void argument, and an empty argument list both mean the same thing.

    So this will not compile with a "too many arguments" error:
    Not in VS2005
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,401
    Quote Originally Posted by vart
    Not in VS2005
    Are you sure you did not compile as C++? It works for me when compiling as C.

    EDIT:
    Wait a minute, I assumed that King Mir meant 'this will not (compile with a "too many arguments" error)' (which does not really make sense, I suppose), but maybe King Mir meant 'this will (not compile) with a "too many arguments" error', in which case the reason why this is not so is that the definition is also a declaration.

    EDIT #2:
    hmm... but the language of C99 seems to indicate otherwise:
    Quote Originally Posted by Section 6.7.5.3, Paragraph 14
    An identifier list declares only the identifiers of the parameters of the function. An empty list in a function declarator that is part of a definition of that function specifies that the function has no parameters. The empty list in a function declarator that is not part of a definition of that function specifies that no information about the number or types of the parameters is supplied.
    The wording "not part of a definition" seems to say that although a definition is a declaration, the fact that it is a definition trumps the fact that it is a declaration for the interpretation of an empty parameter list. If this is so (and also in C90), then gcc and MSVC are both non-conformant in this area.
    Last edited by laserlight; 04-04-2009 at 12:25 AM.
    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

  4. #19
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,786
    I compiled it as C

    VS2005 gives no errors
    gcc port used in CodeBlocks - gives the error specified by King Mir
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,401
    Quote Originally Posted by vart
    gcc port used in CodeBlocks - gives the error specified by King Mir
    What is the version of gcc? The MinGW port of gcc 3.4.5 did not emit such an error for both C90 and C99 mode with -Wall and -pedantic tacked on.
    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

  6. #21
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,786
    Quote Originally Posted by laserlight View Post
    What is the version of gcc? The MinGW port of gcc 3.4.5 did not emit such an error for both C90 and C99 mode with -Wall and -pedantic tacked on.
    ok I played a little with the CodeBlocks installation to see exactly which gcc is configured, what switches are set, and finally - what command line it uses to compile a file.


    Here is it:
    1. gcc is 3.4.5 and it does not show the error. (I have running it from command prompt)
    2. CodeBlocks on some reason starts g++ to compile the c-file


    So - about the second - how do I tell to CodeBlocks that *.c files should be compiled as C and not C++?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,401
    Quote Originally Posted by vart
    So - about the second - how do I tell to CodeBlocks that *.c files should be compiled as C and not C++?
    I think this might work: click on the .c file and select "Properties...". Click on the "Advanced" tab and change "CPP" to "CC" for the "Compiler variable" setting.

    EDIT:
    No, Code::Blocks does not seem smart enough to use gcc instead of g++ in that case.
    Last edited by laserlight; 04-04-2009 at 01:26 AM.
    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

  8. #23
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,653
    Quote Originally Posted by laserlight View Post
    The wording "not part of a definition" seems to say that although a definition is a declaration, the fact that it is a definition trumps the fact that it is a declaration for the interpretation of an empty parameter list. If this is so (and also in C90), then gcc and MSVC are both non-conformant in this area.
    My take on the whole was:
    Code:
    void fun(void); // No parameters
    void fun(); // No parameter information (infinite parameters)
    void fun(void) { } // No parameters
    void fun () { } // No parameters
    I could be wrong, though.
    Although VS would let the last example compile.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calling Java Subroutine
    By thetinman in forum C++ Programming
    Replies: 11
    Last Post: 10-14-2008, 11:52 AM
  2. AIX passwdpolicy() subroutine
    By syndex in forum C Programming
    Replies: 1
    Last Post: 10-06-2008, 06:04 PM
  3. Assembler Language Subroutine 2
    By John_L in forum Tech Board
    Replies: 8
    Last Post: 03-30-2008, 04:48 PM
  4. problem with subroutine
    By pankleks in forum C Programming
    Replies: 2
    Last Post: 11-24-2005, 02:57 AM
  5. fgets in a function subroutine
    By wbeasl in forum C Programming
    Replies: 1
    Last Post: 12-03-2003, 03:51 AM