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!
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.
The traditional first program in any programming language is the "Hello, World!" program. Let's write it in 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.
#include <stdio.h>: This line includes the standard input/output library, allowing us to use functions like printf().
int main(): The main() function is the entry point of any C program. int indicates that the function returns an integer value.
printf("Hello, World!\n");: This function prints the text "Hello, World!" to the console, followed by a newline character (\n).
return 0;: After the execution of the program, the main() function returns 0, indicating that the program has finished successfully.
Now that you've written your first C program, let's compile and run it.
Open your terminal (command prompt on Windows).
Navigate to the directory containing your hello_world.c file using the cd command.
Compile the program using the following command:
gcc hello_world.c -o hello_world./hello_worldOn Windows, the command might look like this:
hello_world.exeYou should see "Hello, World!" printed in your console! π
What does the `#include` directive do in C?
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! π»πΌπ