ClanguageBasicstutorial

LearningClanguage basicconcepts, 语法structure and DevelopmentEnvironment Setup

返回tutoriallist

1. ClanguageIntroduction

1.1 Clanguage history

Clanguage is 由丹尼斯·里奇 (Dennis Ritchie) in 20世纪70年代初于贝尔实验室Development . 它 is in Blanguage Basics on 发展而来 , 最初 is for 了DevelopmentUNIXoperationsystem而design .

Clanguage design理念 is : providing一种able towriting出 high 效, reliable , portable 程序 language, 同时保持language本身 简洁性 and 表达capacity.

1.2 Clanguage 特点

  • 简洁 high 效: Clanguageproviding了丰富 运算符 and dataclass型, 语法简洁明了, 生成 目标codequality high , runefficiency接近汇编language.
  • portability good : Clanguagewriting 程序可以 in 不同 硬件平台 on run, 只需 few 量modify or 无需modify.
  • functions强 big : Clanguageproviding了直接访问memory capacity, 可以writingsystem软件 and 底层驱动程序.
  • structure化programming: supportfunction, 条件语句, 循环语句etc.structure化programming元素.
  • 广泛application: Clanguage is system软件, 嵌入式system, 游戏Development and other fields 首选language.

1.3 Clanguage application领域

  • operationsystemDevelopment (such asUNIX, Linux)
  • 嵌入式systemprogramming
  • 游戏Development
  • 编译器 and 解释器Development
  • datalibrarysystem
  • networkprotocolimplementation
  • graph形user界面 (GUI) Development

2. DevelopmentEnvironment Setup

2.1 编译器选择

要编译 and runClanguage程序, 你需要一个C编译器. 以 under is 一些常用 C编译器:

2.1.1 GCC (GNU Compiler Collection)

GCC is 最流行 C编译器之一, 它 is open-source , support many 种平台, includingWindows, Linux and macOS.

2.1.2 Clang

Clang is a 轻量级 C编译器, 由LLVMprojectDevelopment, 它providing了更 good errorinformation and 更 fast 编译速度.

2.1.3 Visual C++

Visual C++ is MicrosoftDevelopment C/C++编译器, 集成 in Visual Studio IDEin, 主要用于Windows平台.

2.2 installationGCC

2.2.1 Windows平台

in Windows on installationGCC 最 simple method is usingMinGW (Minimalist GNU for Windows) or MSYS2.

  • under 载并installationMinGW
  • in installation过程in选择C编译器
  • 将MinGW binTable of Contents添加 to systemPATHenvironmentvariablein

2.2.2 Linux平台

big many 数Linux发行版默认installation了GCC, or 者可以throughpackagemanagement器easilyinstallation:

# Ubuntu/Debian
apt-get install gcc

# CentOS/RHEL
yum install gcc

2.2.3 macOS平台

in macOS on , 可以throughXcodecommands行toolinstallationGCC:

xcode-select --install

2.3 集成Developmentenvironment (IDE)

除了usingcommands行编译器, 你还可以using集成Developmentenvironment (IDE) 来writing, 编译 and debugC程序. 以 under is 一些常用 ClanguageIDE:

  • Code::Blocks: open-source, 跨平台 C/C++ IDE
  • Dev-C++: 轻量级 C/C++ IDE, 适合初学者
  • Visual Studio: functions强 big IDE, 主要用于Windows平台
  • Eclipse CDT: 跨平台 IDE, 需要installationC/C++Developmenttool
  • CLion: JetBrainsDevelopment 跨平台C/C++ IDE, functions强 big 但收费

3. 第一个C程序

3.1 Hello World程序

让我们writing第一个C程序, 这 is a 经典 "Hello World"程序:

#include <stdio.h>

int main() {
    printf("Hello, World!\n");
    return 0;
}

3.2 程序解析

让我们analysis一 under 这个程序 各个部分:

3.2.1 #include <stdio.h>

