In C Language, Compile-Time and Runtime refer to different phases of program execution. Compile-time when the source code is converted into machine-readable binary code by a compiler and Runtime when the compiled program is executed by the computer.
This is the time when the source code is converted into machine-readable binary code by a compiler.
During compilation, syntax errors and some semantic errors are detected. If there are any errors, the compiler generates error messages, and the program is not compiled successfully.
The compiler performs various tasks such as syntax checking, type checking, and optimization.
Variables and functions are declared, memory is allocated for them, and the code is translated into machine code.
Compile time optimizations can be performed by the compiler to generate efficient machine code.
#include <stdio.h> int main() { printf("Hello, World!\n") return 0; }
the "semicolon" is missing at the end of the "printf" statement. During compilation, the "compiler" will detect this syntax error and generate an error message indicating the "missing semicolon".
#include <stdio.h> int main() { int age = "Hello, World"; // Trying to assign a string to an integer printf("%d\n", age); return 0; }
Here, we're trying to assign a "string literal" to an "integer variable age". During compilation, the compiler will detect "type" mismatch and generate an error.
This is the time when the compiled program is executed by the computer.
During runtime, the program interacts with the environment, including user inputs, system resources, and other external factors.
Variables are assigned values, functions are called, and operations are performed on data.
Errors that occur at runtime, such as division by zero or accessing invalid memory locations, are known as runtime errors. These errors typically lead to program termination or unexpected behaviour.
Performance optimizations, such as loop unrolling and function inlining, may also occur during runtime through techniques like Just-In-Time (JIT) compilation or dynamic linking.
#include <stdio.h> int main() { int dividend = 10; int divisor = 0; int result = dividend / divisor; // Division by zero printf("Result: %d\n", result); return 0; }
we're trying to divide an "integer by zero". This operation is not allowed in C.
If we compile and run this code, it will compile successfully, but during runtime, it will encounter a division by zero error.
#include <stdio.h> int main() { int arr[5] = {1, 2, 3, 4, 5}; printf("%d\n", arr[10]); // Accessing invalid memory (out of bounds) return 0; }
Here, we're trying to access an element of the array "arr" that is beyond its bounds.
This leads to accessing invalid memory, which can result in undefined behaviour at runtime.
compile time occurs before the program is executed and involves translating the source code into machine code, while runtime occurs during program execution and involves the actual processing of instructions by the computer.
Both phases are crucial in the software development process, each serving different purposes.