| by Arround The Web | No comments

Hello World in C Language

In this Linux Hint article, you will learn how to enter the world of programming by creating a program that prints the classic phrase, “Hello world”, on the command console.

To have all the means to understand and implement the code snippets that we will show you, we will first explain step-by-step on how to create a file with the “.c” extension to develop a program in the C language. In a special section, we will also show you how to compile the code with GCC and run a program from the Linux command-line.

Then, we will show you how to insert the necessary headers, create the main() function, and generate the “Hello world” program.

How to Create a File with the “.c” Extension to Write a Program in It

Every program in this language starts with a file with the “.c” extension, in which the main function of the process is written, including the necessary headers which define the functions and variables that are used.

To create such a file, we need to call the Linux file manager and press the right mouse button in the directory where we want to save it. This displays an options menu where we must left-click on the “Create new document” option and select “Empty document” from the menu that displays.

Once we create the file, we select it, press the right mouse button, and select “Properties” from the pop-up menu. Click accept in the file name where we add the “.c” extension under “Properties”. Then, close it.

In this way, we create a file that can be compiled by any compiler on any platform.

How to Compile and Run the Code

Compiling and running a program is a necessary step in understanding the examples that follows. Therefore, in this section, we will show you how to do this task.

To compile a file in the C language, we need to open the Linux command console and use the following command syntax:

~$ gcc path/filename.c -o out-name

GCC calls the compiler that compiles the specified file in the path/name.c, and -o specifies the name of the executable that results from the compilation.

If the code contains errors, the compiler returns a message for each reported error. If it succeeds, it returns nothing.

To run the output, we must use the following command syntax:

~$ ./out-name

The name of the output must always be preceded by the “./” characters.

How to Include the Headers in the “.c” File

After opening the file, include the header that defines the functions that we use. The headers in the C language have the “.h” extension and are inserted into the program with the following syntax:

#include <header name.h>

In this case we only use the “stdio.h” header. In this header, the standard input and output functions are defined as the printf() function which we will use later.

The headers are the first thing that the compiler must read. Therefore, they must appear in the code in the first place before the definitions and the main() function.

How to Create the Main() Function to Write the Program Inside It

Every C language program starts with the main() function. From there, the variables are defined, the data is processed, and the functions are called.

The main() functions can either go out and return empty, go out empty and return with data, or vice versa. In this case, it goes out empty and returns the same way.

Now, let us look at the syntax to create a main() function of this type:

Void main ()

{

}

The compiler processes everything between the two curly braces as part of the main() function.

How to Call the Printf() Function to Write in the Command Console

In this step, we call the printf() function to write the “Hello world” string to the command console.

When you call a function in the C language, its input arguments must always be enclosed in parentheses and separated by commas if there is more than one. All function calls, declarations, statements, etc. must end with the “;” character.

The simplest method of calling the printf() function is to send a simple string as the only input argument.

A string in the C language is a fragment of text between quotes that are stored in a data array of type char. These fragments are composed of alphanumeric characters, symbols, and control characters.

In the following, we see the complete program with the call method of the printf() function which writes the “Hello world” sentence to the command console:

#include <stdio.h>

void main ()

{

  printf("Hello World");

}

The following figure shows the compilation and execution of this code. As you can see, the “Hello World” phrase is printed on the same line as the prompt:

Escape characters have a control function in strings. For example, the “\n” character before the string writes it to a new line. This character after the string writes it and moves the cursor to the next line.

Let us see what happens when we insert these escape characters before and after the string, as the following code shows:

#include <stdio.h>

void main ()

{

  printf("\nHello World\n");

}

As can be seen in the following figure, the string is written to a new line in this case. Then, the cursor is moved next. This causes the command console prompt to be written in a new line in the following example:

Conclusion

In this Linux Hint article, we take the first step into the world of programming by explaining from scratch how to create a file with a “.c” extension to write a program in. We also included a section where we show you how to compile with GCC and run the written program from the Linux command console.

Then, we showed you how to include the headers, create a main() function, and briefly explain what a string is in the C language. We also showed how to use the printf() function and its method call to write the “Hello World” classic phrase to the Linux command console.

Share Button

Source: linuxhint.com

Leave a Reply