Home  >  Article  >  Backend Development  >  In a C program, what is the difference between compile-time errors and run-time errors?

In a C program, what is the difference between compile-time errors and run-time errors?

WBOY
WBOYforward
2023-09-11 23:41:021685browse

In a C program, what is the difference between compile-time errors and run-time errors?

An error or exception is a situation where the expected result cannot be achieved due to an interruption in code execution. Depending on the event that generates or identifies the error, we can classify it into compile-time errors and run-time errors.

The following are the important differences between compile-time errors and run-time errors.

Serial number Key Compile time error Run time error
1 Reference Compile-time errors usually refer to errors related to syntax or semantics. On the other hand, runtime errors refer to errors encountered while executing code at runtime.
2 Detection Compile-time errors are detected by the compiler while the code is being developed. Run-time errors are not detected by the compiler and are therefore only recognized when the code is executed.
3 Fixation As mentioned before, compile-time errors can be fixed while the code is being developed. Run-time errors enter the repair state after the code is executed once and the error is recognized.

Examples of compile-time errors and run-time errors

CompileDemo.c

#include<stdio.h>
public class CompileDemo{
   void main(){
      int x = 100;
      int y = 155;
      // semicolon missed
      printf("%d", (x, y))
   }
}

Output

error: expected &#39;;&#39; before &#39;}&#39; token

Example

RuntimeDemo.c

include<stdio.h>
public class RuntimeDemo{
   void main(){
      int n = 9;
      div = 0;
      div = n/0;
      printf("resut = %d", div);
   }
}

Output

warning: division by zero [-Wdiv-by-zero]
div = n/0;

The above is the detailed content of In a C program, what is the difference between compile-time errors and run-time errors?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete