C语言基础

c的基本数据类型

整形  int     4
        long    8
        short   2
浮点  float   4
        double  8
字符  char    1个字节


 signed 有符号数 最高位是符号位  可以表示的最大值相对较少 
        0-2的7次方   
 unsigned 无符号数 最高是数值位, 可以飙汗死的最大值相对较大
        0-2的8次方
signed unsigned 只能修饰 char  short int double    long

C 中没有 boolean byte   C 用0 和非0表示 false true  

输出函数

    printf("要输出的内容", 变量); 
    %d  -  int
    %ld – long int
    %lld - long long
    %hd – 短整型
    %c  - char
    %f -  float
    %lf – double
    %u – 无符号数
    %x – 十六进制输出 int 或者long int 或者short int
    %o -  八进制输出
    %s – 字符串
    24910               1100001 01001110
    12345678  10111100 01100001 01001110

    占位符的使用注意不要乱用 


    %#x   -------数值转换 ,当前是10进制转成16

指针的应用

值传递和引用传递

    共同点: 传的都是数值

    不同点:    引用传递传的是地址
            值传递传的是所对应的用户赋予的值

    注意:进行交换运算等类似运算时,要用引用传递,值传递不能改变内存中的值

实例: 交换两个int 变量的值

#include <stdio.h>
    #include <stdlib.h>

    main(){
        int x = 4;
        int y = 5;
        swap(&x,&y);
        printf("x = %d \n",x);
        printf("y = %d \n",y);

    }
    void swap(int *x , int *y){
        int temp = *x;
        *x = *y;
        *y = temp;
    }

指针所占字节

只跟操作系统和软件的位数有关

32 位操作系统,地址总线是32位
    4个字节    4*8 = 32;

64 为操作系统,地址总线是64位
    8个字节    8*8 =  64;

多级指针

    int i = 123;
    //一级指针
    int* point1 = &i; 
    //二级指针 
    int** point2 = &point1;
    //三级指针
    int*** point3 = &point2; 

实例: 主函数中获取其他函数中变量的地址

#include<stdio.h>
    #include<stdlib.h> 
    main(){
        int* pointer;
        function(&pointer);
        printf("主函数中获得的地址是:%#x\n" ,pointer);

    }
    void function(int** pointer){
        int i = 300;
        *pointer = &i;
        printf("i的地址是 %#x\n",&i);
        printf("pointer的地址是 %#x \n",*pointer);
    } 

栈内存 和 堆内存

1.栈内存:系统统一分配回收
        栈内存大小固定,内存地址是连续的

2.静态内存分配: malloc memory allocation 内存分配
        需要程序员手动释放 ,free;

    重新申请一块足够大的内存 

    come = realloc(come,sizeof(int)*(count+increment));
    如果 malloc申请到的内存后面还有足够的空间
        realloc会在malloc申请的内存空间后继续申请足够大的内存空间
    如果 malloc申请到的内存后面没有足够的空间 realloc会找到一块      
        足够大的堆内存 并且把 malloc申请到的内存中的值复制过来 

实例1:(出错的)

main(){

        int* pointer = malloc(sizeof(int)*5) ;
        *(pointer+0) = 1;
        *(pointer+1)= 2;
        *(pointer+2)= 3;
        *(pointer+3) = 4;
        *(pointer+4) = 5;
        printf("%d,%d,%d,%d \n",*(pointer+0),*(pointer+1),*(pointer+2),*(pointer+3));
        printf("%d,%d,%d,%d \n",*(pointer+0),*(pointer+1),*(pointer+2),*(pointer+3));
    }
    //第二次打印出错,因为第一次打印后系统回收了内存

实例2:(正确)

/*
        保存班级人数
        申请一块堆内存保存学生的学号
        来了几个插班生
        扩展一下堆内存
        保存插班生的学号 
        realloc re- 

    */
    #include<stdio.h>
    #include<stdlib.h>

    main(){

        printf("请输入班级的人数:");
        int count;
        scanf("%d",&count); 
        int* come = malloc(sizeof(int)*count); 
        int i;
        for(i=0;i<count;i++){
            *(come+i) = i+1;    
        }
        for(i =0;i<count;i++){
            printf("第%d个学生的学号是 20161009--%d \n",i+1,*(come+i));
        }
        printf("又来了几个学生,请输入学生数\n");

        int increment;
           //接受用户的输入 
        scanf("%d",&increment);

        come = realloc(come,sizeof(int)*(count+increment));
        for(i=0;i<count+increment;i++){
            *(come+i) = i+1;    
        }
        for(i =0;i<count+increment;i++){
            printf("第%d个学生的学号是 20161009 --%d \n",i+1,*(come+i));
        }

    }

结构体

介绍:
    c结构体 
             类似于 java 中的class 
             struct 来声明c的结构体
    结构体的大小 
                等于 
                    结构体中每一变量所占字节数的和 
    c结构体中不能定义函数
            函数指针的定义 返回(*函数指针变量名字) (返回值);

    ->   间接引用运算符    



形式: typedef struct Student{
            int i ;
            double d;
            char c;
        } stu;          //stu别名

实例:

#include<stdio.h>
    #include<stdlib.h>
    typedef struct Student{
        int age;
        int score;
        char sex;
        void(*studypointer)();          //函数指针的定义 
    } stud;   //定义一个结构体的别名,方便引用 

    void test(){
        printf("我是一个函数中打印的内容\n");
    }
    main(){

        stud stu = {18,100,'f'};
        stu.studypointer = &test;
        stu.studypointer(); 
        printf("stu.age = %d\n",stu.age);


        struct Student* stuPointer = &stu;
        printf("stuPointer.age = %d \n",(*stuPointer).age);

        printf("stuPointer->age = %d \n",stuPointer->age);

    }

联合体

特点: 所占字节取决于最大成员

        是最大成员所占字节

实例:

#include<stdio.h>
    #include<stdlib.h>
    union u{
        int num;      
        double d; 
        char c;  
    }
    main(){

        union u u1= {1,1.1234,'c'};
        printf("union 占的字节数 %d",sizeof(u1));
    }

枚举

介绍:
        自己定义 ,系统自动赋值,表示一组代表特殊意义的值
        例如 ;  性别,星期,年,月

实例:

#include<stdio.h>
    #include<stdlib.h>
    enum sex{
        MALE,FEMALE
    };

    main(){
        enum sex s = MALE;
        printf("MALE = %d \n",MALE);
        printf("FEMALE = %d \n",FEMALE);
    }

自定义数据类型

介绍:
就是给原有的数据类型,进行包装,然后起别名

实例:

#include<stdio.h>
    #include<stdlib.h>
    typedef int i ;   
    main(){

        i j = 123;
        printf("i = %d \n",j); 

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值