C
  • Introduction
    • Fundamentals of a Program
    • Overview of C
    • Features of C
  • Installing Required Software
    • Setting Up VSCode for Windows
    • Setting Up VSCode for macOS
    • Setting Up VSCode for Ubuntu
  • Starting to write code
    • Compiling and Running Your Code
    • Creating Our First C Program
    • Errors and Warnings
    • Program: Writing a C Program to Display Your Name
    • Structure of a C Program
  • Basic Concepts
    • Comments in C
    • Preprocessor in C
    • The #include Statement
    • Displaying Output
    • Reading Input from the Terminal
    • Enums and Chars
    • Data Types and Variables
    • Format Specifiers
    • Command Line Arguments
    • Program: Calculating the Area of a Triangle
  • Operators
    • Converting Minutes to Years and Days
    • Basic Operators
    • Bitwise Operators
    • Program: Byte Sizes of Basic Data Types
    • cast and sizeof Operators
    • Operator Precedence
  • Control Flow
    • If-Else Statements
    • Program: Weekly Pay Calculation
    • Switch Statement
    • For Loop
    • While and Do-While Loops
    • Nested Loops and Loop Control
    • Program: Guess the Number
  • Arrays
    • Introduction to Arrays
    • Program: Prime Number Generator
    • Multidimensional Arrays
    • Program: simple Weather Program
    • Variable Length Arrays (VLAs)
  • Functions
    • Overview of Functions
    • Defining Functions
    • Arguments and Parameters
    • Returning Data from Functions
    • Variable Scoping
    • Program: Tic Tac Toe Game
    • Recursion
  • Strings
    • Defining a String
    • Constant Strings in C
    • Common String Functions
    • Program: Bubble Sort
    • Searching, Tokenizing, and Analyzing Strings
    • Converting Strings
  • Debugging
    • What is Debugging
    • Understanding the Call Stack
    • Common C Mistakes
    • Understanding Compiler Errors
  • Pointer
    • Defining Pointers
    • Accessing Pointers
    • Program: Pointer Demonstration
    • Pointers and Const
    • Void Pointers
    • String Pointers
    • Array Pointers
    • Utilizing Pointers with Functions
    • Pointer Arithmetic
  • Dynamic Memory Allocation
    • malloc, calloc, and realloc
    • Program: User Input String
    • Memory Deallocation
  • Structure
    • Structures and Arrays
    • Nested Structures
    • Structures and Pointers
    • Structures and Functions
    • Program: Structure pointers and Functions
  • File Input and Output
    • Accessing Files
    • Reading from a File
    • Program: Finding the Total Number of Lines in a Text File
    • Writing to a Text File
    • Finding Your Position in a File
    • Program: Converting Characters in a File to Uppercase
    • Program: Printing the Contents of a File in Reverse Order
  • The Standard C Library
    • Various Functions in C
    • Math Functions in C
    • Utility Functions in C
Powered by GitBook
On this page
  • Understanding Compilation
  • Using GCC (GNU Compiler Collection)
  • Running Your Program
  • Handling Compilation and Execution in IDEs
  • Troubleshooting
  • Conclusion

Was this helpful?

  1. Starting to write code

Compiling and Running Your Code

After you've written your C program, the next steps involve compiling and running it to see the results. In this section, we'll explore these processes step by step.

Understanding Compilation

Compilation is the process of translating your human-readable C code into machine-readable instructions. The compiler takes your source code and converts it into an executable file that the computer can run.

Using GCC (GNU Compiler Collection)

GCC, or the GNU Compiler Collection, is a widely used compiler for C programs. Here's how you can compile your C code using GCC:

  1. Open a Terminal or Command Prompt:

    • Navigate to the directory containing your C file using the cd command.

  2. Compile the Code:

    • Use the following command to compile your code:

      gcc yourfile.c -o yourprogram
      • Replace yourfile.c with the name of your C file and yourprogram with the desired name for the executable.

    • Example:

      gcc hello.c -o hello_program
    • This command tells GCC to compile hello.c and create an executable named hello_program.

  3. Check for Errors:

    • If there are any errors in your code, the compiler will display error messages. Review and fix these errors before proceeding.

Running Your Program

Once you've successfully compiled your code, it's time to run the executable.

  1. Execute the Program:

    • On Linux:

      ./yourprogram
    • On Windows:

      yourprogram.exe
    • Example:

      ./hello_program
  2. View the Output:

    • If your program includes output using printf(), you will see the results displayed in the terminal or command prompt.

Handling Compilation and Execution in IDEs

If you're using an Integrated Development Environment (IDE) like Visual Studio Code, Code::Blocks, or Eclipse, the process is often streamlined within the IDE itself. You may find a "Run" or "Build" option that handles both compilation and execution.

Troubleshooting

  • Common Errors:

    • Pay attention to error messages during compilation. They provide valuable information on what needs fixing in your code.

  • Debugging:

    • If your program isn't behaving as expected, consider using debugging tools or adding printf() statements to identify issues.

Conclusion

Understanding the compilation and execution process is essential for every C programmer. As you become more familiar with these steps, you'll be able to write, compile, and run more complex programs with confidence.

Congratulations! You've successfully compiled and run your C program. This marks a significant milestone in your journey as a C programmer. In the following sections, we'll explore more advanced topics to enhance your programming skills.

PreviousStarting to write codeNextCreating Our First C Program

Was this helpful?