c语言基础

1.HelloWorld
#include <stdio.h>    //相当于java导包   standard io     java.lang 
#include <stdlib.h>   //standard lib 
main(){
       //控制台打印语句 
       printf("HelloWorld\n");   
       //执行windows指令       
       system("pause");
}
2.C的基本数据类型
#include <stdio.h> 
#include <stdlib.h>  

main(){
       //char, int, float, double, long, short, signed, unsigned
       
       signed int i = 3;
       //无符号只是注明最高位是数值位,但不会改变二进制表示的取值 
       unsigned int i2 = 4200000000;
       char c = 'a';
       long long ago = -420000000;
       
       printf("i的值为%d\n", i);
       printf("i2的值为%u\n", i2);
       printf("c的值为%c\n", c);
       
       printf("short的长度为%d\n", sizeof(short)); 
       printf("int的长度为%d\n", sizeof(int)); 
       printf("long的长度为%d\n", sizeof(long)); 
       printf("float的长度为%d\n", sizeof(float)); 
       printf("double的长度为%d\n", sizeof(double)); 
       printf("char的长度为%d\n", sizeof(char)); 
       printf("long long的长度为%d\n", sizeof(long long)); 
       printf("long int的长度为%d\n", sizeof(long int)); 
       printf("short int的长度为%d\n", sizeof(short int));
       system("pause");
}

c语言基础

3.输出函数
#include <stdio.h> 
#include <stdlib.h>  

main(){
       /*
        %d  -  int
        %ld – long int
        %lld - long long
        %hd – 短整型
        %c  - char
        %f -  float
        %lf – double
        %u – 无符号数
        %x – 十六进制输出 int 或者long int 或者short int
        %o -  八进制输出
        %s – 字符串
        */
       int i = 3;
       long int ld = 334;
       long long lld = 4200000000;
       short s = 245;
       printf("%d\n", i);
       printf("%ld\n", ld);
       printf("%lld\n", lld);
       printf("%hd\n", s);
       
       char c = 'a';
       printf("%c\n", c);
       
       float f = 3.14666666;
       double d = 3.14666666;
       printf("%f\n", f);
       printf("%lf\n", d);
       
       printf("%#x\n", ld);
       printf("%#o\n", ld);
       
       //C的字符串是字符数组
       //字符数组末尾一定有一个结束符“\0” 
       char str[] = "sb光头鹏";
       printf("%s\n", str);
       system("pause");
}

c语言基础

4.输入函数2
#include <stdio.h> 
#include <stdlib.h>  

main(){
       printf("请输入班级人数:");
       int count;
       //取出count的内存地址,把用户输入的数据保存在这个地址上 
       scanf("%d", &count);
       
       printf("请输入班级名称:");
       //定义字符数组,必须指定长度
       //C的数组不检测越界 
       char name[7];
       scanf("%s", &name);
       
       printf("count的地址为%#x\n", &count);
       printf("name的地址为%#x\n", &name);
       printf("班级人数为:%d,班级名称为%s\n", count, name); 
       system("pause");
}

c语言基础

5.内存地址
#include <stdio.h> 
#include <stdlib.h>  

main(){
       int i;
       i = 3;
       printf("%d\n", i);
       printf("%#x\n", &i);
       
       system("pause");
}

c语言基础

6.内存修改器
#include <stdio.h> 
#include <stdlib.h>  

main(){
       int time = 60;
       int i;
       printf("%#x\n", &i);
       for(i = time; i > 0; i--){
              printf("剩余时间为%d\n", i); 
              sleep(2500);  
       }
       
       system("pause");
}

c语言基础

7.指针
#include <stdio.h> 
#include <stdlib.h>  

main(){
       int i;
       i = 3;
       //定义一个指针变量 
       int* p = &i;
   
       //定义一个二级指针 
       int** q = &p;
       printf("%d\n", i);
       printf("%#x\n", &i);
       printf("%#x\n", p);
       printf("%d\n", *p);
       printf("%d\n", **q);
       
       system("pause");
}

c语言基础

8.常见错误
#include <stdio.h> 
#include <stdlib.h>  

main(){
       int* p;
       int i = 3;
       double d = 3.14;
       p = &i;
       double* q = &d;
       printf("%#x\n", q);
       printf("%lf\n", *q);
       
       system("pause");
}

c语言基础

9.值传递和引用传递
#include <stdio.h> 
#include <stdlib.h>  

void function(int* p, int* q){
       int temp = *p;
       *p = *q;
       *q = temp;
}
main(){
       int i = 3;
       int j = 5;
       function(&i, &j);
       
       printf("i的值为%d\n", i);
       printf("j的值为%d\n", j);
       system("pause");
}

c语言基础

10.返回多个值
#include <stdio.h> 
#include <stdlib.h>  

void function(int* p, int* q){
      *p = *p * 2;
      *q = *q * 2;
      
}
main(){
       int i = 3;
       int j = 5;
       function(&i, &j);
       
       printf("i的值为%d\n", i);
       printf("j的值为%d\n", j);
       system("pause");
}

c语言基础

11.在主函数中获取子函数的变量的地址
#include <stdio.h> 
#include <stdlib.h>  

void function(int** p){
      int i = 3;
      *p = &i;
     printf("子函数打印地址%#x\n", &i);
}
main(){
       int* mainp;
       function(&mainp);
       
       printf("主函数打印地址%#x\n", mainp);
       printf("i的值为%d\n", *mainp);
       system("pause");
}

