Introduction to C Programming Language (2)

Main Function

int main()
{
    return 0;
}

Every C program, regardless of its size, begins execution at the main function, which serves as the entry point. This function is also referred to as the primary function.

The keyword int preceding main indicates that the function returns an integer upon completion. Therefore, the statement return 0; aligns with this return type.

  • The main function acts as the program's starting point.
  • Only one main function is allowed per program.
  • Even when multiple .c files exist in a project, there must be exactly one main function.

Common mistakes:

  • Typing "mian" instead of "main"
  • Omitting parentheses after main
  • Using Chinese punctuation marks
  • Forgetting semicolons at the end of statements

Printf Function

printf("Hello World");

The code above uses the printf function to output text to the screen.

Printf is a standard library function used for printing data to the standard output device (typically the screen). To print a string, enclose it in double quotes and pass it to the function.

Printf can also display various data types:

int num = 100;
printf("%d\n", num);       // Print integer
printf("%c\n", 'q');       // Print character
printf("%lf\n", 3.14);     // Print double

Format specifiers like %d, %c, and %lf act as placeholders replaced by actual values.

To use library functions, include the required header file. For instance, printf requires stdio.h:

#include <stdio.h>
int main()
{
    printf("Hello World");
    return 0;
}

Standard Library Functions

C language defines a set of commonly used functoins to avoid reimplementation and improve developer productivity. These functions are part of the standard library and implemented by compiler vendors. They are grouped into headers, so including the correct header is necessary for using these functions. A list of standard library functions can be found here: https://cplusplus.com/reference/clibrary/

Keywords

C has a set of reserved identifiers such as int, if, and return. These are called keywords or reserved words.

  • Keywords have specific meanings and are reserved for C.
  • User-defined identifiers cannot match keywords.
  • Keywords cannot be created by users.

The 32 basic keywords in C are:

auto break case char const continue default do double else enum extern float for goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while

Additional keywords introduced in C99 include: inline, restrict, _Bool, _Complex, _Imaginary. More details: https://zh.cppreference.com/w/c/keyword

Characters and ASCII Encoding

Characters such as letters and symbols (e.g., 'a', 'q', '@', '#') are represented using single quotes in C, like 'a', 'b', '@'.

All data in computers is stored in binary form. Each character is assigned a unique binary code known as an encoding. The American National Standards Institute (ANSI) established the ASCII standard to ensure consistent communication.

  • Uppercase letters A-Z range from ASCII codes 65–90
  • Lowercase letters a-z range from 65–90
  • The difference between lowercase and uppercase (e.g., 'a' vs 'A') is 32
  • Numeric characters 0–9 range from 48–57
  • Newline character \n has ASCII value 10

Characters with ASCII values from 0–31 are non-printable control characters.

Printing individual characters uses the %c format specifier:

#include <stdio.h>
int main()
{
    printf("%c\n", 'Q');
    printf("%c\n", 81); // ASCII value of 'Q'
    return 0;
}

Tags: c programming main function printf library functions Keywords

Posted on Sat, 18 Jul 2026 16:36:55 +0000 by danwguy