C - Hello World
C - Hello World
C - Hello World
Every learner aspiring to become a professional software developer starts with
writing a Hello World program in the programming language he/she is learning. In
this chapter, we shall learn how to write a Hello World program in C language.
Example
The first step is to write the source code for the Hello World program. Open a text
editor on your computer. On Windows, open Notepad or Notepad++, enter the
following code and save it as "hello.c".
#include <stdio.h>
int main(){
/* my first program in C */
printf("Hello World! \n");
return 0;
}
Output
Hello World!
https://www.tutorialspoint.com/cprogramming/c_hello_world.htm 1/5
6/16/24, 10:56 AM C - Hello World
Step 1
The first statement in the above code is the #include statement that imports the
stdio.h file in the current C program. This is called a preprocessor directive. This
header file contains the definitions of several library functions used for stand IO
operations. Since we shall be calling the printf() function which is defined in the
stdio.h library, we need to include it in the first step.
Step 2
Every C program must contain a main() function. The main() function in this
program prints the "Hello World" message on the console terminal.
The next statement calls the printf() function. In C, every statement must terminate
with a semicolon symbol (;), failing which the compiler reports an error.
The printf() function, imported from the stdio.h library file, echoes the Hello World
string to the standard output stream. In case of Windows, the standard output
stream is the Command prompt terminal and in case of Linux it is the Linux terminal.
In C, every function needs to have a return value. If the function doesn’t return
anything, its value is void. In the example above, the main() function has int as its
return value. Since the main() function doesn’t need to return anything, it is defined
to return an integer "0". The "return 0" statement also indicates that the program
has been successfully compiled and run.
Step 3
Next, we need to compile and build the executable from the source code ("hello.c").
If you are using Windows, open the command prompt in the folder in which "hello.c"
has been saved. The following command compiles the source code −
The -c option specifies the source code file to be compiled. This will result in an
object file with the name hello.o if the C program doesn’t have any errors. If it
contains errors, they will be displayed. For example, if we forget to put the
semicolon at the end of the printf() statement, the compilation result will show the
following error −
https://www.tutorialspoint.com/cprogramming/c_hello_world.htm 2/5
6/16/24, 10:56 AM C - Hello World
To build an executable from the compiled object file, use the following command −
The hello.exe is now ready to be run from the command prompt that displays the
Hello World message in the terminal.
C:\Users\user>hello
Hello World!
On Ubuntu Linux, the object file is first given executable permission before running it
by prefixing "./" to it.
You can also use an IDE such as CodeBlocks to enter the code, edit, debug and run
the Hello World program more conveniently.
Example
#include <stdio.h>
int main(){
/* my first program in C */
https://www.tutorialspoint.com/cprogramming/c_hello_world.htm 3/5
6/16/24, 10:56 AM C - Hello World
return 0;
}
Output
Hello World!
Choose Build and Run option from the Build menu as shown below −
You can also use the F9 shortcut for the same. If the program is error-free, the Build
Log tab shows the following messages −
https://www.tutorialspoint.com/cprogramming/c_hello_world.htm 4/5
6/16/24, 10:56 AM C - Hello World
Hello World!
If the code contains errors, the build log tab echoes them. For instance, if we miss
the trailing semicolon in the printf() statement, the log will be as below −
You can use any other IDE to run the C program. You will need to follow the
documentation of the respective IDE for the purpose.
Running the Hello World successfully also confirms that the C programming
environment is working properly on your computer.
https://www.tutorialspoint.com/cprogramming/c_hello_world.htm 5/5