A few questions about C programming language

AI Thread Summary
A compiler reads a program by processing the source code character by character in the order it appears in a text file, typically top to bottom and left to right for human readability. When a function, such as main(), is called by the operating system, parameters are pushed onto the stack, and a call is made that includes a return address, facilitating the function's execution. Upon encountering a "return 0;" statement, the program does not stop executing entirely; instead, it returns control to the calling function, allowing the program to continue from a different point. If the current function is main and it is not called by another function, this indicates the end of the program, as it returns an exit code to the operating system. The compiler continues processing until the entire code is compiled or an error is encountered, regardless of return statements.
jaydnul
Messages
558
Reaction score
15
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
 
Technology news on Phys.org
lundyjb said:
1. In what direction does a compiler read a program? Left to right, top to bottom?
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).

lundyjb said:
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?
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.

lundyjb said:
When the compiler hits "return 0;" does the program stop executing completely?
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:
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.
 
lundyjb said:
1. In what direction does a compiler read a program? Left to right, top to bottom?

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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top