Installing gcc and Running Your First C Program
Part 1 of Complete C Course
Foreword: How to download and install gcc for C Programming
By: Chrysanthus Date Published: 31 May 2024
Introduction
Requirements for Installation
- The Operating System, Ubuntu 20 (or any version from 18 and above).
- A text Editor
- The computer, hooked up to the Internet.
It is assumed that the user already knows how to use the computer or a smartphone.
Installation Procedure
When Ubuntu has booted and settles at the desktop,
- click the "Show Applications" button at the left-bottom corner of the screen. The applications icons will appear, and cover most of the screen. At the left is the Favorites Bar (vertical taskbar on the left with a few application icons). This is the activities menu.
- Look for the Terminal icon on the large applications panel on the right, and click it (the radio buttons on the extreme right, may have to be used here, to go to the page that has the Terminal icon). The Terminal window will appear. The Terminal window is like a black-and-white window. The command prompt at the top-left in the Terminal window, should be something like:
user@hostname:~$
The cursor should be flashing just on the right of the $ sign. GCC consists of more than one package, some of which may already be in the computer, but old. There has to be an update for all the packages first.
- So type:
sudo apt update
and press the Enter Key of the Keyboard. The details of this command will be explained later. The response may first be a request for the user\92s password.
- In that case, type your Ubuntu password and press the Enter Key. In typing the password, the actual characters may appear as asterisks, or nothing appears at all (that is still alright).
If the password is typed correctly and the Enter Key pressed, some lines would be displayed until you will see the command prompt again.
There also has to be some upgrading next. Update and upgrade do not mean the same thing. Whatever,
- Type:
sudo apt upgrade
After some lines displayed, you may be prompted with something like:
After this operation, 459 MB of additional disk space will be used.
Do you want to continue? [Y/n]
- Just press the Y key on the keyboard (would appear in lowercase), and Press Enter.
After many lines, you will see the command prompt again. It is now time to install the gcc.
- Type the following and press Enter:
sudo apt install gcc
After some lines, you should see the command prompt again. Has GCC really been installed? - An easy way to know this, is to find the version of GCC installed in your computer.
- Type the following and press Enter:
gcc - version
You should see a feedback like:
gcc (Ubuntu 9.4.0\961ubuntu1~20.04.1) 9.4.0
Copyright \A9 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
If gcc has not been installed, a clear message indicating so, would be displayed. This feedback indicates that GCC has been installed.
Your First C Program
#include <stdio.h>
int main()
{
printf ("Hello World!\n");
return 0;
}
The first line includes a C library, for input from the keyboard, and output to the Terminal window. Then there is the main function that must be in a single file C program. Here, it begins with "int main()" , and that is followed by a block, delimited by { and }. The first line in the block will print "Hello World!" without the quotes, to the terminal. '\n', just before the closing double quotes, will send the cursor at the terminal, to the next line below, for the next printing. The second line "return 0;", without the quotes, ending with a semicolon, is always the last line of the main() function. Note that the first line also ends with a semicolon.
Save this file in the Home directory (the Home directory has the user's name), with the name, "temp.c" without the quotes. Most of the single programs in this blog, will be in this file, replacing the previous content.
To execute (run) a program in C, the program has to be compiled first. To compile the program, type the following command at the terminal window and press Enter:
gcc -o temp temp.c
gcc is the compiler name. "-o" without the quotes is a switch. Its explanation will be given later. "temp" without the quotes, is the executable (or compiled) file. A name different from "temp", would have been given. temp is known as the binary file. "temp.c" without the quotes, is the source (text) file. Both files should now be in the Home directory. temp was created by the gcc compiler.
In order to execute (run) the compiled file, type the following and press Enter:
./temp
"./" refers to the current directory, which happens to be the Home directory here. temp is the compiled file. The source file, temp.c which has the typed code, cannot be executed. The response at the terminal, should display (print out):
Hello World!
Conclusion
A similar installation and the running of your first program, can be done with other versions of Ubuntu.