Print "Hello World" in C

C Hello World Program (Example): This this tutorial, we will learn how to write the first C program to print Hello World on the screen. Also, learn how to execute the first C program? By IncludeHelp Last updated : June 03, 2023

In this program we will print Hello World, this is the first program in C programming language. We will print Hello World using user define function's too.

This is the very first program in c programming language, you have to include only single header file that is stdio.h, this header file contains the declaration printf() function.

How to Print "Hello, World" in C?

It's very easy to print "Hello, World" in C language, you need to include stdio.h header file so that you can use the printf() function that will be used to print "Hello World". Now, inside the main() function, just write printf("Hello World"); - This statement will print "Hello World" on the screen. Read the further tutorial for the explanation.

The printf() is used to display message as well as value on the standard output device (monitor), use of printf function is very easy, you have to just pass the string (message) that you want to print on the screen within inverted commas ("message").

"Hello world" Program in C

/* C program to print Hello World! */
 
#include <stdio.h>
int main()
{
    printf("Hello World!");
 
    return 0;
}

"Hello world" Program in C Using function

Here, we are creating a user define function to print the text (“Hello World”) on the screen, read more about user define functions: C Library and User Define Functions (UDF), C user define functions programs

/* C program to print Hello World! */
 
#include <stdio.h>

// function to print Hello World!
void printMessage(void)
{
    printf("Hello World!");
}
 
int main()
{
     
    //calling function
    printMessage();
 
    return 0;
}

Output

Hello World!

Explanation - How "Hello world" Program in C Works?

There is no other way to learn programming except writing programs. In every programming language we start programming by printing "Hello World" on the output device.
"Print Hello World in C" is the first program which we are writing here.

About the statements

1) #include <stdio.h>

stdio.h is a header file which contains declarations of standard input, output related functions, this statement tells to the compiler to include (add) the declarations of all standard input, output related functions which are declared inside stdio.h

2) int main()

main is a pre-declared function which is defined by the programmer i.e. the declaration of the main is predefine we can define its body.

main is the entry point from where program's execution starts. While int is the return type of the main.

3) printf("Hello World!");

"Hello World!" is the sequence of characters it is called character array, string or constant string.

printf is a library function which is declared in stdio.h; this function is responsible to print the text on the output screen.

4) return 0;

This statement is returning the control of this program to the calling function (operating system's thread which called this function) with value 0.

Return value 0 represents that the program's execution completed successfully.

How to compile and run "Hello world" program under various operating systems (or compilers)?

Write program in any text editor and save it with extension .c

Let's consider the file name is "helloworld.c"

Under TurboC complier (Windows)

Open your saved program (or write program in the editor - use F2 to save it), to compile the program press ALT+F9, compiler will show all errors and warnings, if there is any error or/and warning correct it and compile again. If program is error and warning free you can run it by pressing CTRL+F9, after executing the program, press ALT+F5 to see the output of your program.

Under GCC Compiler (Linux)

To compile the program under GCC run the following command

gcc helloworld.c -o helloworld

Make sure you are in the current directory where program is saved (Learn Linux terminal commands)

If there is no errors or warnings object (binary) file helloworld will be created in the same directory now you can run program by using following command

./helloworld

C Basic Programs »


Related Programs

Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.