C Programming

C Language Elements

One advantage of a high-level language like C is that it lets you write programs that resemble everyday English.

The image below shows the basic structure of a C program:

Even though you do not yet know how to write your own program, you can probably read and understand the program.

Let's explain the key elements of a C program briefly:

Preprocessor Directives

Preprocessor directives are lines included in the code of programs preceded by a hash sign (#). They are not program statements but directives for the preprocessor, which is a program that processes the source code before it is compiled.

The preprocessor directives are commands that give instructions to the C processor, whose job it is to modify the text of a C program before it is compiled.

The two most common preprocessor directives are #include and #define.

Every C implementation contains collections of useful functions and symbols called libraries

The ANSI (American National Standards Institute) requires that all C implementations provide a set of standard libraries.

An individual programmer can also create libraries of functions.

Each library has a standard header file whose name ends with the symbol .h.

The #include directive gives a program access to a library which causes the preprocessor to insert definitions from a standard header file into a program before compilation.

For instance the printf and scanf functions are defined in the stdio.h header file and notifies the preprocessor that some names used in the program (printf and scanf) are found in that file.

The #define directive is used to define constants and macros in C programs. This directive instructs the preprocessor to replace each occurrence of PI in the text of the C program by 3.142 before compilation begins.

As a result, area = PI * radius * radius; becomes area = 3.142 * radius * radius; before compilation.

Comments

Comments are notes added to the code that are ignored by the compiler. They are used to explain the purpose and functionality of the code, making it easier for others (and yourself) to understand and maintain.

In C, comments can be written in two ways:

  1. Single-line comments: These start with // and continue until the end of the line. For example: // This is a single-line comment
  2. Multi-line comments: These start with /* and end with */. They can span multiple lines. For example: /* This is a multi-line comment that can span multiple lines */

Comments are essential for documenting your code and making it more readable. They can explain the logic behind complex sections of code, provide context for why certain decisions were made, and help others (or yourself in the future) understand the code more quickly.

Function main

The main function is the entry point of a C program. When you run a C program, execution starts from the main function.

The remaining lines of the program form the body of the function, which is enclosed in curly braces {}.

A function body has two parts: the declaration and the executable statements.

The declarations tell the compiler what memory cells are needed in the function (e.g. radius and area in the above code caption)

The executable statements contain the actual instructions (derived from the algorithm) that the computer will execute. This is translated by the compiler into machine language and later executed.

The main function contains punctuation and special symbols (*,=)

Commas (,) separate items in a list.

Semicolons (;) mark the end of a statement.

Braces ({}) mark the beginning and end of the body of a function or control structure.

Reserved Words

Reserved words are keywords that have special meanings in the C language. They cannot be used as identifiers (variable names, function names, etc.). Some common reserved words include int, float, char, if, else, for, while, and return.

All reserved words appear in lowercase letters.

Standard Identifiers

Standard identifiers are names given to variables, functions, and other entities in a C program. They must follow certain rules:

  1. They can contain letters (a-z, A-Z), digits (0-9), and underscores (_)
  2. They cannot start with a digit
  3. They cannot be reserved words
  4. They are case-sensitive (e.g., myVar and myvar are different)

Identifiers come in two types: standard and user-defined.

Standard identifiers are predefined in the C language and have specific meanings (e.g., printf, scanf, main, etc.).

User-defined identifiers are created by the programmer to name variables, functions, etc. (e.g., radius, area, etc.).

Valid Identifiers

  1. myVariable
  2. _myVariable
  3. my_variable
  4. myVariable123
  5. myVariable_123

Invalid identifiers

  1. 123myVariable (starts with a digit)
  2. my-Variable (contains a hyphen)
  3. my Variable (contains a space)
  4. int (reserved word)
  5. myVariable! (contains an exclamation mark)

Uppercase and Lowercase Letters

Identifiers in C are case-sensitive, meaning that uppercase and lowercase letters are treated as different characters. For example, myVariable and myvariable are considered two different identifiers.

Adopting consistent naming conventions can improve code readability and maintainability.

Common naming conventions include camelCase and snake_case.

camelCase: The first letter of each word is capitalized except for the first word (e.g., myVariableName).

snake_case: Words are separated by underscores and all letters are lowercase (e.g., my_variable_name).

Uppercase letters are most commonly used for constants in C (e.g., MAX_SIZE, PI).

Choosing Identifiers

When choosing identifiers, it's important to select names that are descriptive and meaningful. This makes the code easier to read and understand.

For example, instead of naming a variable x, you could name it radius if it represents the radius of a circle. This way, anyone reading the code can immediately understand what the variable represents.

Pick a meaning name for a user-defined identifier so its use is easy to understand. For example, radius is a meaningful name for a memory cell used to store the radius of a circle.

If an identifier contains multiple words, consider using either camelCase or snake_case for better readability.

Avoid excessive long names to avoid making a typing error in a long name.

If you mistype an identifier, it can lead to compilation errors or unexpected behavior in your program.

To avoid confusion, do not choose names that are too similar to each other.

Especially avoid using names that differ only by case (e.g., myVariable and myvariable).