Javatpoint Logo
Javatpoint Logo

Compile time vs Runtime

Compile-time and Runtime are the two programming terms used in the software development. Compile-time is the time at which the source code is converted into an executable code while the run time is the time at which the executable code is started running. Both the compile-time and runtime refer to different types of error.

Compile-time errors

Compile-time errors are the errors that occurred when we write the wrong syntax. If we write the wrong syntax or semantics of any programming language, then the compile-time errors will be thrown by the compiler. The compiler will not allow to run the program until all the errors are removed from the program. When all the errors are removed from the program, then the compiler will generate the executable file.

The compile-time errors can be:

  • Syntax errors
  • Semantic errors

Syntax errors

When the programmer does not follow the syntax of any programming language, then the compiler will throw the syntax error.

For example,

int a, b:

The above declaration generates the compile-time error as in C, every statement ends with the semicolon, but we put a colon (:) at the end of the statement.

Semantic errors

The semantic errors exist when the statements are not meaningful to the compiler.

For example,

a+b=c;

The above statement throws a compile-time errors. In the above statement, we are assigning the value of 'c' to the summation of 'a' and 'b' which is not possible in C programming language as it can contain only one variable on the left of the assignment operator while right of the assignment operator can contain more than one variable.

The above statement can be re-written as:

c=a+b;

Runtime errors

The runtime errors are the errors that occur during the execution and after compilation. The examples of runtime errors are division by zero, etc. These errors are not easy to detect as the compiler does not point to these errors.

Let's explore some typical C runtime error types, cases, and their possible effects.

Division by Zero:

Since division by zero is mathematically undefinable, attempting to divide an integer by zero leads to a runtime error. This mistake causes the program to crash or produce an exception. Here's an example:

Output:

Floating point exception (core dumped)

Explanation:

A "Floating point exception" error message is produced when the program encounters a runtime issue because of division by zero.

Accessing Array Out of Bounds:

A runtime error occurs when an array element is accessed outside of certain limits. An error happens when an index is larger than the array's size and memory access laws are broken. Here's an example:

Output:

Segmentation fault (core dumped)

Explanation:

The element at index 10 is beyond the bounds of the array when the program tries to access it. As a result, a segmentation fault occurs, and the program ends with an error.

Null Pointer Dereference:

A runtime error happens when you try to access a null pointer's memory address, which is known as dereferencing a null pointer. Accessing null pointers results in unpredictable behavior because they do not point to legitimate memory locations. Here's an example:

Output:

Segmentation fault (core dumped)

Explanation:

The attempt to dereference a null pointer results in a segmentation fault, which causes the program to crash with an error message.

Stack Overflow:

A stack overflow happens when the call stack grows larger than intended, containing information about function calls. An infinite recursion usually results when recursive functions lack appropriate termination criteria. Here's an example:

Output:

Segmentation fault (core dumped)

Explanation:

The program starts an endless recursion, which overflows the stack and causes a segmentation fault.

Unused variables:

Because uninitialized variables have undefined values, using them might result in runtime errors. The program could provide surprising results or crash, depending on the circumstance. Here's an example:

Output:

Some random value (varies each time)

Explanation:

In this example, the value of an uninitialized variable can be any random value chosen randomly from the memory region designated to that variable.

Let's look at the differences between compile-time and runtime:

Compile-time Runtime
The compile-time errors are the errors which are produced at the compile-time, and they are detected by the compiler. The runtime errors are the errors which are not generated by the compiler and produce an unpredictable result at the execution time.
In this case, the compiler prevents the code from execution if it detects an error in the program. In this case, the compiler does not detect the error, so it cannot prevent the code from the execution.
It contains the syntax and semantic errors such as missing semicolon at the end of the statement. It contains the errors such as division by zero, determining the square root of a negative number.

Example of Compile-time error

In the above code, we have tried to print the value of 'a', but it throws an error. We put the colon at the end of the statement instead of a semicolon, so this code generates a compile-time error.

Output

Compile time vs Runtime

Example of runtime error

In the above code, we try to divide the value of 'b' by zero, and this throws a runtime error.

Output

Compile time vs Runtime

Conclusion:

In conclusion, the periods of software development known as compile-time and runtime errors are separate, and each has a unique set of faults that might occur. Compile-time errors happen when the code is converted into executable form during the compilation step. These errors include semantic errors, which produce illogical or absurd code, and syntax faults, which go against the laws of the programming language. These errors are identified by the compiler and reported, blocking the execution of the code until they are fixed.

On the other hand, runtime errors occur when a program is running and are not caught by the compiler. They can result from several conditions, including division by zero, erroneous memory access, or other unforeseen events. Runtime errors are more difficult to discover and debug since they frequently result in program crashes or unexpected behavior. To elegantly manage runtime errors and guarantee the program's stability, developers use error-handling techniques like exception handling.





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA