Difference Between Compile Time and Run Time in C

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

In programming, compile-time is when high-level code is translated to machine language, identifying syntax and semantic errors. Runtime, conversely, is when the program executes, facing potential runtime errors. Understanding these stages helps grasp their distinct roles and associated errors in software development.

What is Compile Time?

Compile time is the period when the programming code is converted to machine code.

What is compile Time

In the above illustration, We can see that a compiler converts the High-level language into low-level language. If there are any errors in the high-level language, the compiler shows a compilation error.

Compile-time errors

These are the errors that occur at the time of compilation. There are mainly two types of Compile-time errors.

  • Semantic errors.
  • Syntax errors.

Semantic errors

The code having absurd meaning refers to semantic errors. In other words, meaningless statements are termed semantic errors.

Syntax errors

Syntax refers to the rules that define the structure of a language. The syntax error is an incorrect construction of the source code.

What is Run Time?

So far, in our compile time vs runtime journey, we have understood what compile-time is. Now let us understand what is run-time?

Runtime is the period of time when a program is running and generally occurs after compile time.

Run-time errors

These are the errors that occur during the execution of the program. The compiler does not detect the Runtime errors. One of the few basic runtime exceptions is "Array Index Out of Bound." Let us take an example

In the above code, we have declared an array with 6 elements. If we try to access the 10th element in the array, An error is encountered because we have declared 6 elements in the array, and we are trying to access the 10th element, Which does not exist.

The output of the above code is as follows:

We can observe that arr[10] accesses a memory location that stores a garbage value. Such errors are known as array out of bounds in C.

Difference Between Compile Time Errors vs Runtime Errors

Compile-time errorRun-time error
These errors are detected during the compile-timeThese errors are detected at the run-time
Compile-time errors do not let the program be compiledPrograms with run-time errors are compiled successfully, but an error is encountered when the program is executed
Compile-time errors can occur because of wrong syntax or meaningless statementsRun-time errors occur because of absurd operations

Examples of Compile-time Errors and Run-time Errors

Now that we have understood the basic definition of the errors, what is compile time vs runtime, types of errors in compile time vs runtime let's look at some examples to make the concepts clear.

Compile-time errors

Syntax errors

Syntax errors can be of different types, Such as

  • Missing semicolon.
  • Missing Parenthesis (}).
  • Printing the value of a variable without declaring it.

Missing Semicolon

As we can see in the program, we have put ":" instead of ";" which is the wrong syntax. Therefore the compiler will throw a compile-time syntax error something like this.

Semantic Errors

Let's look at an example,

In the above code at line 5, x * y = z; is a semantic error because we know that values are assigned to the left side variable after the execution of the right-hand expression. Here in the above example, we have x*y as the operand on the left-hand side and z on the right-hand side, which is incorrect.

The output for the above code looks something like this:

Missing Parenthesis

Parenthesis plays a significant role in the syntax of the code. An extra or a missing parenthesis can change the logic of the whole code, or sometimes it will yield an error.

In the above example, we can see that the closing parenthesis of the for loop is missed. Let us look at the output of this code.

We can see that the compiler says that a parenthesis is missing.

Printing the value of undeclared variable

Variables are used to store information to be referenced and manipulated in a computer program. If we try to print a variable without declaring it, an error will occur while running the program.

In the above code, it is clear that we declared the variables a and variable b of integer datatype. In line 6, we are trying to print the value of variable c, which had not been declared. Such mistakes will result in a compile-time error.

The output of the above program will be as follows:

Runtime errors

In the above program, we can see that the value of a will be divided by 0 (Because "a" is initialized to "1" and we have "a-a" in the denominator).

This results in a solution that is not defined. Therefore we get a runtime error something like this.

Finally, We are at the end of the topic compile time vs runtime errors. I'm sure you would have understood the concept of compile-time vs runtime errors clearly.

Conclusion

*Compile time is the period when the programming code is converted to the machine code.

  • Compile-time errors are the errors that are encountered at the time of compilation of the program. They are Syntax errors and Semantic errors.
  • Syntax errors are the compile-time errors that occur due to the use of the wrong syntax.
  • Semantic errors occur because of the absurd use of logic.
  • Runtime is the period of time when a program is running and generally occurs after compile time.
  • Run-time errors occur when we try to access index out of bound, 'divide by zero`, etc.