In this section, we will write our first C program: the classic "Hello, World!" This simple program will help you understand the basic structure of a C program and how to compile and run it.

Key Concepts

  1. Basic Structure of a C Program
  2. Including Header Files
  3. The main Function
  4. Printing to the Console
  5. Compiling and Running the Program

Basic Structure of a C Program

A C program typically consists of the following parts:

  • Preprocessor Directives: Instructions to the compiler to preprocess the information before actual compilation starts.
  • Functions: Blocks of code that perform specific tasks.
  • Statements and Expressions: Instructions that perform actions.

Step-by-Step Guide

  1. Including Header Files

Header files contain definitions of functions and macros. The #include directive is used to include these files. For our "Hello, World!" program, we need the stdio.h header file, which contains the definition of the printf function.

#include <stdio.h>

  1. The main Function

Every C program must have a main function. This is the entry point of the program where execution starts.

int main() {
    // Code goes here
    return 0;
}

  1. Printing to the Console

To print text to the console, we use the printf function. This function is defined in the stdio.h header file.

printf("Hello, World!\n");

  1. Complete "Hello, World!" Program

Here is the complete code for the "Hello, World!" program:

#include <stdio.h>

int main() {
    printf("Hello, World!\n");
    return 0;
}

  1. Compiling and Running the Program

To compile and run the program, follow these steps:

  1. Save the Program: Save the code in a file with a .c extension, for example, hello.c.

  2. Open Terminal/Command Prompt: Navigate to the directory where you saved the file.

  3. Compile the Program: Use a C compiler like gcc to compile the program.

    gcc hello.c -o hello
    

    This command compiles hello.c and creates an executable named hello.

  4. Run the Program: Execute the compiled program.

    ./hello
    

You should see the output:

Hello, World!

Practical Exercise

Exercise 1: Modify the Program

  1. Modify the "Hello, World!" program to print your name instead of "World".
  2. Save the modified program as hello_name.c.
  3. Compile and run the program.

Solution

#include <stdio.h>

int main() {
    printf("Hello, [Your Name]!\n");
    return 0;
}

Replace [Your Name] with your actual name. Follow the same steps to compile and run the program.

Common Mistakes and Tips

  • Missing Semicolon: Every statement in C must end with a semicolon (;). Forgetting this will result in a compilation error.
  • Case Sensitivity: C is case-sensitive. Ensure you use the correct case for function names and keywords.
  • Correct Header File: Make sure to include the correct header file (stdio.h for printf).

Conclusion

In this section, you learned how to write, compile, and run a simple C program. You also learned about the basic structure of a C program, including header files, the main function, and printing to the console. This foundational knowledge will be crucial as you progress through more complex topics in C programming.

© Copyright 2024. All rights reserved