这 is a 预processing指令, 用于package含标准输入输出头file (stdio.h) . 这个头filepackage含了 printf etc.输入输出function 声明.

3.2.2 int main()

这 is 主function 声明. main function is C程序 入口点, 每个C程序都必须 has 且只 has 一个 main function. int 表示function返回一个整数class型 值.

3.2.3 { ... }

花括号 in in 容 is function体, package含了function 具体implementation.

3.2.4 printf("Hello, World!\n");

这 is a function调用, 调用 printf function输出string "Hello, World!". \n 表示换行符.

3.2.5 return 0;

这 is function 返回语句, return value for 0, 表示程序正常结束. in main functionin, 返回 0 通常表示程序成功执行, 非零值表示程序exception终止.

3.3 编译 and run程序

3.3.1 usingcommands行编译

fake设我们将程序保存 for hello.c, using以 under commands编译:

gcc hello.c -o hello

然 after run编译 after 程序:

# Windows
hello.exe

# Linux/macOS
./hello

3.3.2 usingIDE编译 and run

such as果你usingIDE, 通常可以through点击tool栏 on 编译 and run按钮来completion这些operation. 具体步骤因IDE而异, 但一般都非常直观.

4. Clanguage basic语法

4.1 标识符

标识符 is 用来命名variable, function, arrayetc. 名称. Clanguagein标识符 命名规则:

  • 只能由字母, number and under 划线组成
  • 不能以number开头
  • 区分 big small 写
  • 不能usingClanguage 关键字

example:

// 合法 标识符
int age;
float average_score;
char _grade;

// 非法 标识符
int 2name;     // 不能以number开头
float my-score; // 不能package含连字符
char for;       // 不能using关键字

4.2 关键字

关键字 is Clanguagein预留 具 has 特殊含义 单词, 不能用作标识符. 以 under is Clanguage 关键字:

auto break case char const continue default do double else enum extern float for goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while

4.3 语句 and 分号

Clanguagein 每个语句都以分号 (;) 结束. 分号 is 语句 终止符, 告诉编译器一个语句已经结束.

int a = 10; // 赋值语句
printf("a = %d\n", a); // function调用语句
return 0; // 返回语句

4.4 comment

comment is 程序in不被编译器执行 文本, 用于解释code functions and 逻辑. Clanguagesupport两种comment方式:

4.4.1 单行comment

// 这 is a 单行comment
int x = 5; // 这也 is a 单行comment

4.4.2 many 行comment

/* 这 is a 
    many 行comment */
int y = 10;

/*  many 行comment也可以
   用于comment掉一段code */
/*
int z = 15;
printf("z = %d\n", z);
*/

实践case: 温度转换器

writing一个C程序, 将华氏温度转换 for 摄氏温度.

requirementsanalysis

  • 提示user输入华氏温度
  • using公式: C = (F - 32) * 5/9 for转换
  • 输出转换 after 摄氏温度

referencecode

#include <stdio.h>

int main() {
    float fahrenheit, celsius;
    
    // 提示user输入华氏温度
    printf("请输入华氏温度: ");
    scanf("%f", &fahrenheit);
    
    // 转换 for 摄氏温度
    celsius = (fahrenheit - 32) * 5 / 9;
    
    // 输出结果
    printf("华氏温度 %.2f°F 转换 for 摄氏温度 is  %.2f°C\n", fahrenheit, celsius);
    
    return 0;
}

run结果

请输入华氏温度: 98.6
华氏温度 98.60°F 转换 for 摄氏温度 is  37.00°C

互动练习

练习1: writing一个C程序, 输出你 姓名 and 年龄.

提示: usingprintffunction输出string and variable.

练习2: writing一个C程序, 计算两个整数 and .

提示: usingscanffunction输入两个整数, 然 after 计算它们 and 并输出.

练习3: writing一个C程序, 将厘米转换 for 英寸.

提示: 1英寸 = 2.54厘米