Problem
Printing text to screen using Hello World
tl;dr
nano hello_world.c
#include //Library for standard input output - E.G printf function
main()
{
printf("Hello World!\n"); // The \n is for new line.
}
gcc -o hello_world hello_world.c
./hello_world
Solution
Hello World is a classic example of the most basic functions of C.
The following information will show you how to use the printf function to print text to a screen.
This function can be used to print instructions or other information in your program.
First we want to make sure our compiler is installed.
You can confirm it's installed, or install it even by running this command:
yum install gcc -y
If it is already installed, you will see this:
Package gcc-4.4.7-11.el6.x86_64 already installed and latest version Nothing to do
Or it will install it.
Step 2: Create your C filenano hello_world.c
Enter the following to create a simple hello world C file, which prints "Hello world!"
#include //Library for standard input output - E.G printf function
main()
{
printf("Hello World!\n"); // The \n is for new line.
}
Exit nano by pushing Ctrl+X and then S to save changes.
Step 3: Compile your C file.gcc -o hello_world hello_world.c
This will get gcc to compile the file to hello_world from hello_world.c.
It will return nothing if there are no errors.
Next - run your file!
./hello_world Hello World! [root@nowhere /]#
You should be able to compile any valid C file now.
- Updated