A few questions about C programming language

In summary: 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?A function is called by another function when its address is pushed onto the stack. This is done by the compiler, when it encounters the function's name in the source code. From there, the call is made, and the return address is pushed onto the stack as well.3. When the compiler hits "return 0;" does the program stop executing completely?No. The compiler will continue to execute the code from where it stopped, because main() returns a value. However, any code
  • #1
jaydnul
558
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
  • #2
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:
  • #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.
 
  • #4
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.
 
  • #5
for your questions about the C programming language. I can provide some insights into these concepts.

1. The direction in which a compiler reads a program depends on the specific implementation of the compiler. Generally, compilers read code from top to bottom, left to right, just like how we read text. However, some compilers may use different strategies for optimizing code, which could affect the order in which they read the program.

2. When a function is called by another, it means that the program is transferring control to that function. In the case of main() being called by the operating system, the OS sends a signal to the program, indicating that it should start executing at the beginning of the main() function. This signal is typically in the form of binary code, as that is the language that computers understand.

3. When the compiler hits "return 0;", the program will stop executing completely. This statement is typically used to indicate that the program has successfully completed its task and can now terminate. The program will not continue to read from a different point, unless there is another function or code segment that is called after the return statement.
 

What is C programming language?

C programming language is a high-level, general-purpose programming language that was developed in the 1970s by Dennis Ritchie at Bell Labs. It is widely used for developing operating systems, system software, embedded systems, and various applications.

What are the main features of C programming language?

Some of the main features of C programming language include its simplicity, efficiency, portability, and flexibility. It also allows for low-level memory manipulation and has a large standard library for various operations.

How is C programming language different from other programming languages?

C programming language is a procedural language, meaning it follows a step-by-step approach to solve problems, whereas other languages like Java and Python are object-oriented. C also allows for direct access to computer hardware, which makes it highly efficient.

What are the common uses of C programming language?

C programming language is commonly used for developing operating systems, system software, device drivers, embedded systems, and various applications. It is also used for developing high-performance software, such as gaming engines and graphics libraries.

Is it difficult to learn C programming language?

Learning C programming language can be challenging for beginners, as it requires a good understanding of computer architecture and low-level programming. However, with practice and dedication, it can be a rewarding language to learn and master.

Similar threads

  • Programming and Computer Science
Replies
8
Views
866
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
2
Replies
65
Views
2K
  • Programming and Computer Science
Replies
29
Views
2K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
2
Replies
59
Views
7K
Replies
6
Views
1K
  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
7
Views
633
  • Programming and Computer Science
Replies
14
Views
2K
Back
Top