C语言听课笔记

c语言的第一个程序详解

//导入一个文件  stdio.h std是一个标准库 i input o output   标准输入输出库 .h 头文件
//<>表示导入系统文件 "" 表示导入自定义文件
#include 
//两个正斜杠表示注释  行注释 只能注释一行
/*
这是一个块注释
可以注释多行
注释内容不参与程序编译
*/
//int 数据类型  整型 如果出现在函数中表示函数的返回值
//main 程序的主函数  程序有且只有一个主函数 ()里面表示函数的参数 函数的参数可以有多个 中间用逗号分隔int main()
{//{} 函数体 代码体 程序体//printf 是stdio.h中系统提供的函数 表示在标准输出设备上打印字符串 //"" 称为字符串 ;一条语句的结束// //\n是一个转义字符  表示换行printf("hello world!\n");//return 如果出现在其他函数中表示函数结束  如果出现在main 函数中表示程序结束//0 表示函数的返回值 要和函数返回值类型对应return 0;
}

window中GCC 设置

找到QT安装目录C:\Qt\Qt5.4.1\5.4\mingw491_32\bin C:\Qt\Qt5.4.1\Tools\mingw491_32\bin 在我的电脑右击选择属性 在高级系统设置选择环境变量 在系统PATH 下将目录添加

gcc编译程序:
gcc -o hello.exe hello.c

语言的发展史:

机器语言-》汇编语言-》高级语言

C语言分布编译:

1、预处理 宏定义展开 头文件展开 条件编译 去掉注释
2、编译 检查语法 将C语言转成汇编语言
3、汇编 将汇编语言转成机器语言(二进制码)
4、链接 将C语言依赖库链接到程序中

常见代码问题:

1、编辑时异常
2、编译时异常
3、运行时异常

汇编语言:

mov 移动
add 添加
push 压栈
pop 出栈
call 调用

eax 32位寄存器
简单加法计算:

	//__asm//{//	mov a, 10//	mov b, 20//	mov eax, a//	add eax, b//	mov c, eax//}

断点和单步调试:

断点: 在选中行单击或按F9
调试程序:在“调试”中选择“开始调试”或按F5
逐语句执行:F11
逐过程执行:F10
跳出过程:Shift+f11
停止调试:Shift+F5

常量

在程序运行过程中 其值不能发生改变的量 成为常量
定义常量方式:
const 数据类型 常量名 = 值 (不安全)
#define 常量名 值

变量

在程序运行过程中 其值可以发生改变的量 成为变量

定义格式:
** 数据类型 变量 = 值**

标识符:

1、不能使用系统关键字
2、允许使用字母数字下划线
3、不允许数字开头
4、标识符区分大小写
5、见名知义

scanf安全问题解决:

#define _CRT_SECURE_NO_WARNINGS
pragma warning(disable:4996)

原码与补码

char ch = 10;
原码:0000 1010
反码:0000 1010
补码:0000 1010
char ch1= -10;
原码:1000 1010
反码:1111 0101
补码:1111 0110 f6

ch-ch1=0;

补码:0000 1010
补码:1111 0110
1 0000 0000
0000 0000

0在计算机中存储的方式:

int a=0;
0000 0000

76 - 32 76+(-32) = 44
原码:0100 1100
反码:0100 1100
补码:0100 1100

原码:1010 0000
反码:1101 1111
补码:1110 0000

补码:0100 1100
补码:1110 0000
1 0010 1100
补码:0010 1100
反码:0010 1100
原码:0010 1100 44

76 - 82
原码:0100 1100
反码:0100 1100
补码:0100 1100

原码:1101 0010
反码:1010 1101
补码:1010 1110

补码:0100 1100
补码:1010 1110

补码:1111 1010
反码:1111 1001
原码:1000 0110 -6

有符号字符型取值范围:-2^7 ~ 2^7-1 -128~127
8bit数据区间为 -2^7 ~ 2^7-1
原码:0111 1111 2^7-1=127
最小值:
原码:1111 1111 -127
反码:1000 0000
补码:1000 0001 =-127

==数据存储时将-0对应的区间值设为最小值 -2^7 ==

signed int 32bit -2^31 ~ 2^31-1 -2147483648 ~ 2147483647

long long 64 -2^63 ~ 2^63 -1

无符号

数据存储在计算机中不存在符号位
unsigned char
1111 1111 2^8-1
0000 0000 0
unsigned char 0 ~ 255

unsigned int 0 ~ 4294967295
1111 1111 1111 1111 1111 1111 1111 2^32-1 4294967295
0000 0000 0000 0000 0000 0000 0000 0

char ch = 127;
ch=ch+2;

补码:0111 1111
补码:0000 0010

补码:1000 0001
反码:1000 0000
原码:1111 1111 = -127

运算符优先级别:

1、()
2、++、–、!、sizeof、(数据类型) 结合性从右到左 单目运算符
3、、/、%
4、+、-
6、>、>=、<、<=
7、==、 !=
11、&&
12、||
14、=、+=、-=、
=、/=

代码演示(条件运算符)

if else

