Hello World Program in C

  • A simple C program to print Hello World string on screen. It is often the first C program of new programmers used to illustrate the basic syntax of a C programming language.

We are going to learn a simple “Hello World” C program which will give us a brief overview of how to write and compile a c program. Before that, we have to understand the structure and fundamental essential components of a bare minimum C program.

Components of a C Program

  • Preprocessor Commands : The preprocessor step comes before compilation of c program and it instruct the compiler to do required pre-processing before actual compilation.

  • Main Function : This is the function from where execution of any C program starts.

  • Comments : You can add comments and documentation in your source code, which will not be executed as the part of program. It helps others to better understand the logic of your code.

  • Variables : A variable in C is a named memory location where a program can store and modify the data.

  • Statements & Expressions : Statements and Expressions are used to implement the core logic of the C program(For Example: Sum = A+B;).

Setting Up Your C Development Environment

Before we dive into writing our first C program, let's make sure you have the necessary tools installed on your system.
  • Installing a C Compiler : To write and run C programs, you need a C compiler. One of the most widely used C compilers is GCC (GNU Compiler Collection). Depending on your operating system, you can install GCC as follows:
    • Linux
      sudo apt-get update
      sudo apt-get install build-essential
      
    • macOS
       xcode-select --install
       
    • Windows : Download and install MinGW (Minimalist GNU for Windows) from https://mingw-w64.org/doku.php.

  • Integrated Development Environment (IDE) : While not strictly necessary, using an Integrated Development Environment can enhance your coding experience. Some popular C IDEs include Code::Blocks, Eclipse, and Visual Studio Code. Choose one that suits your preferences and install it.

C Program to Print "Hello World"

Printing "Hello World" program is one of the simplest programs in C programming languages. It became the traditional first program that many people write while learning a new programming language.

#include <stdio.h>

int main(){
    printf("Hello World");
    return 0;
}
Output
Hello World

How to Write and Compile a C Program

  1. Write
    • Open a text editor and type the above mentioned "Hello World" code.
    • Save this file as HelloWorld.c.

  2. Compile
    • Open command prompt and go to your current working directory where you saved your HelloWorld.c file.
    • Compile your code by typing gcc HelloWorld.c in command prompt. Your program will compile successfully, If your program doesn't contain any syntax error.
    • It will generate an a.out file.

  3. Execute
    • Now run your program by typing a.out in command prompt.

  4. Output
    • You will see "Hello World" printed on your console.

Description of Hello World program
  • First line is program is a comment placed between /* and */. Comments are used in a C program to write an explanation or for documentation purpose. When a program becomes very complex, we need to write comments to mark different parts in the program. These comments will be ignored by the compiler at compilation time.

  • The next line #include is a preprocessor directive to load the stdio.h(Standard Input/Output header file) library. This library provides some macros and functions for input or output which we can use in our program(like printf and scanf). We need stdio for printing "Hello World" using printf function.

  • The next line is int main(){.The int is what is called the return value. Every C program must have a main() function, and every C program can only have one main() function where program execution begins. The two curly brackets {} are used to group all statements together.

  • The next line is printf("Hello World") which is used for printing "Hello World" string on the screen.

  • The next line is return 0. int main() declares that main must return an integer. Returning 0 means informing operating system that program successfully completed, whereas returning 1 means error while running program.


Points to Remember
  • A comment starts with /*, and ends with */.

  • Some header files must be included at the beginning of your C program.

  • Every C program should have only one main() function where program execution starts.

  • All variables must be declared with respective data types before their first use.

  • Every statement must end with a semicolon.

  • Main returns an integer to Operating system informing about it's termination state. Whether program executed successfully or an error has occurred.

Conclusion

You have been taken through the process of writing and comprehending a C "Hello, World!" program by this tutorial. You now know the fundamental elements of a C program, such as the printf function, the main function, and the #include directive. You've also assembled and run your program, which gives you firsthand knowledge of the C development process.

Learn increasingly sophisticated ideas like functions, data structures, and control structures (if statements, loops) as you advance in your C programming career. Building on this base will enable you to take on a variety of programming tasks and facilitate your development as a C programmer.