• Programming in C

    From Black Panther@21:1/186 to All on Sunday, July 28, 2019 14:01:26
    Hi All,

    This may be a stupid question, so stupid answers are expected... ;)

    Coming from Pascal, where the order of the code mattered, as in you could not call a function that was physically below where you were in the code, as the compiler didn't know it existed yet.

    Is this also a concern with C?


    ---

    |03B|09lack |03P|09anther|03(|09RCS|03)|07

    --- Mystic BBS v1.12 A43 2019/03/02 (Linux/64)
    * Origin: Castle Rock BBS - bbs.castlerockbbs.com (21:1/186)
  • From Alterego@21:2/116 to Black Panther on Monday, July 29, 2019 08:10:24
    Re: Programming in C
    By: Black Panther to All on Sun Jul 28 2019 02:01 pm

    Hi All,

    This may be a stupid question, so stupid answers are expected... ;)

    Coming from Pascal, where the order of the code mattered, as in you could not call a function that was physically below where you were in the code, as the compiler didn't know it existed yet.


    No, in C you have a .h file where you can predefine your future functions.

    ...лоег
    --- SBBSecho 3.07-Linux
    * Origin: Alterant | An SBBS in Docker on Pi! (21:2/116)
  • From Black Panther@21:1/186 to Alterego on Sunday, July 28, 2019 16:36:28
    On 29 Jul 2019, Alterego said the following...

    Coming from Pascal, where the order of the code mattered, as in you cou not call a function that was physically below where you were in the cod the compiler didn't know it existed yet.

    No, in C you have a .h file where you can predefine your future
    functions.

    That's what I thought, but wanted to make sure... Thank you. :)


    ---

    |03B|09lack |03P|09anther|03(|09RCS|03)|07

    --- Mystic BBS v1.12 A43 2019/03/02 (Linux/64)
    * Origin: Castle Rock BBS - bbs.castlerockbbs.com (21:1/186)
  • From Xenos@21:4/147 to Black Panther on Monday, July 29, 2019 11:24:24
    RE: Programming in C
    BY: Black Panther (21:1/186)

    On Sunday, July 28, 2019 at 13:01, Black Panther (21:1/186) wrote:

    Coming from Pascal, where the order of the code mattered, as in you could n BP>call a function that was physically below where you were in the code, as th BP>compiler didn't know it existed yet.

    Is this also a concern with C?


    In C and C++, you can declare the function prototype before defining it. Usually the header files (*.h) contain the function prototypes that you expect to use. These can be defined elsewhere and the linker handles putting them all together after compilation.

    A .-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-.
    /*\ | /\liens' /\lcove! WWIV on Linux - Taguig, Metro Manila, Philippines |
    / v \`-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=[ WWIV Reg. #30282 ]-=-=-=-=-=-=-=-'

    --- WWIV 5.3.0.dev-xenos
    * Origin: /\lien's /\lcove! WWIV 5 on Linux | aliens.free.net.ph (21:4/147)
  • From Black Panther@21:1/186 to Xenos on Sunday, July 28, 2019 21:46:56
    On 29 Jul 2019, Xenos said the following...

    In C and C++, you can declare the function prototype before defining it. Usually the header files (*.h) contain the function prototypes that you expect to use. These can be defined elsewhere and the linker handles putting them all together after compilation.

    That's what I was thinking. I'm just trying to figure out all of the different quirks with the language... :)

    Thanks


    ---

    |03B|09lack |03P|09anther|03(|09RCS|03)|07

    --- Mystic BBS v1.12 A43 2019/03/02 (Linux/64)
    * Origin: Castle Rock BBS - bbs.castlerockbbs.com (21:1/186)
  • From tenser@21:1/112 to Black Panther on Friday, August 02, 2019 09:52:24
    This may be a stupid question, so stupid answers are expected... ;)

    Coming from Pascal, where the order of the code mattered, as in you
    could not call a function that was physically below where you were in
    the code, as the compiler didn't know it existed yet.

    Is this also a concern with C?

    This has changed over time as C has evolved as a language.

    In the original versions of C, as defined by Dennis Ritchie's PDP-11
    compiler, the default return type was always `int`. So if one referred
    to a function that the compiler hadn't seen before in the translation
    unit (roughly file), then the compiler defaulted to assuming that function returned an `int`. Functions that returned other types, or that didn't
    return any type at all (`void`), had to be forward-declared. There's a
    syntax for doing this in archaic C that one encounters occasionally.
    for example, `extern float foo();`

    As C evolved into the language that ANSI standardized in 1988, this
    default was preserved but compilers would emit warnings for previously undeclared functions. Now, with C11 and the better compilers,
    it is an error to call a function that has been neither previously
    defined nor declared. Best practice is to add prototypes for all
    externally visible functions to a header file. For static-qualified
    functions, which are not visible outside of the translation unit, best
    practice is to either declare those with prototypes at the beginning of
    the file or declare them before first use.

    --- Mystic BBS v1.12 A43 2019/03/03 (Windows/32)
    * Origin: Black Flag <ACiD Telnet HQ> blackflagbbs.com (21:1/112)