Compile Time versus Run Time

Let's break down the concepts of compile time and run time in C with examples and comments.

Compile Time:

Compile time refers to the time when the source code is translated into machine code by the compiler. During this phase, the compiler checks for syntax errors, performs type checking, and generates an executable file.

Example 1: Syntax Error at Compile Time

#include <stdio.h>

int main() {
    // Syntax error: Missing semicolon
    printf("Hello, World!")
    return 0;
}

Explanation:

  • The missing semicolon in the printf statement is a syntax error.

  • The compiler will catch this error during the compilation process.

Example 2: Type Error at Compile Time

#include <stdio.h>

int main() {
    int x = "Hello";  // Type error: Assigning a string to an integer
    printf("%d", x);
    return 0;
}

Explanation:

  • Assigning a string to an integer variable is a type error.

  • The compiler will detect this error during the compilation phase.

Run Time:

Run time refers to the time when the compiled program is executed. During this phase, the program runs and performs its intended tasks.

Example 3: Run-Time Error

#include <stdio.h>

int main() {
    int a = 10, b = 0;
    int result;

    // Run-time error: Division by zero
    result = a / b;

    printf("Result: %d", result);
    return 0;
}

Explanation:

  • Attempting to divide by zero leads to a run-time error.

  • The compiler won't catch this error, but it will occur when the program is executed.

Example 4: Run-Time Behavior

#include <stdio.h>

int main() {
    int x = 5;

    if (x > 0) {
        printf("Positive\n");
    } else {
        printf("Non-positive\n");
    }

    return 0;
}

Explanation:

  • The behavior of the program depends on the user input during runtime.

  • The decision on whether to print "Positive" or "Non-positive" is made during runtime based on the value of x.

Use Cases for Beginners:

  1. Compile Time:

    • Understanding and fixing syntax errors.

    • Resolving type errors by ensuring correct data types.

  2. Run Time:

    • Handling user input and making decisions based on runtime conditions.

    • Dealing with exceptions and errors that occur during program execution.

Some more involved cases:

Compile Time:

Example 1: Static vs. Dynamic Typing

#include <stdio.h>

int main() {
    // Static typing: Detected at compile time
    int staticVar = "Hello";  // Type error

    // Dynamic typing: Checked at runtime (Example using void pointer)
    void* dynamicVar = "Hello";  // No compile-time error, but potential runtime issues

    return 0;
}

Explanation:

  • Static typing errors, like assigning a string to an integer, are detected at compile time.

  • Dynamic typing errors, such as using a void pointer to hold a string, may not be caught until runtime.

Run Time:

Example 2: Memory Allocation Failure

#include <stdio.h>
#include <stdlib.h>

int main() {
    int* arr;

    // Run-time error: Memory allocation failure
    arr = (int*)malloc(sizeof(int) * -1);

    if (arr == NULL) {
        printf("Memory allocation failed\n");
        return 1;
    }

    free(arr);
    return 0;
}

Explanation:

  • The attempt to allocate negative memory size using malloc results in a run-time error.

  • Checking for NULL after dynamic memory allocation is a good practice to handle potential allocation failures.

Example 3: File Input/Output Error

#include <stdio.h>

int main() {
    FILE* file;

    // Run-time error: Unable to open file
    file = fopen("nonexistent_file.txt", "r");

    if (file == NULL) {
        perror("Error");
        return 1;
    }

    fclose(file);
    return 0;
}

Explanation:

  • The attempt to open a nonexistent file results in a run-time error.

  • Checking for NULL after file operations is crucial to handle potential file-related issues.

These advanced examples demonstrate scenarios where issues are detected or arise during run time. Understanding and handling such situations is essential for writing robust and error-resistant C programs.

0
Subscribe to my newsletter

Read articles from Jyotiprakash Mishra directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Jyotiprakash Mishra
Jyotiprakash Mishra

I am Jyotiprakash, a deeply driven computer systems engineer, software developer, teacher, and philosopher. With a decade of professional experience, I have contributed to various cutting-edge software products in network security, mobile apps, and healthcare software at renowned companies like Oracle, Yahoo, and Epic. My academic journey has taken me to prestigious institutions such as the University of Wisconsin-Madison and BITS Pilani in India, where I consistently ranked among the top of my class. At my core, I am a computer enthusiast with a profound interest in understanding the intricacies of computer programming. My skills are not limited to application programming in Java; I have also delved deeply into computer hardware, learning about various architectures, low-level assembly programming, Linux kernel implementation, and writing device drivers. The contributions of Linus Torvalds, Ken Thompson, and Dennis Ritchie—who revolutionized the computer industry—inspire me. I believe that real contributions to computer science are made by mastering all levels of abstraction and understanding systems inside out. In addition to my professional pursuits, I am passionate about teaching and sharing knowledge. I have spent two years as a teaching assistant at UW Madison, where I taught complex concepts in operating systems, computer graphics, and data structures to both graduate and undergraduate students. Currently, I am an assistant professor at KIIT, Bhubaneswar, where I continue to teach computer science to undergraduate and graduate students. I am also working on writing a few free books on systems programming, as I believe in freely sharing knowledge to empower others.