Write A Basic "Hello World" Program
In this step, we'll revisit the classic "Hello World" program in C. It will help you review the basic structure of a C program, including headers, the main function, and the printf()
function. This initial exercise is crucial for understanding how C programs are constructed and executed.
For newcomers to programming, creating your first program can feel both exciting and intimidating. Don't worry - every professional programmer started exactly where you are now, with a simple line of code that prints a greeting to the screen.
Open the WebIDE terminal and navigate to the project directory:
cd ~/project
Creating a new file is your first practical step in bringing your program to life. The process of creating and editing files is a fundamental skill in software development.
Create a new file called hello.c
using the touch editor:
touch hello.c
Now, type the following code into the editor:
#include <stdio.h>
int main() {
printf("Hello, World\n");
return 0;
}
Tips: Practice writing C programs to improve your coding skills. The more you write, the better you'll get.
Each line of this simple program is a building block of C programming. Let's explore what's happening behind the scenes. The code might look short, but it contains several important programming concepts that you'll use throughout your coding journey.
Let's break down the code:
#include <stdio.h>
includes the standard input/output library
int main()
is the main function where the program starts
printf()
prints text to the screen
\n
creates a new line after printing
return 0;
indicates the program completed successfully
Compiling transforms your human-readable code into instructions that a computer can understand. This process is a critical step in bringing your program to life.
Compile the program using GCC:
gcc hello.c -o hello
Example output:
(no output if compilation is successful)
Running the program is the moment of truth - where you see the result of your coding efforts. Each successful run is a small victory in your programming journey.
Run the compiled program:
./hello
Example output:
Hello, World
If you see the "Hello, World" message, congratulations! You've just written, compiled, and run your first C program. This achievement marks the beginning of your programming adventure, opening doors to more complex and exciting coding challenges.