C Hello World Program

C Hello World Program

The C programming language is known for its simplicity and power, and the ‘Hello World’ program is the quintessential first step for anyone embarking on their journey into this versatile language. In this program, you’ll take your first steps into the world of C programming by learning how to display the classic ‘Hello World’ message on the screen. It’s a fundamental starting point that introduces you to the essential concepts of C, setting the stage for your exploration of this language’s incredible capabilities.

The “Hello World” program is the first thing you learn when starting to program in a new language. It’s a basic program where you make the computer show the words “Hello World” on the screen.

C Program to Print “Hello World”

// Simple C program to display "Hello World"
 
// Header file for input output functions
#include <stdio.h>
 
// main function -
// where the execution of program begins
int main()
{
 
    // prints hello world
    printf("Hello World");
 
    return 0;
}

Output

Hello World

Compiling the First C Program

Before Writing the first program, the user needs to set up a C program compiler, which works in compiling and executing the “Hello World” program. Here we have used a Windows-based GCC compiler to compile and run the program

Step 1 involves writing the “Hello World” program in a text editor and saving it with a .c file extension. Here’s the “Hello World” program written in C and saved as HelloWorld.c

Step 2 involves opening the Command Prompt (CMD) or Command Prompt window and navigating to the directory where the HelloWorld.c file is located, which is C:\Users\Chin\Sample in this case

Step 3: We have to compile the code in order to execute the following command:

gcc HelloWorld.c

Hence, this would enable us to create a C-executable file with a random name given by the compiler itself. We got the executable filename as a.

To give a user-oriented name, run the following command:

gcc -o helloworld HelloWorld.c/pre>

Therefore, this can create a C-executable file by the name HelloWorld.

Step 4: To run the executable file and get the required result, run the following command:

helloworld

Explanation of the Code

Line 1:

// Simple C program to display “Hello World”
  1. Why We Use Comments: Comments are like little notes we write inside our program to help ourselves and other programmers understand what’s going on. They don’t tell the computer what to do; they’re just there for us to read.
  2. What Comments Look Like in C:
    • Single-line comments start with // and go until the end of the line. They’re good for short comments on one line.
    • Multi-line comments are surrounded by /* and */. They let us write longer comments that can span across multiple lines.

Line 3:

#include 
  1. What Are Directives: In C, lines that start with a # sign are called directives. These lines are special instructions that are handled by a program called the preprocessor before the actual compilation by the compiler.
  2. The #include Directive: One common directive is #include. When you see #include in your code, it’s like telling the preprocessor to grab another piece of code and put it here. For example, #include <stdio.h> tells the preprocessor to include a file called stdio.h, which contains information about standard input and output functions.

Line 6:

int main()
  1. The main Function: In C, we use a line like this to declare a function called main that gives back a number (integer) when it’s done. A function is like a group of instructions that does a specific job.
  2. Starting Point: Every C program starts running from the main function, no matter where it is in the program. So, every C program must have a main function, and this is where the program begins.
  3. Curly Braces { }: These curly braces { and } mark the start and end of the main function. Everything inside these braces is the set of instructions that the main function will perform. We call this part between the braces a “block.”

Line 10:

printf("Hello World");
  1. Displaying “Hello World”: This line tells the computer to show the words “Hello World” on the screen. In C, this is called a “statement.” Every statement is like a little instruction for the computer.
  2. Semi-colon: At the end of each statement, we put a semi-colon ;. It’s like telling the computer, “Hey, this is the end of what I want you to do for this instruction.”
  3. The printf Function: We use printf to print things on the screen. When we put something inside double quotes (" "), like “Hello World,” that’s what will be shown on the screen.

Line 12:

return 0;
  1. The return Statement: This line is also a command. It’s like telling the computer, “Okay, I’m done with my job, and here’s the result.” In C, we use return in functions to give back a value and indicate that the function is finished.
  2. Indentation: You’ll notice that the printf and return lines are slightly pushed to the right (indented). We do this to make the code easier to read. In simple programs like “Hello World,” it might not seem important, but as programs get more complex, indentation helps us understand the structure and avoid mistakes

FAQ- C Hello World Program

Q1. What is Hello World program in C?

Ans. This line makes the computer display “Hello World” on the screen. It’s a statement in C, and we use a semicolon ‘;’ to signal the end of the statement.

Q2. How to create Hello C?

Ans. C Program:
#include <stdio. h>
int main() {
printf(“Hello, world!\ n”);
return 0;
}

Q3. Why is Hello World used?

Ans. A “Hello, world!” program is a common starting point for beginners learning a programming language. It’s also a quick way to test if a programming environment is set up correctly and the user knows how to use it.

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

Leave a Comment