Getting Started (Lesson)
Getting Started with C Programming
Welcome to the world of Structured Programming! This chapter will guide you through the process of setting up your development environment and understanding the core tools required to write, compile, and run C programs.
1.1 Understanding the Development Environment
To write C programs, you need two essential tools:
- Text Editor: Where you write your code (e.g., VS Code, Sublime Text, Notepad++).
- Compiler: A special program that translates your human-readable C code into machine code that the computer can execute (e.g., GCC, Clang).
Recommended IDEs (Integrated Development Environments)
An IDE combines a text editor and compiler into one package:
- Visual Studio Code (VS Code): Modern, lightweight, and highly customizable.
- Code::Blocks: A popular choice for beginners with a built-in compiler (MinGW).
- Dev-C++: A classic, simple IDE for Windows.
1.2 Setting up GCC and VS Code
Step 1: Install a Compiler (MinGW for Windows)
- Download the MinGW-w64 installer or MSYS2.
- Install the
mingw-w64-x86_64-gccpackage. - Crucial Step: Add the
binfolder (e.g.,C:\msys64\mingw64\bin) to your System PATH environment variable.
Step 2: Install Visual Studio Code
- Download and install from code.visualstudio.com.
- Open VS Code and go to the Extensions view (
Ctrl+Shift+X). - Install the "C/C++" extension by Microsoft.
Step 3: Verify Installation
Open your terminal or command prompt and type:
gcc --version
If you see the version information, you are ready to go!
1.3 How a C Program Runs
The life cycle of a C program involves several stages:
- Editing: Writing code in a
.cfile. - Preprocessing: Handling
#includeand#definedirectives. - Compilation: Converting C code to Assembly.
- Assembly: Converting Assembly to Object code (
.objor.o). - Linking: Combining object code with libraries to create an Executable (
.exe).
Basic Command to Compile and Run
If your file is named main.c:
# Compilation
gcc main.c -o myprogram
# Execution
./myprogram
Refer to the Lecture Slides for visual diagrams of the compilation process.