1. 局部变量

来源:互联网 发布:淘宝提高销量实用工具 编辑:程序博客网 时间:2024/06/08 07:29

1. 局部变量

在函数体内,或者是形参都是局部变量。
局部变量具有自动存储期限和块作用域。
#include <stdio.h>
/* 交换连个数 */
void swap(int a,int b){
    printf("a=%d,b=%d\n",a,b);
    int tmp;
    tmp = a;
    a = b;
    b = tmp;
    printf("a=%d,b=%d\n",a,b);
}
int main(int argc, const char * argv[])
{
    swap(1,2);
    return 0;
}
这里的a、b和tmp都是局部变量。
为局部变量添加static关键字,自动存储期限变为静态存储期限
/* 判断字符串中是否有元音 */
int check(char *str){
    /*只初始化一次*/
    static char temp[] = {'a','e','i','o','u'};
    int len1 = sizeof(str)/sizeof(char);
    int len2 = sizeof(temp)/sizeof(char);
    for (int i=0;i<len1;i++) {
        for (int j=0; j<len2; j++) {
            if (str[i]==temp[j]) {
                return 1;
            }
        }
    }
    return 0;

}

该博客教程视频地址:http://geek99.com/node/1023

原文出处:http://geek99.com/node/880#

0 0