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 like printf 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 the main function, enclosing the code that belongs to it.
  • printf("Hello, World!\n");: This line uses the printf 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 the main function is returning an integer value of 0, which conventionally indicates a successful execution of the program.

To run this program:

  1. Open a text editor (like Notepad, Visual Studio Code, or any other code editor).
  2. Copy and paste the code into the text editor.
  3. Save the file with a .c extension (e.g., hello.c).
  4. Open a terminal or command prompt.
  5. Navigate to the directory where you saved the file.
  6. 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.
  7. 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.