c语言基础

12.数组
#include <stdio.h> 
#include <stdlib.h>  

main(){
       //char c[] = "hello";
       int c[] = {1,2,3,4,5};
       printf("%#x\n", &c[0]);
       printf("%#x\n", &c[1]);
       printf("%#x\n", &c[2]);
       printf("%#x\n", &c[3]);
       
       printf("%#x\n", &c);
       //字符数组的地址,就是第一个字符的地址 
       int* p = &c;
       
       //c+1表示位移一个单位,一个单位是几个字节根据指针类型决定 
       printf("第0个元素的值%d\n", *(p + 0));
       printf("第1个元素的值%d\n", *(p + 1));
       printf("第2个元素的值%d\n", *(p + 2));
       printf("第3个元素的值%d\n", *(p + 3)); 
       printf("两个地址的距离%d\n", (p + 3) - (p + 1)); 
       
       system("pause");
}

c语言基础

13.指针长度
#include <stdio.h> 
#include <stdlib.h>  

main(){
       //指针长度为4个字节,因为32位环境,内存地址长度就是4 
       printf("int*的长度是%d\n", sizeof(int*));
       printf("double*的长度是%d\n", sizeof(double*));
       
       int i = 3;
       int j = 5;
       
       //不是连续空间的地址,不能做指针运算 
       printf("%#x\n", &i);
       printf("%#x\n", &j);
       printf("两个地址的距离%d\n", &i - &j);
       system("pause");
}

c语言基础

14.申请堆内存
#include <stdio.h> 
#include <stdlib.h>  

main(){
       //申请12个字节的堆内存 ,此函数返回堆内存的首地址 
       int* p = malloc(3 * sizeof(int));
       
       *(p + 1) = 3;
       printf("%d\n", *(p + 1));
       printf("%#x\n", p + 1);
       
       //释放堆内存 
       free(p);
       printf("%d\n", *(p + 1));
       printf("%#x\n", p + 1);
       system("pause");
}

c语言基础

15.学号管理系统
#include <stdio.h> 
#include <stdlib.h>  

main(){
       
       printf("请输入班级人数:");
       int count;
       scanf("%d", &count);
       
       int i;
       //申请堆内存保存学生学号
       int* p = malloc(count * sizeof(int)); 
       
       for(i = 1; i <= count; i++){
             printf("请输入第%d个学生的学号:", i);
             scanf("%d", (p + i - 1));
       }
       
       printf("请输入插班生人数:");
       int newCount;
       scanf("%d", &newCount);
       
       //重新申请堆内存 
       //在旧的堆内存后面扩展新的空间 
       //如果旧的堆内存后面空间都被占用了,那么就无法扩展
       //系统会另觅他处,寻觅一个足够大的堆内存区域重新申请 
       //并且会把旧堆内存的数据拷贝至新的堆内存 
       //最后释放旧的堆内存 
       p = realloc(p, (count + newCount) * sizeof(int)); 
       //输入插班生学号 
       for(i = count + 1; i <= count + newCount; i++){
             printf("请输入第%d个学生的学号:", i);
             scanf("%d", (p + i - 1));
       }
       
       for(i = 1; i <= count + newCount; i++){
             printf("第%d个学生的学号为%d\n", i, *(p + i - 1));      
       }
       system("pause");
}

c语言基础

16.多级指针
#include <stdio.h> 
#include <stdlib.h>  

main(){

       int i = 3;
       int* p = &i;
       int** q = &p;
       int*** l = &q;
       
	   printf("%d\n", &i);
       printf("%d\n", **q);
       printf("%d\n", ***l);
       system("pause");
}

c语言基础

17.结构体
#include <stdio.h> 
#include <stdlib.h>  
void study(){
     printf("熊大和光头鹏解锁了一个新姿势\n");     
}
//结构体中不能定义函数,但是可以定义函数指针 
struct student{
       int age;
       int height;
       char sex;
       
       void(*studyp)();
};
int main(){
       struct student stu = {20, 190, 'f', study};
       printf("%d\n", stu.age);
       //为了方便位移运算,结构体中各个属性的长度会被自动补齐 
       printf("%d\n", sizeof(stu));
       
       //调用函数指针
       stu.studyp(); 
       
       struct student* stup = &stu;
       (*stup).studyp();
       
       //引用这个指针指向的结构体的属性 
       stup->studyp();
       system("pause");
}

c语言基础

18.联合体
#include <stdio.h> 
#include <stdlib.h>  

main(){
       //联合体能表示这几种变量 
       union{int i; short s; char c} un;
       //联合体同时只能赋一个值,新值会覆盖旧值
       //所以长度取决于最长的变量 
       un.i = 30;
       un.s = 20;
       printf("%d\n", un.i);
       printf("%d\n", sizeof(un));
       system("pause");
}

c语言基础

19.枚举
#include <stdio.h> 
#include <stdlib.h>  

enum WeekDay
{
Monday = 10,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday
};

int main(void)
{
  //int day;
  //枚举作用:规定了变量的取值范围 
  enum WeekDay day = Thursday;
  printf("%d\n",day);
  system("pause");
  return 0;
}

c语言基础

20.自定义类型
#include <stdio.h> 
#include <stdlib.h>  

typedef int haha;
main(){
       haha i = 3;
       printf("%d\n", i);
       
       system("pause");
}

c语言基础

上一篇:for循环中暂停,async await


下一篇:使用Moco搭建mock服务