Fundamentals of C Language: From Keywords to Variables, The First Step in Programming

1. Keywords in C Language

In C language, keywords are part of the programming language that have special meanings, used to inform the compiler about the structure and control flow of the program. Keywords are predefined words that cannot be used to name variables, functions, or any other identifiers.

Common keywords include:

<span>int:</span> Defines a variable of integer type

<span>float:</span> Defines a variable of floating-point type

<span>if:</span> Conditional statement

<span>for:</span> Loop statement

<span>return:</span> Return value of a function

🔑 Characteristics of Keywords:

Keywords have fixed meanings and cannot be arbitrarily modified.

In C language, there are a total of 44 keywords (32 traditional C keywords + 5 new keywords from C99 + 7 new keywords from C11), which are an important part of programming.

You cannot use keywords as identifiers, such as variable names, function names, etc.

Common examples of C language keywords (brief):

int main() {
    int number = 5;
    if (number > 0) {
        return 1;
    }
}

2. Identifiers in C Language

Identifiers are names used to identify variables, functions, arrays, structures, etc. In simple terms, an identifier is the name you use when naming these program elements.

Rules for Identifiers:

Identifiers can only contain letters, numbers, and underscores, but cannot start with a number.

Keywords cannot be used as identifiers.

There is no strict limit on the length of identifiers, but for code readability, it is recommended to keep them reasonably short.

Common examples of identifiers:

  • <span>number</span>
  • <span>sum_of_numbers</span>
  • <span>calculateAverage</span>

🔑 Naming Suggestions for Identifiers:

Try to use meaningful names, such as <span>age</span>, <span>total_price</span>.

Identifiers should have a certain level of readability, avoiding meaningless names like <span>a</span>, <span>b</span>, etc.

3. Definition and Initialization of Variables

In C language, a variable is a memory location used to store data. Each variable needs to define its type, and C language requires that each variable must be declared before use.

1. Definition of Variables

When defining a variable, we need to declare the type and name of the variable. For example:

int age; // Defines an integer variable age
float price; // Defines a floating-point variable price

2. Initialization of Variables

When defining a variable, we can also initialize it with a value. For example:

int age = 18; // Defines and initializes age to 18
float price = 29.99; // Defines and initializes price to 29.99

🔑 Note:

The variable type determines the type of data stored in the variable (e.g., integer, floating-point, etc.).

Initialization refers to assigning an initial value to a variable at the time of declaration; otherwise, it will contain a random garbage value.

4. Summary: Keywords, Identifiers, and Variables

  • Keywords: In C language, keywords have special meanings and cannot be used for naming identifiers.
  • Identifiers: Used to name variables, functions, etc. Naming must follow certain rules and have readability.
  • Variables: Used to store data, must be defined first, then initialized.

By mastering keywords, identifiers, and the definition of variables, you have taken the first step in C language programming. Next, we will continue to delve into other foundational content to help you gradually master programming skills.

Leave a Comment