Introduction
C programming is a versatile language that allows you to build a wide range of applications. Whether you're a beginner or an experienced programmer, you must know how to set up your development environment and start with basic programming tasks. In this guide, we'll walk you through the process of creating directories and checking for the installation of C on different operating systems. We'll also cover the steps to install C if it's not already available. Finally, we'll demonstrate how to write and compile a simple Hello, World!
program in C. Let's get started!
Code Snippet
Installing Choco
If this is not your first time using Chocolatey or Chocolatey is already installed on your Windows Operating System skip this section.
To install Chocolatey, follow these steps:
- Search for PowerShell and run it as an administrator on Windows.
- Go to the Chocolatey installation page.
- If this is your first time installing Chocolatey on your operating system, execute the following command in your terminal, based on the instructions provided on the installation page:
Get-ExecutionPolicy
- If the output is "Restricted", enter one of the commands below:
Set-ExecutionPolicy AllSigned
or
Set-ExecutionPolicy Bypass -Scope Process
- Regardless of whether "Restricted" was the previous output or not, run the following command (copy and paste it into your terminal):
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
- If there are no errors, check if Chocolatey was successfully installed by running the following command:
choco
If the output displays the version of Chocolatey you are using, it means the installation was successful.
Creating a Directory
To begin, let's create a directory named c-programming
using the command line. Follow the instructions below based on your operating system:
- Open the command prompt or PowerShell.
- Run the following commands:
mkdir basic-programming
mkdir basic-programming/c-programming
- change directory to
c-programming
cd basic-programming/c-programming
Checking if C is Installed
Before you start writing C programs, it's important to check if C is installed on your system. You can verify this by using the gcc
or cc
command, a popular C compiler. Follow the steps below:
- Open the terminal.
- Run the following command:
gcc --version
Note:
You may also include the extension to gcc
. That is gcc
is the same as gcc.exe
.
gcc.exe --version
gcc.exe input.c -o output.exe # optionally omit the .exe
If C is installed, the version information will be displayed. Otherwise, you may be prompted to install the necessary tools or package. For example, for Linux, if gcc
is not installed, running gcc --version
in the terminal typically results in an error message like this:
The program 'gcc' is currently not installed. You can install it by typing:
sudo apt install gcc
For Windows, the error may look like this shown below:
'gcc' is not recognized as an internal or external command, operable program or batch file.
For macOS, the error may look like this shown below:
-bash: gcc: command not found
gcc
or cc
In the article, the
gcc
command will be primarily used for checking the version of the C compiler and compiling C programs. However, it is worth noting thatcc
can be used as an alternative togcc
for the same purposes.While
gcc
is commonly used as the command-line interface to the GNU Compiler Collection, including the C compiler,cc
is often set as a symbolic link or alias togcc
. Therefore, both commands essentially serve the same purpose and can be used interchangeably in most cases.
The choice between using gcc
or cc
may depend on personal preference or specific system configurations. However, in the context of the article, it is safe to assume that the gcc
command will be predominantly used while acknowledging that the cc command could be used as an alternative.
Installing C
If C is not installed on your system, follow the appropriate instructions based on your operating system:
General Installation (VSCode)
If you prefer to use VSCode, you can install it by using a package manager specific to your operating system. For Windows, you can use Chocolatey by running the command
choco install vscode
. macOS users can use brew with the commandbrew install --cask visual-studio-code
. Ubuntu users can use sudo with the commandsudo apt install code
.After installing VSCode, you should install the following extensions: C/C++ Extension Pack and Code Runner.
After the installation is complete, you can launch Visual Studio Code by searching for it in the applications menu or by running the
code
command in the terminal (code
and clickEnter
orReturn
).
Once Code Runner is installed, you will see a play icon beside which is a dropdown.Click on the play dropdown icon. See the image below:
- Open the terminal by pressing
Ctrl + J
. - Click the option that says Run C/C++.
- From the debug configuration options, select the option to build and debug an active file.
You will see the result Hello, World!
In your terminal. Notice the new executable file created during execution - hello.exe
Note that the Keyboard shortcut is Ctrl + Shift + B
.
Now that you have done the configuration, the next time you want to run a C program just click on the play button or use the keyboard shortcut Ctrl + Alt + N
.
Issue
If the installation doesn't work for you, go to the VS Code download page and click the download button based on your operating system - macOS, Linux, Windows and install (click on the executable file after download - accept all default settings).
If the problem persists, download and install Codeblocks. You may navigate to the Binary releases page and download based on your operating system.
Alternatively, use the online Replit text editor.
Note: You can also try VSCode in the browser and still follow along the course.
macOS
On macOS, Homebrew can be used to install C-related tools and libraries. However, Homebrew itself is not specifically used to install the C language since macOS already includes the Clang compiler, which supports C. Here's how you can install Homebrew and verify the availability of Clang:
- Open the terminal.
- Install Homebrew by running the following command:
On macOS, brew might be pre-installed on your system, verify by running the command below:
brew -v
If it is not installed or it shows an error, run the command below:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
- After Homebrew is installed, you can install additional C-related tools and libraries if needed by using Homebrew commands. For example, to install the GNU Compiler Collection (GCC), you can run the following command:
brew install gcc
- To verify the availability of Clang, which comes preinstalled on macOS, run the following command:
clang --version
Remember, on macOS, the default compiler is Clang, which supports C and other programming languages. If you install GCC using Homebrew, you can use it as an alternative to Clang.
Alternative installation on macOS
- Install Xcode from the App Store.
- Open Xcode and navigate to Preferences.
- In the Downloads tab, install the Command Line Tools.
Ubuntu
- Open the terminal.
- Run the following commands
sudo apt-get update
sudo apt-get install build-essential
Note: The sudo
command is used to run the subsequent commands with administrative privileges, which may require you to enter your password.
Windows
To install C on Windows, we'll use the MinGW-w64 development environment, which provides a Windows port of GCC.
Follow these steps:
On Windows, you can use Chocolatey to install the MinGW-w64 development environment, which provides a Windows port of GCC. Here's how you can do it:
- Open the command prompt or PowerShell with administrative privileges.
- Install Chocolatey by following the instructions on the official Chocolatey website.
- Once Chocolatey is installed, run the following command to install MinGW-w64:
choco install mingw
- During the installation, make sure to select the appropriate options as prompted.
For example, if asked to run the script, type a
or all
. It means you want to run all scripts.
Do you want to run the script?([Y]es/[A]ll - yes to all/[N]o/[P]rint): a
- After the installation is complete, you should have the GCC compiler available on your system. You can verify the installation by running the command gcc --version in the command prompt or PowerShell.
Alternative installation on Windows OS
- Visit the MinGW-w64 website and download the installer for your system architecture (32-bit or 64-bit).
- Run the installer and ensure you select the Add to PATH option during installation.
- After installation, open the command prompt or PowerShell.
- Run the following command to verify the installation:
gcc --version
Note:
-
printf
is used to output (print) the result in the terminal.
Writing and Compiling a C Program
Now that you have C installed, let's write a simple Hello, World!
program and compile it. Follow the steps below:
- Create a file named
hello.c
and paste (right click) the following code into it and save (based on the editor):
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
vim hello.c
Save file:
:wq
-
Enter
orReturn
or
nano hello.c
Save file:
Ctrl + O Ctrl + C Ctrl + X
Y
-
Enter
orReturn
or
emacs -Q -nw hello.c
It is the same as emacs hello.c
except that it might open the GNU graphical interface:
emacs hello.c
Save file:
Ctrl + X Ctrl + S
Ctrl + X Ctrl + C
Open the terminal or command prompt and navigate to the directory where the
hello.c
file is located.Use the
gcc
command to compile the program and generate an executable file:
gcc hello.c -o hello
or
gcc hello.c -o hello.exe
The -o
option specifies the output file name (in this case, hello
).
- Run the program by executing the generated executable file:
./hello
or
./hello.exe
You should see the output Hello, World!
displayed in the terminal or command prompt.
Practice
Assuming you have an account or are already logged in on Replit.
- Try the Replit Editor online.
- Click the Fork button.
- Edit the current code.
Conclusion
Congratulations! You've learned how to create directories, check if C is installed, install C if needed, and write a simple C program. This knowledge forms the foundation for your C programming journey. As you progress, you can explore more complex programs and dive deeper into the language's capabilities. Have fun coding and building amazing software with C! If you have any further questions, feel free to ask.
References
- Learn C - Free Interactive C Tutorial
- Learn C Programming - Tutorialspoint
- C Programming - GeeksforGeeks
- C Programming - Codecademy
Happy Coding
Top comments (0)