Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5

What does a compiler do besides run a C++ program? And how?

#1
Ostronomos Offline
As I understand it, when we define a variable in C++ without initializing it, the variable is default initialized. The value it is given depends on where it is defined (such as within a function body or outside it, within the class defined by the standard library, or within the built-in type defined by the language itself).

On page 41 of C++ Primer 5th edition the topic "Variable Definitions" is introduced to its readers. It says that to define a variable we must provide a type specifier (which, as the name says, specifies a type), provide a name to the object (or variable) and separate it with commas, and finally end it with a semi-colon. Note that programmers distinguish between named and unnamed objects with the former being called a variable.

The variable gives named storage which the program manipulates as data. The type of an object, as mentioned before, tells us what the data mean and what operations can be performed on the data. The std namespace allows us to avoid inadvertent collisions between names we define and names defined in the standard library. All the names defined in the standard library are given the std namespace, such as std:Confusedtring, std::cout, std::cin and std::err.

My Question: what does a compiler do to this data besides run a program sequentially by first calling a function, and what does that look like?
Reply
#2
Syne Offline
It's not the value that is defined by it's location; it's the variable's scope that is defined by its location. If the variable is defined within a function, its scope (where it can be accessed) is only within that function...unless that function passes it, as an argument, to another called function.

The value type must be declared when it is initialized...at least in strongly typed programming languages. In dynamically typed languages, you don't have to declare the variable type, and it can be changed on the fly.

Compilers translate human-readable code into machine code instructions, like binary. In many cases this executes faster than human-readable code. But a compiler doesn't run the code. When a compiled program is executed, the OS runs the code. In most languages, only very simple code will execute 100% sequentially, as gotos and functions will send execution to other parts of the code (returning after executing a function or not when executing a goto).
Reply
#3
confused2 Offline
Hi Ostro,

When you start programming you'll probably use a thing called an 'Integrated Design Environment' or 'IDE' which simplifies life a lot. You write your program in some sort of editor and then you press 'run' or F9 or whatever the IDE uses to to make a program compile and run. Before actually running the program the IDE will normally check that it looks sensible - lines end properly and so on even before it attempts to compile it. The compiler is a program in itself that converts the program you have written into code that the microprocessor actually uses. The compiler creates a file that is the program that will actually run - if the compiler hasn't found errors and refused to create a program. Once you have your compiled version you can 'run' it. Often, to start with, you will 'run' the program within the IDE - the IDE will usually insert extra code into your program to make it easier to debug. For example if you have a division by zero error the IDE (on a good day) will take you to the line number in your program where the error occurred and try to tell you what went wrong. Normally a program won't know anything about the source code that generated it and would just crash with (possibly) a division by zero error report.
IDEs are your friend to start with. Once you have a program that compiles and runs in the IDE you can save the compiled code and run it like any other program.

The traditional first program is one that prints out "Hello World" - once you've got that working (hopefully) everything becomes much clearer.
Reply




Users browsing this thread: 1 Guest(s)