C Language - The Grammar of a Programming Language

1. Language Grammar

  • Human language has a wide variety of grammar, and learning it requires a great deal of time.
    • You need to learn many grammatical elements such as nouns, verbs, tense, and word order.
  • A programming language is a language with the clear purpose of instructing a computer what to do and how to do it.
    • Even with a relatively small number of grammatical elements, it is possible to express meaning and execute actions.
  • Unlike human language, a programming language can be used immediately once you understand a few core grammar rules.

2. C Language Grammar

A programming language consists of the following five elements.

  1. Variables - store values.
  2. Operations - calculate values.
  3. Statements - execute content.
  4. Control - change the order of execution.
  5. Functions - group multiple statements into one.

Understanding just these five grammar elements is enough to understand the basic structure of a program.

2.1. Variables

  • A variable is a memory space that stores a value and has been given a name.
    • A program uses variables to remember, reference, and modify data.

2.1.1. Variable Type

  • A variable type is a rule that determines what kind of value a variable stores and how that value should be interpreted.
    • The variable type determines the form of values that can be stored, the way memory is used, and the kinds of operations that are allowed.

int count;
count = count + 1;      // int type stores integer values.
 
char grade = 'A';       // char type stores a single character or small integer values.
 
float ratio = 0.75f;
ratio = ratio * 100.0f; // float type stores floating-point (real) values.

2.1.2. Constant

  • A constant is a variable whose value does not change.
  • In C, the way to grammatically guarantee that a variable cannot be changed is const.
    • A variable declared with const is not allowed to change its value after initialization.
    • The compiler detects any attempt to change the value of that variable as a syntax error.

const int limit = 10;
limit = 20; // error: assignment of read-only variable

2.1.3. Data Structure

  • A data structure is a way to group and store multiple values as one.

int arr[10];   // array
 
struct Point { // structure
    int x;
    int y;
};

A data structure is a structural organization of variables designed to hold multiple values rather than a single value.

2.1.4. Macro Constant

  • A macro constant is a rule that replaces a name with a value.
    • Before compilation begins, the defined name is simply replaced with the specified value in the source code.

The following definition replaces every occurrence of MAX_COUNT in the source code with 5 before compilation.

#define MAX_COUNT   5

The characteristics of macro constants are as follows.

  • They do not have the concept of a type.
  • They use a simple text substitution method.
  • They are applied as-is throughout the entire code, regardless of where they were declared.
  • They operate during the preprocessing stage before compilation.

2.2. Operations

  • An operation is the process of calculating values to create a new value.
    • It produces a single result by adding numbers or comparing them.

2.2.1. Arithmetic Operations

count + 1
count - 1
count * 2
count / 2

Add, subtract, multiply, and divide values.

2.2.2. Comparison Operations

count < MAX_COUNT
count == LIMIT
count != 0

Compare two values to produce true or false.

2.2.3. Logical Operations

count > 0 && count < MAX_COUNT
count == LIMIT || count == 0

Combine multiple conditions into a single result.

2.3. Statements

  • A statement is the smallest unit that is actually executed.
    • A program is a collection of statements.
  • In C, a statement must always end with a semicolon (;).
    • A semicolon is a grammatical element that tells the compiler that one unit of execution has ended.

2.3.1. Comments

  • A comment does not affect the execution of a program and is a grammatical element used to leave an explanation about the code.
    • During compilation, all comments are removed.

Single-line Comment

Any content after // is treated as a comment until the end of that line.

int count = 0; // Initialize counter

Multi-line Comment

Everything between /* and */ is treated as a multi-line comment.

/*
 This function increases the input value by one.
 The input value is not modified directly.
 The result is returned to the caller.
*/
int increase(int value) {
    return value + 1;
}

2.3.2. Assignment Statement

  • An assignment statement is a statement that stores a calculated value into a variable.

count = count + 1;

It is an execution command telling the program to apply the calculated result to the variable.

2.3.3. Function Call Statement

  • It is a statement that tells a function to execute.

increase(count);

2.3.4. Input/Output Statement

  • Input and output are executed in the form of function calls.

