01.基础篇-HelloWorld
1.1 环境
使用 clion
做为开发工具。构建 项目 cbooks
。
1.2 HelloWorld
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
include
是关键词,加载lib
包,类比java
的import
关键词main
入口函数,同Java
的main
函数
1.3 小结
- C 语言 HelloWorld 如此简单
- 开发工具的进步,远超 大学时期的 记事本
1.4 工程目录规划
├── CMakeLists.txt
├── ch01
│ ├── hello_world.c
│ └── hello_world.h
├── ch02
├── ch03
......
└── main.c
1.4.1 重新构建 HelloWorld的代码
hello_world.h 定义 接口
//
// Created by xx wang on 2021/2/16.
//
#ifndef CBOOKS_HELLO_WORLD_H
#define CBOOKS_HELLO_WORLD_H
/**
* hello world 引入C语言
*/
void hello_world();
#endif //CBOOKS_HELLO_WORLD_H
hello_world.c 接口的实现
//
// Created by xxx wang on 2021/2/16.
//
#include "stdio.h"
void hello_world(){
printf("Hello World!\n");
}
主函数 main
#include "ch01/hello_world.h"
int main() {
hello_world();
return 0;
}
CMakeLists.txt 工程管理文件
cmake_minimum_required(VERSION 3.17)
project(cbooks C)
set(CMAKE_C_STANDARD 99)
add_executable(cbooks main.c ch01/hello_world.h ch01/hello_world.c)