Many can relate to the challenge of learning a new coding language. I’ve found that quickly being able to make a first program to start experimenting with the language helps inspire the confidence needed to keep going. In this blog post I will be briefly going over the steps needed to create a simple first program in the C language in Ubuntu.
Setting Up Your Development Environment On Your Computer:
- Install your favorite text editor; mine happens to be VS Code by Microsoft.
- Know how to use the cd and ls commands in the terminal to access the files you will be creating.
C is a compiled language which means that once you have written the .c file you will need to “compile” it which will create another file which the computer will use to run the program.
To install the GCC C compiler on your computer run:
$ sudo apt-get update # This is generally recommended in order to make sure that your system has up to date packages. | |
$ sudo apt-get install gcc # This installs the GCC C compiler to your system. |
To Create Your First Program:
Create a File with the .c file extension in your text editor. Below is a sample first C file.
// My "First" C Program: | |
// To Compile:gcc -o first_c_prg first_c_prg.c | |
// To Run:./firstc | |
#include <stdio.h> | |
#define EXIT_SUCCESS 1 | |
int main(void) | |
{ | |
char sentence[]= "Hello World!"; | |
printf("%s\n", sentence); | |
return EXIT_SUCCESS; | |
} |
To Compile Your First Program:
Move to the directory that contains your program and type:
gcc -o first_c_prg first_c_prg.c |
This code has two important effects, it runs the gcc compiler and creates and names an object file from your program file.
To Run Your First Program:
In the same directory, type:
./first_c_prg |
Please let me know if this helped you to get started quickly with C on Linux.