Difference Between Compile Time and Run Time in C

Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!

Learn via video course

C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
By Prateek Narang
Free
star5
Enrolled: 1000
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
Prateek Narang
Free
5
icon_usercirclecheck-01Enrolled: 1000
Start Learning

Overview

A compiler translates the entire program written in a high-level language to machine language before execution, and the process of translation of high-level language to machine language is known as compilation. The code is just translated into the machine-level language but not evaluated during the compilation.

Errors encountered during the compilation of programs are known as compilation errors. Runtime is the period of time when a program is running, and the errors encountered at this time are known as runtime errors.

Scope of the Article

  • In this article, we will look at the difference between compile-time vs run-time.
  • Some of the errors that are encountered at the compile-time and run-time.
  • We will also understand the different types of compile-time errors, such as syntax and Semantic errors. And also errors at the run-time.

Introduction

Computers understand only binary language, If we want to communicate with computers, we have to use binary language. Imagine writing a program to add two numbers in just 1's and 0's. Sounds impossible, right? To overcome this problem, we write codes in a high-level language. Writing a program in a high-level language might be easy for us, but the computer doesn't understand the high-level language. To fix this problem, we make use of compilers. A compiler translates the entire program written in a high-level language to machine language before execution.

Let us take an example of a robot. The robot responds to a set of commands for which they are programmed. Suppose you give a command for which the robot is not programmed. The robot won’t do anything but go blank. Even computers understand only a few words; if you say something the computer does not know, it'll get confused and shows an error.

With this basic knowledge, It will be easy for us to understand compile-time vs run-time errors.

Before jumping into the difference between compile-time vs run-time errors, Let us understand what is compile time.

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.