printf("%d\n", count);
scanf("%d", &count);

Input and output are not operations; they are statements executed by calling standard functions.

2.4. Control

  • Control determines which statements to execute and when.
    • Control is divided into conditions and repetition.

2.4.1. Condition

  • A condition executes a statement only in a specific situation.

if (count == LIMIT) {
    break;
}

It creates a branch in the flow of execution.

2.4.2. Repetition

  • Repetition executes the same action multiple times.

2.4.2.1. while

The while loop checks the condition first, then executes the statement only when the condition is true.

while (count < MAX_COUNT) {
    count = count + 1;
}

2.4.2.2. do-while

The do-while loop executes the statement first, then checks the condition based on the result.

do {
    count = count + 1;
} while (count < MAX_COUNT);

2.4.2.3. for

The for loop expresses the repetition condition and control elements together in one place.

for (int i = 0; i < MAX_COUNT; i++) {
    count = count + 1;
}


2.5. Functions

  • A function is something that groups multiple statements into a single feature.
    • It makes it possible to reuse the same action.

2.5.1. Function Block

  • The body of a function consists of a block enclosed in { }.

int increase(int value) {
    return value + 1;
}

A block is a grammatical structure that groups multiple statements into a single unit of execution.

2.5.2. Function Argument

  • A function argument is a value passed into a function from the outside.

int printValue(const int value) {
    printf("%d\n", value);
    return value;
}

A const function argument expresses in grammar the promise that the value will only be used inside the function and not changed.

2.5.3. Function Return Value

  • A function return value is the value that, after the function finishes executing, returns the calculated result to the place that called it.

int increase(int value) {
    return value + 1;
}
int count = increase(10);

It receives the function argument 10, returns the result of adding 1, and the return value 11 is stored in the variable count.

2.6. Example

The following example includes all five grammatical elements: variables, operations, statements, control, and functions.

#include <stdio.h>
 
#define MAX_COUNT     5
 
int increase(int value) {
    return value + 1;
}
 
int main() {
    const int LIMIT = 3;
    int count = 0;
    while (count < MAX_COUNT) {
        if (count == LIMIT) {
            break;
        }
        count = increase(count);
    }
    printf("%d\n", count);
    return 0;
}

Store values → calculate → execute → change the flow → group into a function.


3. Composition of the C Language

3.1. Keywords

In C90 (ANSI C, ISO C90), the C language has 32 keywords.

autobreakcasecharconst
continuedefaultdodoubleelse
enumexternfloatforgoto
ifintlongregisterreturn
shortsignedsizeofstaticstruct
switchtypedefunionunsignedvoid
volatilewhile

After C90, C99 and C11 expanded the set of keywords with additions such as _Bool, _Complex, inline, _Thread_local, _Alignas, and _Static_assert, supporting data representation, optimization, multithreading, alignment, and compile-time validation.

3.2. Preprocessor Directives

In C90 (ANSI C, ISO C90), there are 12 preprocessor directives defined.

#include#define#undef#if#ifdef
#ifndef#elif#else#endif#line
#error#pragma

After C90, C99 and C11 did not add new preprocessor directives, but the range of expression and usage was expanded through variadic macros such as #define LOG(fmt, ...) and preprocessor operators such as _Pragma("pack(1)").


4. Versions of the C Language

4.1. C90 (ANSI C, ISO C90)

  • C90 is the first official C language standard, standardized in 1989~1990.
  • It established the core grammar and structure of the C language, including basic data types, control statements, functions, and the preprocessor.

4.2. C99

  • C99 is a standard established in 1999, focused on expanding expressiveness and practicality.
  • It added modern programming features such as the Boolean type, complex numbers, inline, variadic macros, and declarations inside a block.

4.3. C11

  • C11 was established in 2011 and evolved with the goal of supporting multi-core and parallel environments.
  • Features strengthening system programming were introduced, such as _Thread_local, _Atomic, alignment control, and compile-time checking.

Popular posts from this blog

Vibe Coding - Creating Code with Natural Language

Computer Operations Explained with Gate Circuits