A few questions about C programming language

Click For Summary

Discussion Overview

The discussion revolves around fundamental questions regarding the C programming language, specifically focusing on compiler behavior, function calls, and the execution flow of programs. It includes theoretical aspects of how compilers interpret code and practical implications of function execution.

Discussion Character

  • Exploratory
  • Technical explanation
  • Conceptual clarification

Main Points Raised

  • Some participants propose that a compiler reads source code top to bottom and left to right, while others suggest that it processes the information character by character as ordered in a text file.
  • There is a discussion about what happens when a function is called, with some stating that parameters are pushed onto the stack and a "call" is made, while others question the specifics of how the operating system interacts with the program during this process.
  • Participants express differing views on the behavior of the program upon encountering a "return 0;" statement, with some asserting that the program continues executing from a different point, while others clarify that this indicates the end of the program if it is in the main function.

Areas of Agreement / Disagreement

Participants express multiple competing views on how compilers read programs and the implications of function calls and return statements. The discussion remains unresolved with no consensus reached on these fundamental aspects.

Contextual Notes

There are limitations in the discussion regarding the assumptions made about compiler behavior, the specifics of function execution, and the interpretation of return statements, which may depend on the context of the programming environment.

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.
 

Similar threads

Replies
86
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
Replies
65
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 29 ·
Replies
29
Views
4K
  • · Replies 59 ·
2
Replies
59
Views
9K
  • · Replies 17 ·
Replies
17
Views
4K
Replies
6
Views
3K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 122 ·
5
Replies
122
Views
18K