C First Program

Writing your first C program is an exciting milestone for every beginner. The classic Hello World program is the traditional starting point, as it demonstrates the basic structure and syntax of a C program. Let’s see how it works and what each part means!


Example: Hello World in C

#include <stdio.h>

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

Output:

Hello World!

 


Breakdown of the C Code

  • #include
    Preprocessor Directive: Tells the compiler to include the Standard Input Output library. This header file is needed for functions like printf().
  • int main() { ... }
    Main Function: Every C program must have a main() function. Program execution always starts here. The int means this function returns an integer value to the operating system.
  • printf("Hello World!");
    Output Statement: The printf() function prints text to the screen. The text to display goes inside double quotes.
  • return 0;
    Return Statement: Ends the main function and returns 0 to the operating system, indicating the program ran successfully.

When you run this program, it displays Hello World! on your screen. This simple example introduces you to the structure and flow of a C program.


What Happens When You Run a C Program?

  1. Write the code in a text editor and save it with a .c extension (e.g., hello.c).
  2. Compile the code using a C compiler (like GCC). This converts your code into machine language.
  3. Run the program to see the output on your terminal or console.

Basic C Program Tips

  • Every C program must have exactly one main() function.
  • Statements in C end with a ; (semicolon).
  • All variables must be declared with a data type before use.
  • C is case-sensitive: Main and main are different.
  • Use // for single-line comments and /* ... */ for multi-line comments.
  • Braces { } are used to group statements together.
  • C is a free-form language: you can use spaces and newlines to format your code for readability.
  • Use meaningful variable names and consistent indentation for better code clarity.

Did You Know?

  • The Hello World program originated from the book “The C Programming Language” by Kernighan and Ritchie.
  • Most C compilers will automatically return 0 from main() if you omit the return 0; line, but it’s good practice to include it.
  • Learning C helps you understand how computers work at a low level, which is valuable for all programmers.

Leave a comment