C语言基本结构
# 第一个 C 语言程序
#include <stdio.h>
main() {
printf("Hello, World!\n");
}
1
2
3
4
5
2
3
4
5
保存文件为 hello.c
# 编译
cc hello.c
1
# 运行
./a.out
1
如下
➜ clangcode cc hello.c
hello.c:3:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
main() {
^
1 warning generated.
➜ clangcode ./a.out
Hello, World!
1
2
3
4
5
6
7
2
3
4
5
6
7
# 分析
#include <stdio.h> // 包含有关标准库的信息
main() { // 定义 main 函数,不接受参数,main 函数的语句包含在花括号中 printf("Hello, World!\n"); // main 函数调用库函数 printf 打印字符序列,\n 代表换行符 }
# 总结
每一个 C 函数,都由变量和函数组成。
printf 不会自动换行,无论多少个 printf 语句。
printf("hello,");
printf("world");
printf("\n");
// 打印结果如下
// hello,world
1
2
3
4
5
6
2
3
4
5
6
\n 只代表一个字符。
类似 \n 这样的字符为不能打印或者不可见字符提供了通用的扩展机制。
其他的换码序列还有:
制表符 \t
回退符 \b
双引号 \"
反斜杠本身 \\
1
2
3
4
2
3
4
注意:当输入的转义字符不受支持时,会原样输出,且编译和运行都不会报错。
# 实战
# VC6
下面三种写法在 Microsoft Visual C++ 6.0 都可以编译通过并执行成功。
最后一种虽然能运行通过,但是编译有警告 warning C4508: 'main' : function should return a value; 'void' return type assumed
,前两种写法都不会有警告。
#include "stdafx.h"
int main(int argc, char* argv[]){
printf("Hello World!\n");
return 0;
}
//void main(){
// printf("hello\n");
//}
//main(){
// printf("hello\n");
//}
//void print1(){
// printf("hello in fun\n");
//}
//main(){
// print1();
//}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# CMake
cmake 使用 c90 标准也能通过
cmake_minimum_required(VERSION 3.23)
project(clangcode C)
set(CMAKE_C_STANDARD 90)
add_executable(clangcode hello.c)
1
2
3
4
5
6
2
3
4
5
6
编辑 (opens new window)
上次更新: 2023/02/22, 13:47:25
- 01
- 解决css部分border被圆角切掉之后圆角的边框消失问题03-18
- 02
- 使用TypeScript开发一个自定义的Node-js前端开发脚手架03-08
- 03
- Github-Actions使用release-please实现自动发版03-06