#define _CRT_SECURE_NO_WARNINGS
#include
#include
int main()
{int score;scanf("%d", &score);//如果表达式结果为真,执行{}里面的if (score > 700){printf("我就不上清华上北大\n");if (score > 720){printf("我可以上清华的计算机\n");}else if (score > 710){printf("我要学考古\n");}else{printf("我还是学挖掘机把");}}else{printf("想上北大分不够啊\n");}system("pause");return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include
#include
int main()
{int a, b, c;printf("输入三只兔子的体重:\n");scanf("%d%d%d", &a,&b,&c);//如果想要12,56,78这样输入值得话应该变成scanf("%d,%d,%d",&a,&b,&c);if (a > b){if (a > c){printf("a最重就吃它吧\n");}else{printf("c最重还是吃它吧");}}else if (b > c){printf("b最重还是吃它吧\n");}else{printf("c最重还是吃它吧\n");}system("pause");return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#define MAX(a,b) (a)>(b)?(a):(b)
int main()
{int a = 10;int b = 20;printf("最大值为%d\n", MAX(a, b));system("pause");return 0;
}
int main0()
{int a = 10;int b = 20;int c;//表达式1?表达式2:表达式3c=a > b ? a : b;printf("最大值为%d\n", c);//if (a > b)//{//     printf("a大\n");//}//     else//     {//            printf("b大\n");//     }system("pause");return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#define MAX(a,b) (a)>(b)?(a):(b)
int main()
{int score;scanf("%d", &score);switch (score / 10){case 10:printf("优秀\n");break;case 9:printf("优秀\n");break;case 8:printf("良好\n");break;case 7:printf("及格\n");break;case 6:printf("及格\n");break;default:printf("不及格\n");break;}system("pause");return 0;
}

switch case(break)

#define _CRT_SECURE_NO_WARNINGS
#include
#include
#define MAX(a,b) (a)>(b)?(a):(b)
int main()
{int score;printf("请输入1到7:\n");scanf("%d", &score);switch (score){case 1:printf("星期一\n");break;case 2:printf("星期二\n");break;case 3:printf("星期三\n");break;case 4:printf("星期四\n");break;case 5:printf("星期五\n");break;case 6:printf("星期六\n");break;case 7:printf("星期七\n");break;default:printf("WARNING!!!BOOM SHA KA LA KA!!!\n");break;}system("pause");return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#define MAX(a,b) (a)>(b)?(a):(b)
int main()
{int score;printf("请输入1到7:\n");scanf("%d", &score);switch (score){case 1:printf("星期一\n");break;case 2:printf("星期二\n");break;case 3:printf("星期三\n");break;case 4:printf("星期四\n");break;case 5:printf("星期五\n");break;case 6:printf("星期六\n");break;case 7:printf("星期七\n");break;default:printf("WARNING!!!BOOM SHA KA LA KA!!!\n");break;}system("pause");return 0;
}

while 循环

#define _CRT_SECURE_NO_WARNINGS
#include
#include
int main()
{int i = 1;while (i<=10000){if (i % 2 == 0){printf("%d\n", i);}i++;}system("pause");return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include
#include
int main(void)
{int i = 1;while (i<100){//7的倍数 || 个位带7的 || 十位带7的if (i % 7 == 0||i%10==7||i/10==7){printf("敲桌子\n");}else{printf("%d\n", i);}i++;}system("pause");return 0;
}

do while

#define _CRT_SECURE_NO_WARNINGS
#include
#include
int main(void)
{int i = 0;do{printf("%d\n", i);++i;} while (i < 10);system("pause");return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
//水仙花 从100-999各个位数的立方和相加等于本身就是一个水仙花数
int main(void)
{int i = 100;do{//个位数int a = i % 10;//十位数int b = i / 10 % 10;//百位数int c = i / 100;if (pow(a,3) + pow(b, 3) + pow(c, 3)==i)//if (a^){printf("%d\n", i);}i++;} while (i <= 999);system("pause");return 0;
}

for 循环

#define _CRT_SECURE_NO_WARNINGS
#include
#include
int main(void)
{//int i;gcc -o hello.out hello.c (-std=c99)int j = 5;for (int i = 0; i < 10,j<10;i++,j+=5){printf("%d\n", i);}system("pause");return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include
#include
int main(void)
{int i = 0;for (; ;){if(i>=10)//在循环语句中遇到break表示结束循环break}printf("%d\n", i);i++;system("pause");return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
int main(void)
{//差生随机数//1.导入头文件 time.h stdlib.h//2.添加随机数种子//3.获取随机数srand((unsigned int)time(NULL));//每次随机数不一样//int value=rand()%10;//0~9for (int i = 0; i < 10; i++){int value = rand() % 10;printf("%d\n", value);break;}system("pause");
}
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(void)
{srand((unsigned int)time(NULL));//每次随机数不一样int value=rand()%100;//0-99int num;//for (;;)while(1){printf("请输入数字:\n");scanf("%d", &num);if (value > num){printf("您输入的数太小了\n");}else if (value < num){printf("您输入的数太大了\n");}else{printf("恭喜你,猜对了\n");break;}}system("pause");return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
int main(void)
{//外层执行一次,内层执行一周for (int i = 0; i < 5; i++){for ( int j= 0;  j < 5; j++){printf("i=%d,j=%d\n", i, j);}}system("pause");return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
#include
int main(void)
{for (int s = 0; s < 24; s++){for ( int f= 0;  f < 60; f++){for (int m = 0; m < 60; m++){printf("%d:%d:%d\n", s, f, m);Sleep(900);system("cls");}}}system("pause");return 0;
}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部