First C Program

Writing the first program is always an exciting step. In C programming, the traditional starting point is the "Hello, World!" program — a simple program that displays a message on the screen. It introduces the basic structure of a C program and how all pieces fit together.

The Hello World Program


#include <stdio.h>

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

Output


Hello, World!

Line-by-Line Explanation

Line 1: #include <stdio.h>

This is a preprocessor directive. It tells the compiler to include the Standard Input/Output header file (stdio.h) before compiling the program. The stdio.h file contains the definition of the printf() and scanf() functions. Without this line, the compiler would not know what printf is.

Line 2: int main()

This is the main function. Every C program must have exactly one main() function. Execution of the program always begins from here. The keyword int before main means the function will return an integer value to the operating system when it finishes.

Line 3: { (Opening Brace)

The opening curly brace marks the beginning of the function body. Everything written between { and } belongs to the main() function.

Line 4: printf("Hello, World!\n");

printf() is a built-in function used to print output on the screen. The text inside the double quotes is called a string literal. The \n is an escape sequence that moves the cursor to a new line after printing.

Line 5: return 0;

This statement returns the value 0 to the operating system. A return value of 0 means the program ran without errors. This is the standard way to end the main() function.

Line 6: } (Closing Brace)

This marks the end of the main function.

Common Escape Sequences

Escape SequenceMeaning
\nNew line
\tHorizontal tab (spacing)
\\Backslash character
\"Double quote character
\0Null character (end of string)
\rCarriage return

Example Using Escape Sequences


#include <stdio.h>

int main()
{
    printf("Name:\tAlice\n");
    printf("City:\tDelhi\n");
    printf("He said: \"Hello!\"\n");
    return 0;
}

Output


Name:   Alice
City:   Delhi
He said: "Hello!"

Adding Comments in C

Comments are lines in the code that the compiler ignores. They are used to explain what the code does. Good comments make code easier to understand.

Single-Line Comment


// This is a single-line comment
printf("Hello!"); // This prints Hello on screen

Multi-Line Comment


/* This is a multi-line comment.
   It can span more than one line.
   Useful for longer explanations. */
printf("Welcome to C!");

C Tokens

In C programming, the smallest individual units in a program are called tokens. When a C program is compiled, the compiler breaks the source code into these small pieces to understand it. Think of tokens as the individual words and punctuation in a sentence.

There are six types of tokens in C:

  1. Keywords
  2. Identifiers
  3. Constants
  4. String Literals
  5. Operators
  6. Punctuators (Special Symbols)

Example Showing Different Tokens


int age = 25;
  • int — Keyword
  • age — Identifier
  • = — Operator
  • 25 — Constant
  • ; — Punctuator

Keywords in C

Keywords are reserved words that have a fixed meaning in C. They are predefined by the language and cannot be used as variable names or function names. All keywords in C are written in lowercase.

C89 has 32 keywords:

Column 1Column 2Column 3Column 4
autobreakcasechar
constcontinuedefaultdo
doubleelseenumextern
floatforgotoif
intlongregisterreturn
shortsignedsizeofstatic
structswitchtypedefunion
unsignedvoidvolatilewhile

Important Rules About Keywords

  • Keywords must always be written in lowercase (e.g., int not INT).
  • Keywords cannot be used as the name of a variable, function, or array.
  • Using a keyword as an identifier will cause a compilation error.

int int = 10;   // ERROR! 'int' is a keyword
int value = 10; // CORRECT

Identifiers in C

Identifiers are the names given to variables, functions, arrays, and other user-defined elements in a program. They are like labels that help identify different parts of the program.

Examples of Identifiers


int age;         // 'age' is an identifier
float salary;    // 'salary' is an identifier
void display();  // 'display' is an identifier

Rules for Naming Identifiers

  1. An identifier can contain letters (A-Z, a-z), digits (0-9), and underscore (_).
  2. An identifier must begin with a letter or an underscore. It cannot start with a digit.
  3. Identifiers are case-sensitive. So age, Age, and AGE are three different identifiers.
  4. Keywords cannot be used as identifiers.
  5. There is no restriction on the length of an identifier, but most compilers recognize up to 31 characters.
  6. No special characters (like @, #, $, %) are allowed in identifiers.

Valid and Invalid Identifiers

IdentifierValid / InvalidReason
ageValidContains only letters
total_marksValidLetters and underscore allowed
_countValidCan start with underscore
value1ValidLetters followed by digit
1valueInvalidCannot start with a digit
my-nameInvalidHyphen (-) is not allowed
intInvalid'int' is a keyword
my nameInvalidSpace is not allowed

Case Sensitivity Example


#include <stdio.h>

int main()
{
    int score = 90;
    int Score = 80;  // different from 'score'
    int SCORE = 70;  // different from both

    printf("score = %d\n", score);
    printf("Score = %d\n", Score);
    printf("SCORE = %d\n", SCORE);

    return 0;
}

Output


score = 90
Score = 80
SCORE = 70

Best Practices for Naming Identifiers

  • Use meaningful names that reflect the purpose of the variable (e.g., studentAge instead of x).
  • Use camelCase (e.g., totalMarks) or snake_case (e.g., total_marks) for readability.
  • Avoid using single-letter names except for loop counters like i, j, k.
  • Keep identifier names short but descriptive.

Summary

The Hello World program is the perfect starting point to understand how a C program is structured. Tokens are the building blocks of every C program, and among them, keywords and identifiers are the most fundamental. Keywords are reserved words with fixed meanings, while identifiers are custom names given by the programmer. Understanding these basics correctly sets a strong foundation for everything that follows in C programming.

Leave a Comment

Your email address will not be published. Required fields are marked *