Runtime vs. Compile time
Solution 1:
The difference between compile time and run time is an example of what pointy-headed theorists call the phase distinction. It is one of the hardest concepts to learn, especially for people without much background in programming languages. To approach this problem, I find it helpful to ask
- What invariants does the program satisfy?
- What can go wrong in this phase?
- If the phase succeeds, what are the postconditions (what do we know)?
- What are the inputs and outputs, if any?
Compile time
- The program need not satisfy any invariants. In fact, it needn't be a well-formed program at all. You could feed this HTML to the compiler and watch it barf...
- What can go wrong at compile time:
- Syntax errors
- Typechecking errors
- (Rarely) compiler crashes
- If the compiler succeeds, what do we know?
- The program was well formed---a meaningful program in whatever language.
- It's possible to start running the program. (The program might fail immediately, but at least we can try.)
- What are the inputs and outputs?
- Input was the program being compiled, plus any header files, interfaces, libraries, or other voodoo that it needed to import in order to get compiled.
- Output is hopefully assembly code or relocatable object code or even an executable program. Or if something goes wrong, output is a bunch of error messages.
Run time
- We know nothing about the program's invariants---they are whatever the programmer put in. Run-time invariants are rarely enforced by the compiler alone; it needs help from the programmer.
-
What can go wrong are run-time errors:
- Division by zero
- Dereferencing a null pointer
- Running out of memory
Also there can be errors that are detected by the program itself:
- Trying to open a file that isn't there
- Trying find a web page and discovering that an alleged URL is not well formed
- If run-time succeeds, the program finishes (or keeps going) without crashing.
- Inputs and outputs are entirely up to the programmer. Files, windows on the screen, network packets, jobs sent to the printer, you name it. If the program launches missiles, that's an output, and it happens only at run time :-)
Solution 2:
I think of it in terms of errors, and when they can be caught.
Compile time:
string my_value = Console.ReadLine();
int i = my_value;
A string value can't be assigned a variable of type int, so the compiler knows for sure at compile time that this code has a problem
Run time:
string my_value = Console.ReadLine();
int i = int.Parse(my_value);
Here the outcome depends on what string was returned by ReadLine(). Some values can be parsed to an int, others can't. This can only be determined at run time
Solution 3:
Compile-time: the time period in which you, the developer, are compiling your code.
Run-time: the time period which a user is running your piece of software.
Do you need any clearer definition?
Solution 4:
(edit: the following applies to C# and similar, strongly-typed programming languages. I'm not sure if this helps you).
For example, the following error will be detected by the compiler (at compile time) before you run a program and will result in a compilation error:
int i = "string"; --> error at compile-time
On the other hand, an error like the following can not be detected by the compiler. You will receive an error/exception at run-time (when the program is run).
Hashtable ht = new Hashtable();
ht.Add("key", "string");
// the compiler does not know what is stored in the hashtable
// under the key "key"
int i = (int)ht["key"]; // --> exception at run-time