Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

A few questions about C programming language

  1. Feb 23, 2013 #1
    1. In what direction does a compiler read a program? Left to right, top to bottom?

    2. What does it mean when a function is called by another. Like when main() is called by the operating system, what is going on within the computer? Does the OS send a code in binary or what?

    3. When the compiler hits "return 0;" does the program stop executing completely? Or does it continue to read the program from a different point?

    Thanks
     
  2. jcsd
  3. Feb 23, 2013 #2

    rcgldr

    User Avatar
    Homework Helper

    It reads the source code top to bottom, then each line left to right. The order of execution of operators depends on precedence and parenthesis (if present).

    For a stacked based evironment, parameters are pushed onto the stack, then a "call" is made, which pushes the return address onto the stack, and a branch is made to the function.

    There is some code linked with a program that is not generated from the programmer's source code. This code calls main, passing the command line parameters (int argc, char **argv). When main returns, that code then returns to the system, via a system call, which is an usually X86 interrupt instruction (INT hex 21 with AH register = hex 4C) in the case of MSDOS or Windows. The return value from main is passed back to the operating system as an "exit code".
     
    Last edited: Feb 23, 2013
  4. Feb 26, 2013 #3
    I have the feeling the 3rd question may have been even more basic than rcgldr's interpretation of it:

    1) The compiler does not stop upon encountering a return statement. It will just continue compiling until the whole code is compiled or errors are encountered.

    2) The running program, when encountering a "return 0;" statement (more precisely: the code generated from the statement), will leave the current function and return zero to the function that called it. In that sense the program does indeed continue at a different point, afterwards.

    3) In the case that the current function is "main", which itself is not called from another function, this implies that the program ends.
     
  5. Feb 26, 2013 #4

    Borek

    User Avatar

    Staff: Mentor

    I would say - neither. It follows the information as it is ordered in a text file, character by character. We tend to represent it top-down, left-right, but it is just a matter of our convention, compiler doesn't care about the way we display the information.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: A few questions about C programming language
Loading...