← C EnglishChapter 01 of 13

Introduction to C

## Learning Objectives - Understand C history and importance - Set up C development environment - Write and run your first C program - Understand C syntax basics ## What is C? C is a procedural programming language developed by Dennis Ritchie at Bell Labs in 1972. It was designed to implement the UNIX operating system. ```c #include int main(void) { printf("Hello, World!\n"); return 0; } ``` ## Why Learn C? ### Key Features - **Low-level access** - Direct memory manipulation - **Efficiency** - Fast execution, minimal runtime - **Portability** - Runs on virtually any platform - **Foundation** - Inspired C++, Java, Python, and more - **Control** - Complete control over system resources ### Common Uses | Area | Examples | |------|----------| | Systems Programming | Operating systems, drivers, embedded systems | | Application Software | Databases, compilers, interpreters | | Embedded Systems | Microcontrollers, IoT devices | | Game Development | Game engines, high-performance graphics | ## Installing C ### Linux ```bash sudo apt-get install build-essential ``` ### macOS ```bash xcode-select --install ``` ### Windows Install MinGW or use WSL (Windows Subsystem for Linux) ### Verify Installation ```bash gcc --version ``` ## Your First Program ### File: hello.c ```c #include int main(void) { printf("Hello, World!\n"); return 0; } ``` ### Compile and Run ```bash gcc hello.c -o hello # Compile ./hello # Run the program ``` Output: ```text Hello, World! ``` ## C Program Structure ```c #include // Preprocessor directive int main(void) { // Main function (entry point) // Statements return 0; // Return status } ``` ### Components 1. **Preprocessor directives** - `#include` statements 2. **Main function** - Entry point of every C program 3. **Statements** - Commands to execute 4. **Return statement** - Exit status (0 = success) ## Comments ```c // Single-line comment /* * Multi-line comment */ ``` ## The printf Function ```c // Basic output printf("Hello\n"); // Formatted output printf("Name: %s, Age: %d\n", "Alice", 30); // Escape sequences printf("Tab:\tTab\n"); printf("Newline:\nDone\n"); printf("Backslash: \\\n"); printf("Percent: %%"); ``` ### Format Specifiers | Specifier | Type | |-----------|------| | `%d` | int | | `%f` | float/double | | `%c` | char | | `%s` | string | | `%p` | pointer | | `%%` | literal % | ## The main Function ### Standard Forms ```c int main(void) { return 0; } int main(int argc, char *argv[]) { // argc: argument count // argv: argument values return 0; } ``` ### Command Line Arguments ```c #include int main(int argc, char *argv[]) { printf("Program: %s\n", argv[0]); for (int i = 1; i < argc; i++) { printf("Arg %d: %s\n", i, argv[i]); } return 0; } ``` ```bash gcc main.c -o main ./main Alice 42 ``` Output: ```text Program: ./main Arg 1: Alice Arg 2: 42 ``` ## Input with scanf ```c #include int main(void) { int age; printf("Enter your age: "); scanf("%d", &age); printf("You are %d years old\n", age); return 0; } ``` ## IDEs ### Popular IDEs - VS Code (recommended with C/C++ extension) - CLion - Eclipse CDT - Code::Blocks ## C Standards | Standard | Year | Features | |----------|------|----------| | C89/C90 | 1989 | Original ANSI C | | C99 | 1999 | `//` comments, `bool`, `stdint.h` | | C11 | 2011 | Multithreading, Unicode support | | C17 | 2017 | Bug fixes, no new features | | C23 | 2023 | Latest standard | ```bash gcc -std=c11 hello.c -o hello ``` ## Summary - C was created in 1972 by Dennis Ritchie - GCC is the GNU C compiler (use `gcc --version` to check) - Every C program needs `main()` function - `gcc file.c -o program` compiles C code - `printf()` outputs formatted text - `scanf()` reads input - Comments: `//` or `/* */`

Comments

Comments powered by Giscus

To enable comments, add your Giscus embed code here.

Learn more about Giscus →