一、变量
1 #include<stdio.h>
2 #include<stdlib.h>
3
4 void main0(){
5 //数据使用必须在范围内,否则产生溢出
6 unsigned short num=65535+1;//+1之后溢出为0
7 //printf("%d",sizeof(num));
8 printf("阿飞有%d元",num);
9 getchar();
10 }
11
12 void main1(){
13 short num=32767+1;//+1之后溢出为-32768
14 printf("阿飞有%d元",num);
15 getchar();
16 }
17
18 void main2(){
19 //printf的本质是按照自己的方式,将变量转换为字符串,无论变量以前是什么类型,而且printf不会自动转换
20 printf("%d",1);//%d整数
21 printf("n%f",1);//%f实数 打印出0.000000 1改成1.0之后打印正确
22 getchar();
23 }
24
25 void main3(){
26 //使用printf打印的时候,必须要类型匹配,否则出错
27 printf("%d,%u",-1,-1);//%d有符号十进制,%u无符号十进制
28 getchar();
29 }
30 //能编译,不能保证结果正确
31 void main(){
32 printf("%d",-10);//正确
33 printf("n%u",-10);//错误
34 printf("n%x",-10);//无符号16进制,错误
35 printf("n%o",-10);//无符号8进制,错误
36 getchar();
37 }
二、随机数
1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<time.h>
4
5 void main(){
6 //三目运算符:0就执行notepad,非0执行calc
7 //-1?system("calc"):system("notepad");
8 int num=rand()%100;//0~99的随机数
9 printf("%dn",num);
10 num>80?printf("你赢了"):printf("你输了");
11 getchar();
12 }
经过测试,上述代码只能产生一次随机数,即多次运行结果不变。那么如何让程序每次都产生不同的随机数呢?
1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<time.h>
4
5 void main(){
6 int num;
7 time_t t;//定义时间类型的变量
8 //time是一个函数,获取时间,复制给t,转换为unsigned int类型
9 srand((unsigned int)time(&t));
10
11 num=rand()%100;//0~99的随机数
12 num=rand()%100+100;//100~199的随机数
13 printf("%dn",num);
14 num>80?printf("你赢了"):printf("你输了");
15 getchar();
16 }
三、随机恶作剧
1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<windows.h>
4 #include<time.h>
5
6 _declspec(dllexport) void go1(){
7 while(1){
8 malloc(10*1024*1024);//10M
9 Sleep(1000);
10 }
11 }
12
13 _declspec(dllexport) void go2(){
14 while(1){
15 MessageBoxA(0,"马化腾邀请你共进晚餐","享受终生QQ红钻",0);
16 Sleep(3000);
17 }
18 }
19
20 _declspec(dllexport) void randgo(){
21 int num;
22 time_t t;
23 srand((unsigned int)time(&t));
24 num=rand()%100;
25 num>60?go1():go2();
26 }
然后注射到程序中,实现恶作剧
四、数据类型
1 #include<stdio.h>
2 #include<stdlib.h>
3
4 void main()
5 {
6 int num=0;
7 char ch='0';
8 printf("%d,%c",num,num);
9 printf("n%d,%c",ch,ch);
10 system("pause");
11 }
稍作修改如下
1 #include<stdio.h>
2 #include<stdlib.h>
3
4 void main(){
5 printf("%d",sizeof(4));//int的常量 4个字节
6 printf("n%d",sizeof(4.0));//double 8个字节
7 printf("n%d",sizeof(4.0f));//float 4个字节
8 printf("n%d",sizeof(4u));//unsigned int 4个字节
9 getchar();
10 }
1 #include<stdio.h>
2
3 void main(){
4 //数据类型指明了二进制该如何解析
5 int num1=10;
6 float num2=10.0;
7 printf("%x,%f",num1,num1);
8 printf("n%x,%f",num2,num2);
9 printf("n%x,%x",&num1,&num2);
10 getchar();
11 }
五、三角形面积
1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<math.h>
4
5 void main(){
6 float a,b,c,s,p;
7 scanf("%f%f%f",&a,&b,&c);//数据输入,初始化a,b,c
8 printf("a=%f,b=%f,c=%fn",a,b,c);
9 p=(a+b+c)/2;
10 s=sqrt(p*(p-a)*(p-b)*(p-c));//sqrt求平方根
11 printf("面积s=%f",s);
12 system("pause");
13 }
六、字符转换成整数
1 #include<stdio.h>
2 #include<stdlib.h>
3
4 void main(){
5 //字符0的编号是48,按照字符是0
6 printf("字符0编号=%d,字符0的字符=&c",'0','0');
7 printf("n整数0编号=%d,整数0的字符=&c",0,0);
8 printf("n%d",'0'-0);//字符转换成整数
9 printf("n%d",'1'-1);//都等于48
10 system("pause");
11 }
七、大小写转换
1 #include<stdio.h>
2 #include<stdlib.h>
3
4 void main()
5 {
6 char ch=getchar();
7 //putchar(ch+32);//大写->小写
8 putchar(ch-32);//小写->大写
9 system("pause");
10 }
八、字符串拼接
1 #include<stdio.h>
2 #include<stdlib.h>
3
4 void main(){
5 char str1[10]="note";
6 char str2[10]="pad";
7 char str3[10];
8 sprintf(str3,"%s%s",str1,str2);
9 system(str3);
10 getchar();
11 }
原文链接: https://www.cnblogs.com/little-monkey/p/7209836.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/257196
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!