C语言开发工具Dev-C++,CLionCygwin

Cygwin 和其他两个工具有一个即可, Cygwin主要是为了使用纯命令测试原理,
Dev-C++CLion工具自带C开发环境

环境搭建

使用Cygwin只是为了测试一下纯命令开发C语言

windows系统使用Cygwin搭建C语言开发环境

  1. 下载Cygwin安装,选择镜像http://mirrors.163.com,更多镜像参考https://cygwin.com/mirrors.html

  2. select Packages 中选择gcc-coregcc-g++makegdbbinutils五个包下载(注意在右侧三角选择版本)

  3. 配置环境变量,让cmd中可以使用

    把安装好的的Cygwin配置环境变量

    环境变量 C:\cygwin64\binC:\cygwin64\sbin

  4. 安装结束

第一个程序

把下面内容写入到fei.c文件中

1
2
3
4
5
6
7
8
9
#include <stdio.h>

int main()
{
printf("这是我的第一个C程序 \r\n");
printf("Hello, World! \n");

return 0;
}
1
2
3
4
5
6
7
8
9
#include <stdio.h>
#include <process.h>

int main() {
system("chcp 65001"); // process.h 这个库解决中文乱码
printf("Hello, World!\n");
printf("大ddd飞!\n");
return 0;
}

执行命令

1
2
3
4
#编译代码
gcc -o fei.out fei.c
#执行程序
fei.out 回车

C语言入门

用工具创建程序

使用Dev-C++CLion创建Console Application应用即可执行

入门dem01

成绩评估

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <stdio.h>

int main() {
// 优秀 :90—100之间
// 良好: 70-90
// 及格: 60-70
// 不及格:分数低于60分
system("chcp 65001");
int score;
printf("请输入你的成绩(0-100):");
scanf("%d:", &score);

// if 从上往下判断, 所以 else if, 依次写即可
if (score >= 90) {
printf("优秀,你真棒\n");
} else if (score >= 70) { // 等价 score >= 70 && score < 90
printf("良好,还不错\n");
} else if (score >= 60) { // 等价 score >= 60 && score < 70
printf("及格,要再努力提高\n");
} else { // 等价 score < 60
printf("不及格,别灰心,加把劲\n");
}

return 0;
}

控制语句

  1. 分支
    1. if
    2. switch
  2. 循环
    1. for
    2. while
    3. do…while
  3. 辅助控制
    1. continue
    2. break
    3. goto
    4. return

{}括起来的一组语句,(具有作用域功能)

1
2
3
4
5
6
7
8
9
10
int a = 2, b = 3, c;
c = a + b;
{
int c;
c = a * b;
printf("%d \n", c); // 输出括号里面z的值
}

printf("%d", c); // 输出括号外面z的值
// 输出结果 5 6

自增自减,记忆

1
2
3
4
5
6
7
8
9
10
// 先记忆for循环
int sum = 0; // 这里常写 i++, i++ 是最后计算
for (int i = 0; i <= 100; i++) {
sum += i;
}
printf("%d", sum);

int x = 2, y = 2;
printf("%d \n", x++); // 输出2
printf("%d", ++y); // 输出 3

输出空间大小sizeof

输出short, int, long, float, double, char等类型变量所占的存储空间大小

1
2
3
4
5
6
printf("%d \n", sizeof(short));
printf("%d \n", sizeof(int));
printf("%d \n", sizeof(long));
printf("%d \n", sizeof(float));
printf("%d \n", sizeof(double));
printf("%d \n", sizeof(char));

编程小工具

1
2
system("chcp 65001"); // 解决,中文乱码
fflush(stdin); // 刷新缓冲,否则下面的不能输入和获取

输出字符数组

1
2
3
4
5
char st = 'A'; // 输出一个字符
printf ("bar = %c ", st);

char str[10] = "abc_123"; // 输出一个字符数组
printf ("bar = %s ", str);