Runtime vs compile time in C
In the C programming language, the often used terms in every step consist of compile time and runtime. The compile time is referred to the source code that will be converted into an executable code; on the contrary, the runtime will be considered when the executable code starts running. Compile time and runtime differ in many ways, such as types of error and so on.
Compile time: It is referred to as a program’s time to compile the program followed by some operations. The operations are followed by a part of the step known as a compiler. The operations are done to convert the code written by a developer or a programmer in a particular programming language.
- There is no need for a program to satisfy any variants. It needn’t be a well formed program.
- Always keep an eye on syntax errors, type checking errors, compiler crashes (happens rarely) as they might lead to a bug further in the program.
- If a compiler succeeds, then the program is well formed and is a meaningful program, and you can proceed to the next step, running the program.
Runtime: Before coining the term runtime, writers used the phrase “when a program is run”, and due to this repeated usage of the phrase came the word runtime. It is used to refer to a program when it is running. In a program life cycle, a program’s total time starting to run in the memory until the user or the operating system terminated the program is known as the runtime.
- The runtime will not know about the program’s variants. Anything a programmer or a developer types in will be considered.
- Runtime variants are rarely enforced by a compiler alone. It needs external help from the programmer.
- Always keep an eye on errors such as division by zero, dereferencing a null pointer, and running out of memory.
- There might be errors detected only by the program, such as trying to open a file that is not available and finding a web page that does not have a well formed URL (Uniform Resource Locator).
- If a runtime succeeds, then the program is considered successful, and it finishes without any crashing.
- Inputs and outputs are dependent upon a developer or a programmer. Files, windows on the screen, network packets, commands to a printer, anything a programmer wishes to do will happen if the program runs successfully.
Compile time errors:
Compile time errors are the errors that will occur when a programmer or a developer writes a wrong syntax. If we write the wrong syntax or semantics, then the compile-time errors will be thrown by the compiler. The compiler will not let the program run until all the errors are removed from the program. Once all the errors are removed from the program, then the compiler will generate the executable file.
Compile time errors include:
1. Syntax errors: when a developer does not follow the programming language’s syntax, the compiler will throw a syntax error.
E.g.:
#include <sdtio.h>
#include <conio.h>
int main()
{
int a = 4, b = 3, c:
c = a + b;
printf (“Sum is: %d \n”, c );
return 0;
}
In the above example, it can be seen that the declaration ends with a colon (:) instead of a semicolon (;), which is a syntax error.
2. Semantic errors: Semantic errors occur when the statements are not meaningful to the compiler.
E.g.:
#include <sdtio.h>
#include <conio.h>
int main()
{
int a = 4, b = 3, c:
a + b = c;
printf (“Sum is: %d \n”, c );
return 0;
}
In the above example, the sentence ‘a + b = c’ throws a compile time error. In the above statement, the programmer assigns the value ‘c’ to the summation of ‘a’ and ‘b’, which is not possible in the C programming language as it can contain only one variable on the left to the assignment operator while the right side of the operator can contain more than one variable.
Runtime errors:
These are errors that occur during the execution and after compilation. Some of the examples of runtime errors are division by zero, and so on. The errors are not easily detectable as the compiler does not point precisely to them.
Difference between compile time and runtime:
Compile time | Runtime |
The compiler prevents the code from execution if the the compiler detects an error in the program. | The compiler will not detect any error, and hence it cannot prevent the code from any execution happening further. |
These are the errors that are produced at compile time and are detected by the compiler only. | These are the errors which are not generated by the compiler and produce an unpredictable result at the execution time. |
It contains the syntax and semantic errors like missing semicolon at the end of any statement. | It may contain errors such as division by zero, determining the square root of any negative number. |