In this blog post, I'll guide you through writing your first "Hello World" program in C—a classic starting point for every budding programmer.
But before we start make sure to set up the Development Environment before diving into coding. Launch your IDE and create a new file with a ".c" extension, such as "hello_world.c".
Now, write the "Hello World" code down below. In your newly created file, enter the following lines:
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
Let's break down the code:
#include <stdio.h>
: This line includes the standard input/output library, which provides functions likeprintf
for displaying output on the console.int main() { ... }
: This is the main function—the entry point of a C program. All the program's execution starts from here.printf("Hello, World!\n");
: Theprintf
function is used to print the "Hello, World!" message to the console.return 0;
: Thereturn
statement indicates that the program has executed successfully and is returning a value of 0 to the operating system.
Once you've finished writing the code, save the file. The shortcut keys are "Ctrl + S" to save a file on your editor.
Now you can compile and build your program. Navigate to the build section from to top menu and select "Build Solution" or "Build Project", they will do the same in our case.
You can watch the compiler work from the output panel at the bottom. Once the build has been completed you can run the program.
To run the program you have two options:
Use the big green triangle button on the top panel,
Or go to your project folder and navigate to "Debug"->"x64"->"hello_word.exe".
The ".exe" file is your program and is the distributable version, while the button on IDE is your shortcut to run it.
Run the program and you should see "Hello, World!" on your screen.
PS: The window that pops up is called a console by the way.
Congratulations! You've successfully written and executed your first "Hello World" program in C. This simple yet powerful exercise marks the beginning of your programming journey with the C language. Now that you have a basic understanding of how to set up the environment, create a C program, and compile it, you can start exploring the language further.
The "Hello World" program serves as a stepping stone towards more complex projects and problem-solving in C. Continue learning the language's syntax, data types, control structures, and advanced concepts like pointers and memory management. With practice, patience, and a desire to explore, you'll unlock endless possibilities in the world of programming.
Happy coding!