zoukankan      html  css  js  c++  java
  • typedef , static和 extern

    typedef 

    1.作用:给已经存在的类型起一个新的名称

     2.使用场合:

     1> 基本数据类型

     2> 指针

     3> 结构体

     4> 枚举

     5> 指向函数的指针

    #include <stdio.h>
    
    typedef int MyInt;
    typedef MyInt MyInt2;
    
    // 给指针类型char *起一个新的类型名称String
    typedef char * String;
    
    /*
    struct Student
    {
        int age;
    };
    typedef struct Student MyStu;
    */
    
    /*
    typedef  struct Student
    {
        int age;
    } MyStu;
    */
    
    
    typedef struct
    {
        int age;
    } MyStu;
    
    /*
    枚举型 enum Sex {Man, Woman}; typedef enum Sex MySex; */ typedef enum { Man, Woman } MySex; typedef int (*MyPoint)(int, int); int minus(int a, int b) { return a - b; } int sum(int a, int b) { return a + b; } /* struct Person { int age; }; typedef struct Person * PersonPoint; */ typedef struct Person { int age; } * PersonPoint; int main() { // 定义结构体变量 struct Person p = {20}; PersonPoint p2 = &p; //struct Person *p2 = &p; //MyPoint p = sum; //MyPoint p2 = minus; //int (*p)(int, int) = sum; //int (*p2)(int, int) = minus; //p(10, 11); //MySex s = Man; //enum Sex s = Man; //enum Sex s2 = Woman; // struct Student stu3; //MyStu stu = {20}; //MyStu stu2= {21}; return 0; } void test2() { String name = "jack"; printf("%s ", name); } void test() { int a; MyInt i = 10; MyInt2 c = 20; MyInt b1, b2; printf("c is %d ", c); }

    static

    1.作用:

    static修饰局部变量的使用场合:

     1.如果某个函数的调用频率特别高

     2.这个函数内部的某个变量值是固定不变的

    void test()
    {
        static double pi = 3.14;
        
        double zc = 2 * pi * 10;
        
        int a = 0;
        a++;
        printf("a的值是%d
    ", a); // 1
        
        /*
         static修饰局部变量:
         1> 延长局部变量的生命周期:程序结束的时候,局部变量才会被销毁
         2> 并没有改变局部变量的作用域
         3> 所有的test函数都共享着一个变量b
         */
        static int b = 0;
        b++;
        printf("b的值是%d
    ", b); // 3
    }
    
    int main()
    {
        for (int i = 0; i<100; i++) {
            test();
        }
        
        
        test();
        
        test();
        
        test();
        
        
        return 0;
    }
    

    全局变量分2种:

     外部变量:定义的变量能被本文件和其他文件访问

     1> 默认情况下,所有的全局变量都是外部变量

     1> 不同文件中的同名外部变量,都代表着同一个变量

     内部变量:定义的变量只能被本文件访问,不能被其他文件访问

     1> 不同文件中的同名内部变量,互不影响

     static对变量的作用:

     定义一个内部变量

     extern对变量的作用:

     声明一个外部变量

     static对函数的作用:

     定义和声明一个内部函数

     extern对函数的作用:

     定义和声明一个外部函数(可以省略)

  • 相关阅读:
    sencha touch 扩展篇之将sencha touch打包成安装程序(上)- 使用sencha cmd打包安装程序
    sencha touch 扩展篇之使用sass自定义主题样式 (下)通过css修改官方组件样式以及自定义图标
    一个不错的android组件的网站
    sencha touch 扩展篇之使用sass自定义主题样式 (上)使用官方的api修改主题样式
    sencha touch 入门系列 (九) sencha touch 布局layout
    面试题总结
    国外接活网站Elance, Freelancer和ScriptLance的介绍和对比
    sencha touch 入门系列 扩展篇之sencha touch 项目打包压缩
    Android Design Support Library——Navigation View
    设计模式——命令模式
  • 原文地址:https://www.cnblogs.com/dj123456/p/4762731.html
Copyright © 2011-2022 走看看