A few questions about C programming language...


by Jd0g33
Tags: language, programming
Jd0g33
Jd0g33 is offline
#1
Feb23-13, 02:49 PM
P: 266
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
Phys.Org News Partner Science news on Phys.org
Cougars' diverse diet helped them survive the Pleistocene mass extinction
Cyber risks can cause disruption on scale of 2008 crisis, study says
Mantis shrimp stronger than airplanes
rcgldr
rcgldr is offline
#2
Feb23-13, 03:42 PM
HW Helper
P: 6,929
Quote Quote by lundyjb View Post
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).

Quote Quote by lundyjb View Post
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.

Quote Quote by lundyjb View Post
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".
Timo
Timo is offline
#3
Feb26-13, 11:49 AM
P: 301
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.

Borek
Borek is online now
#4
Feb26-13, 01:11 PM
Admin
Borek's Avatar
P: 22,705

A few questions about C programming language...


Quote Quote by lundyjb View Post
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.


Register to reply

Related Discussions
Best programming language? Academic Guidance 22
Which programming language is best? Academic Guidance 12
Recommended programming language (and texts) for middle-school beginner programming Programming & Computer Science 9
Programming language Programming & Computer Science 10
Which programming language do you use? Academic Guidance 20