1.3) First C program
Here’s a simple “Hello, World!” program in C, which is often the first program people write when learning a new programming language:
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
Let’s break down what each part of the code does:
#include <stdio.h>
: This line includes the standard input/output library, which provides functions likeprintf
for displaying output on the screen.int main()
: This is the main function of the program, where execution begins. It returns an integer value to indicate the program’s exit status.{
and}
: These curly braces define the scope of themain
function, enclosing the code that belongs to it.printf("Hello, World!\n");
: This line uses theprintf
function to print the text “Hello, World!” to the console. The\n
is an escape sequence that represents a newline character, which moves the cursor to the next line.return 0;
: This line indicates that themain
function is returning an integer value of0
, which conventionally indicates a successful execution of the program.
To run this program:
- Open a text editor (like Notepad, Visual Studio Code, or any other code editor).
- Copy and paste the code into the text editor.
- Save the file with a
.c
extension (e.g.,hello.c
). - Open a terminal or command prompt.
- Navigate to the directory where you saved the file.
- Compile the program using a C compiler. For example, if you’re using the GNU Compiler Collection (GCC), you can type
gcc -o hello hello.c
and press Enter. - Run the compiled program by typing
./hello
and pressing Enter.
You should see the output:
Hello, World!
You’ve just written and executed your first C program. This simple example demonstrates the basic structure of a C program and how to use the printf
function to display output. From here, you can start exploring more C programming concepts and building more complex applications.