The C Programming Language
C has the power of assembly language and the convenience of … assembly language. -- Dennis Ritchie
Mac already has gcc (the compiler we need for C) and debugging tools installed for C so we just need to setup the IDE.
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!Paste the following snippet and save the file:
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}Now, you need to compile your code. Let's see how we can do it from Terminal. Open Terminal in the same directory as the
main.c
file and run the following command:bash gcc -o main_executable main.c
This command will invokegcc
- compiler of the C code, compile the file calledmain.c
and output a binary file namedmain_executable
.To run your program, simply type:
./main_executable
which should produce
> Hello, World!
Well done, Terminal is for Turbo cool people! 🎉 🥳
Installation
Open the Jetbrains Toolbox and install CLion.
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;
}
Additional Notes
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 withnew_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)If you ever need to pipe something from C to another program (such as a turtle program) then you can compile your application with CLion (hammer icon) and then run your program in terminal with a pipe operator.
> .\your_program | turtle
If you ever need to access
gcc
directly then you can access it via your terminal.