The C Programming Language
C has the power of assembly language and the convenience of … assembly language. -- Dennis Ritchie
C on Windows is not the best experience but virtualizing UNIX is not the solution.
Install Cygwin
Cygwin is an extensive collection of GNU and open-source tools that give access to Linux-like functionality on Windows. One of these tools, gcc
, is the de facto compiler for C, which you will need during your degree.
You can either install Cygwin here or use winget:
winget install -e --id Cygwin.Cygwin
When installing Cygwin you will need to select
gcc-g++
, cmake
, make
, and gdb
from the packages list.
Let's test your installation!
Type gcc
in your shell. You should receive an error: no input files
message.
If not, then follow the below instructions:
- Go to the
cygwin
folder in your Files application. - Copy the complete path to the
cygwin/bin
directory. - Right-click "This PC" on the left sidebar.
- Select Properties > Advanced > Environment Variables.
- Navigate towards the PATH variable in System variables, click "Edit".
- Click "New", and paste the copied
cygwin/bin
directory path.
Restart your shell or restart your computer for the changes to take effect.
Configure your IDE
While you can probably survive your C class without touching Terminal, we recommend using it! It not only gives you a better understanding of how a computer program is created, but also prepares you for your further modules, which almost certainly will require familiarity with the shell.
- VS Code
- JetBrains CLion
Let's use VS Code to create a simple C program!
- Open VS Code and create an empty file called
main.c
. This will be our C code source file. Such files have the.c
extension! - Enable language support for C by installing the C/C++ Microsoft extension.
Paste the following snippet and save the file:
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}Now, you need to compile1 your code. Let's see how we can do it from PowerShell (or VS Code's built-in shell). Open PowerShell in the same directory as the
main.c
file and run the following command:
gcc -o main_executable main.c
This command will invoke gcc
- compiler of the C code, compile the file called main.c
and output a binary file named main_executable
. 5. To run your program, simply type:
./main_executable
which should produce
> Hello, World!
Well done! 🎉 🥳
Nevermind, let's use CLion instead!
Open the Jetbrains Toolbox and install CLion
Open CLion, click configure and then open
Toolchains
underBuild, Execution, Deployment
. Click the small plus button and select Cygwin, if your environment is not found automatically select the dropdown and find the root of your cygwin install. Ensure that CMake, Make, C Compiler, and Debugger are all found (either detected or a small tick mark).
- Click OK, and then press
New Project
, select C executable from the new project screen with any language standard. A simple Hello World program should open and if you click the play symbol at the top right it will compile and execute in a terminal at the bottom of the screen.
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
CLion uses CMake to configure running your project. While others are worrying about linking new files to their project, you can simply add it to the add_executable
section (as shown with new_file.c
below).
cmake_minimum_required(VERSION 3.16)
project(untitled C)
set(CMAKE_C_STANDARD 99)
add_executable(untitled main.c new_file.c)
Additional Notes
- If you ever need to pipe something from C to another program (such as a turtle program) then you can compile your application and then run your executable in PowerShell Core with a pipe operator.
.\your_program.exe | turtle.exe
If you ever need to access
gcc
directly then you can open the installedCygwin Terminal
.If compiling with Cygwin via the terminal results in an access denied message you can try disabling your real-time anti-virus scanning.
Certain courseworks in C require that the code be compilable on UNIX systems (MacOS, Linux etc.), which Windows is not. As such, it is strongly recommended you test your code on a UNIX machine to ensure that your code functions on both machines. To access a Linux instance, you can either use virtualization, multi-booting2, or use the UCL CS lab machines (recommended).