C First Program (Hello World) 🎯

beginner
15 min

C First Program (Hello World) 🎯

Welcome to CodeYourCraft! Today, we're embarking on a thrilling journey to explore the world of C programming. By the end of this tutorial, you'll not only create your first C program but also understand the essential building blocks that power this versatile language.

Let's dive right in!

Getting Started πŸ“

C is a powerful, high-level programming language known for its efficiency and wide usage in system software, device drivers, and embedded systems.

To write and compile C programs, you'll need a text editor and a C compiler. If you're on Windows, consider using Notepad++, and on Linux or macOS, try Vim or Sublime Text. For compiling, use the GCC (GNU Compiler Collection) tool.

Your First C Program πŸš€

The traditional first program in any programming language is the "Hello, World!" program. Let's write it in C!

c
#include <stdio.h> int main() { printf("Hello, World!\n"); return 0; }

πŸ’‘ Pro Tip: Save the above code in a file named hello_world.c.

Breaking Down the Code πŸ“

  1. #include <stdio.h>: This line includes the standard input/output library, allowing us to use functions like printf().

  2. int main(): The main() function is the entry point of any C program. int indicates that the function returns an integer value.

  3. printf("Hello, World!\n");: This function prints the text "Hello, World!" to the console, followed by a newline character (\n).

  4. return 0;: After the execution of the program, the main() function returns 0, indicating that the program has finished successfully.

Compiling and Running the Program πŸ“

Now that you've written your first C program, let's compile and run it.

  1. Open your terminal (command prompt on Windows).

  2. Navigate to the directory containing your hello_world.c file using the cd command.

  3. Compile the program using the following command:

bash
gcc hello_world.c -o hello_world
  1. Run the compiled program with:
bash
./hello_world

On Windows, the command might look like this:

bash
hello_world.exe

You should see "Hello, World!" printed in your console! πŸŽ‰

Quiz πŸ’‘

Quick Quiz
Question 1 of 1

What does the `#include` directive do in C?

Next Steps πŸ“

Now that you've created your first C program, you're ready to explore more. In the next lessons, we'll dive deeper into C, learning about data types, variables, functions, and control structures.

Stay tuned, and happy coding! πŸ’»πŸ’ΌπŸš€