Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Introduction to the C Programming Language

Tech 4

What is C?

C is a general-purpose computer programming language widely used in low-level development. The design goals of C were to provide a language that could be compiled in a straightforward manner, handle low-level memory, produce a small amount of machine code, and run without requiring any runtime environment support.

Although C provides many low-level processing capabilities, it maintains excellent cross-platform portability. A C program written to a standard specification can be compiled on many computer platforms, including embedded processors (microcontrollers or MCUs) and supercomputers.

In the 1980s, to prevent syntax variations among different compiler vendors, the American National Standards Institute (ANSI) established a complete set of American National Standard syntax rules for C, known as ANSI C, which served as the initial standard for the language. The current official standard, as of December 8, 2011, is C11, published by the International Organization for Standardization (ISO) and the International Electrotechnical Commission (IEC). This is the third and latest official C standard, offering better support for function names and identifiers using Chinese characters, enabling a degree of Chinese-language programming.

C is a procedural programming language, differing from object-oriented languages like C++ and Java. Its main compilers include Clang, GCC, WIN-TC, SUBLIME, MSVC, and Turbo C.

Your First C Program

#include <stdio.h>

int main() {
    printf("Hello, world!\n");
    printf("Welcome to C.\n");
    return 0;
}

Explanation:

  • The main function is the program's entry point.
  • A project must have exactly one main function.

The #include Directive (Preprocessor Command)

The #include directive is used to include header files (.h files) and is a type of C preprocessor command.

The #include process is simple: it inserts the contents of the specified header file into the location of the directive, effectively merging the header file with the current source file, similar to copy-pasting.

Two Ways to Use #include

#include <standard_header.h>
#include "my_header.h"

The difference between using angle brackets < > and double quotes " " lies in the search path for the header file:

  • Angle brackets < >: The compiler searches for the header file in the system's standard include paths.
  • Double quotes " ": The compiler first searches in the current directory (where the source file is located). If not found, it then searches the system paths.

Thus, using double quotes provides an additional search path, making it more flexible.

Standard headers like stdio.h and stdlib.h reside in system paths, so both angle brackets and double quotes can successfully include them. However, custom header files, typically located in the project directory, should be included using double quotes.

Note: For standard headers like stdio.h and stdlib.h, angle brackets are conventionally used.

While you could add your project directory to the system path to use angle brackets for custom headers, this is generally unnecessary and not recommended.

Important Notes on #include

  • A single #include directive can include only one header file. Multiple headers require multiple directives.
  • The same header file can be included multiple times without issues, as headers typically have mechanisms to prevent duplicate inclusion.
  • Header files can be nested; an incldued file can itself include other files.

Escape Sequences and Comments

Suppose you want to print a file path to the screen: c:\code\test.c

How would you write the code?

#include <stdio.h>

int main() {
    printf("c:\code\test.c\n");
    return 0;
}

However, the actual output might be unexpected due to how certain characters are interpreted. This introduces the concept of escape sequences—characters that change the meaning of the following character(s).

Common Escape Sequences

Escape Sequence Meaning
\? Used when writing multiple question marks to prevent trigraph parsing.
\' Represents a single quote character within a character constant.
\" Represents a double quote character within a string literal.
\\ Represents a literal backslash character.
\a Alert (bell) character.
\b Backspace character.
\f Form feed (new page).
\n Newline character.
\r Carriage return character.
\t Horizontal tab character.
\v Vertical tab character.
\ddd Character represented by 1–3 octal digits (e.g., \130 is 'X').
\xdd Character represented by 2 hexadecimal digits (e.g., \x30 is '0').

Example:

#include <stdio.h>

int main() {
    // How to print a single quote character ' ?
    printf("%c\n", '\'');
    // How to print a string containing a double quote character " ?
    printf("%s\n", "\"");
    return 0;
}

Test your understanding:

#include <stdio.h>
#include <string.h>

int main() {
    printf("%d\n", strlen("abcdef"));          // Output: 6
    // \t is a tab, \62 is an octal escape sequence for '2'
    printf("%d\n", strlen("c:\test\628\test.c")); // Analyze carefully!
    return 0;
}

Comments

Comments serve two primary purposes:

  1. To temporarily disable code without deleting it.
  2. To explain complex or non-obvious code.

Example:

#include <stdio.h>

int add(int a, int b) {
    return a + b;
}

/* C-style comment block.
   This function is currently commented out.
int subtract(int a, int b) {
    return a - b;
}
*/

int main() {
    // C++-style single-line comment.
    // int value = 10; // This line is commented out.
    
    // Call the add function and print the result.
    printf("%d\n", add(1, 2));
    return 0;
}

Comment Styles:

  • C-style comments: /* ... */
    • Can span multiple lines.
    • Drawback: They cannot be nested.
  • C++-style comments: // ...
    • Single-line comments.
    • Can be placed on consecutive lines for multi-line comments.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.