C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » C solved programs » C basic programs

C program to print Hello World! / First C program in C

This is a first program in C language; this program will print the text (‘Hello World’) on the output screen. Here, we are going to learn how to write, execute and run a first program in C programming language?

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.

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/first program in C

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

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

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 this 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








COMMENTS