C语言Android开发经验谈C语言

2675字带你进阶C语言中的关键字

2017-12-03  本文已影响28人  PcDack

C语言进阶之C语言关键字

关键字总结.png

基本数据类型

1.数据类型

数据类型的本质就是固定字节别名

2.变量

auto,register,static分析(属性关键字)

auto(在程序的栈上面)

最佳示例

写一个简单的打印数字的程序

#include<stdio.h>
int main(){
        auto  int a =10;
          printf("%d",a);
}

你会发现auto 似乎并没有什么软用

static(在内存的静态区里面的变量)

最佳实例一

新建两个.c文件,分别为test.ctest2.c

test.c

#include<stdio.h>
extern int test2_g;
int main()
{
    printf("%d",test2_g);
    return 0;
}

test2.c

static int test2_g=1;

我们尝试用用gcc编译两个.c文件,会报一个未定义应用的错误。如果,我们把test2.c中的static去掉,程序就和我们预想的结果相同。也就是说static让变量test2_g私有化了。在C语言面向对象编程中,可以完全将它作为private来用。

如果我们需要访问这个变量,也是有办法的。正如其他面向对象语言中一样,我们可以写一个get方法来获取它的值。我们修改test2.c这个文件。

static int test2_g=1;
int get_test2_g()
{
    return test2_g;
}

再修改test.c这个文件

#include<stdio.h>
extern int  get_test2_g();
int main()
{
    printf("%d", get_test2_g());
    return 0;
}

两句话总结static

  1. static存在内存的静态存储区,有着和程序一样长的生命周期。
  2. static修饰的函数作用域只是申明的文件中(也就是说只能在一个C文件中)。相当于java中的private

register(用在非常讲究性能的实时系统中,通常嵌入式工程师才需要)

注意:

register变量得必须是CPU寄存器可以接受的值
不能用&运算符获取register变量的地址

总结

  1. auto变量存储在程序的栈中,默认属性只能是局部变量
  2. static变量存储在程序的静态区
  3. register变量存储请求存储于CPU寄存器中只能是局部变量

if,switch,do,while,for分析

分支语句

bool数据类型

C语言中默认是没有bool类型(标准为C99),实际项目开发又十分需要bool类型我们可以自己创建一种新的数据类型。

typdef enum _bool{
    FALSE,
    TRUE
}BOOL;

使用

BOOL a;
if(a)
{
}

if注意点

switch注意事项

分支语句的总结

循环语句

switch能否用continue?
不能

do和break的妙用(一般程序设计分析)

do{
    //todo
}while(0)//作为一种代码块的作用
free(p);

goto,void,extern,sizeof分析

goto

void的意义

表示无

void指针的意义

extern中隐藏的意义

sizeof

const和valatile分析

const(是可以修改的)

const修饰指针

const int* p; //p可变,p指向的内容不可变
int const* p;//p可变,p指向的内容不可变
int* const p;//p不可变,p指向的内容可变
const int* const p;//p和p指向的内容都不可变

最佳实例

**口诀:左数右指 **
指针符号在const左边的时候指针所指向的数字不能发生改变,右边的时候

左数
void main()
{
    int i=0;
     int const * p=&i;
    * p=3;
}
右指
#include<stdio.h>
void main()
{
    int i=0;
     int * const p=&i;
    * p=3;
    p=NULL;
}

const修饰函数参数和返回值

#include<stdio.h>
const int * func()
{
    int i=0;
    i++;
    return & i;
}
int main()
{
    const int * q=func();
    printf("%d\n",*q);
}

valatile

struct和union分析

由结构体产生柔性数组

柔性数组实例

struct SoftArray{
    int len;
    int array[];
};

柔性数组Dome

#include<stdio.h>
#include<malloc.h>
typedef struct _SoftArray{
    int len;
    int array[];
}SoftArray;
int main()
{
    int len=10,i=0;
    SoftArray *p=(SoftArray*)malloc(sizeof(SoftArray)+sizeof(int)*len);
    p->len=len;
    for(i=0;i<len;i++)
    {
        p->array[i]=i+1;
    }
    for(i=0;i<len;i++)
    {
        printf("%d\n",p->array[i]);
    }
    
    return 0;
}

使用柔性数组创建斐波那契数列

#include<stdio.h>
#include<malloc.h>
typedef struct _softArray{
  int len;
  int array[];
}SoftArray;
SoftArray * creat_Array(int size)
{
    SoftArray *p=NULL;
    if(size>0)
    {
    p=(SoftArray*)malloc(sizeof(SoftArray)+sizeof(int)*size);
    p->len=size;
    }
    return p;
}
void del_Array(SoftArray * p)
{
    free(p);
}
void create_Fibonacci(SoftArray* p)
{
    int i=0;
    if(NULL!=p)
    {
        if(1==p->len)
        {
            p->array[0]=1;
        }else
        {
            p->array[0]=1;
            p->array[1]=1;
            if(p->len>2)

            {
                for(i=2;i<p->len;i++)

                {
                    p->array[i]=p->array[i-1]+p->array[i-2];
                    }
                }
        }

    }
}
int main()
{
    int i=0;
    SoftArray * my=creat_Array(10);
    create_Fibonacci(my);
    for(i=0;i<10;i++)
    {
        printf("%d\n",my->array[i]);
    }
    
    del_Array(my);
    
    return 0;
}

记得释放数组(和JavaSE不同)

union和struct的区别

区别的最佳实例

#include<stdio.h>
#include<malloc.h>
struct A{
    int a;
    int b;
};
union B{
    int a;
    int b;
};
int main()
{
    printf("struct is %d\n",sizeof(struct A));//结果为8
    printf("union is %d\n",sizeof(union B));//结果为4
    return 0;
}

枚举类型

enum Color{
    GREEN,
    RED=2,
    BLUE
};

enum 默认的第一个是0,也就是说上面代码段中GREEN为0,BLUE为3

枚举类型与#define的区别

#include <stdio.h>
#define YELLOW 1
int main() {
    enum Color{
        GREEN,
        RED
    };
    if (YELLOW ==GREEN){

    }
    return 0;
}

我们用gcc -E 处理一下这个程序,然后会发现原本的程序变成

....
int main() {
    enum Color{
        GREEN,
        RED
    };
    if (1 ==GREEN){

    }
    return 0;
}



区别全在程序里,程序本身没有意义

typedef

上一篇 下一篇

猜你喜欢

热点阅读