Hello World program step by step execution

Hello World program step by step execution
//Header file
#include <stdio.h>

//main function
int main() 
{
    // This is a simple "Hello, World!" program
    printf("Hello, World!\n");

    return 0;
}

Now, let’s break down the program and go through its execution step by step:

See also – The history of C programming

Hello World program step by step execution
  1. Preprocessor Directives:
   #include <stdio.h>
  • The #include directive is used to include the contents of the standard input/output library (stdio.h). This library provides functions like printf and scanf for input and output operations.
  1. Function Declaration:
   int main() {
  • The program starts executing from the main function. It is the entry point of every C program.
  • int is the return type of the main function, and main takes no parameters in this case.
  1. Opening Brace { and Comments:
   // This is a simple "Hello, World!" program
  • Comments are for human-readable explanations and do not affect the program’s execution. They start with // for single-line comments.
  1. Print Statement:
   printf("Hello, World!\n");
  • The printf function is used to print text to the console. In this case, it prints “Hello, World!” followed by a newline (\n) character to move the cursor to the next line.
  1. Closing Brace } and return statement:
   return 0;
   }
  • The return 0; statement indicates that the program executed without any errors. The value 0 is returned to the operating system, indicating successful execution.

Now, let’s summarize the execution steps:

  1. The preprocessor includes the contents of the stdio.h library.
  2. The program starts executing from the main function.
  3. The printf function prints “Hello, World!\n” to the console.
  4. The return 0; statement indicates successful execution, and the program exits.

When you compile and run this program, you should see the output:

Hello, World!

Keep in mind that the exact steps for compiling and running the program may vary depending on the development environment or compiler you are using.

Compilation and Execution Steps:

  1. Write the Code:
    Create a file, for example, hello.c, and paste the “Hello, World!” program into it.
   #include <stdio.h>

   int main() {
       printf("Hello, World!\n");
       return 0;
   }
  1. Open Terminal:
    Open a terminal on your system.
  2. Navigate to the File Directory:
    Use the cd command to navigate to the directory where your hello.c file is located.
   cd path/to/your/directory
  1. Compile the Program:
    Use the gcc (GNU Compiler Collection) command to compile the C program.
   gcc hello.c -o hello
  • The -o flag is used to specify the output file name. In this case, it’s named hello.
  1. Run the Executable:
    Execute the compiled program.
   ./hello

You should see the output:

   Hello, World!

Explanation:

  • Compilation (gcc hello.c -o hello):
  • The C compiler (gcc) translates the C source code (hello.c) into machine code and creates an executable file (hello).
  • Execution (./hello):
  • The ./hello command runs the compiled program (hello). The ./ is used to specify the current directory.
  • Output:
  • The program’s output, “Hello, World!”, is displayed on the terminal.

Tips:

  • Ensure that you have GCC installed on your system. You can install it using your system’s package manager. For example, on Debian-based systems:
  sudo apt-get install gcc
  • The compilation and execution steps may vary slightly on different operating systems or development environments.
  • Understanding these steps will help you with more complex C programs as you continue your programming journey.
Mahesh Wabale

Leave a Comment