c语言中的局部变量怎么定义
更新:HHH   时间:2023-1-7


这篇“c语言中的局部变量怎么定义”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“c语言中的局部变量怎么定义”文章吧。

1、函数内部定义的变量称为局部变量,其作用域仅限于函数内部,离开函数后无效,使用后报错。

2、局部变量只能在函数内部使用,离开函数后无效,再次使用会报错。

实例

#include <stdio.h>
 
int f1(int a){
    int b,c;  //a,b,c仅在函数f1()内有效
    return a+b+c;
}
int main(){
    int m = 5;
    int n = 6;  //m,n仅在函数main()内有效
    printf(" m = %d n = %d \n",m,n);
 
    printf(" b = %d c = %d \n",b,c); //b,c 在函数中并未声明
 
    return 0;
}
 
/*
输出:
main.cpp: In function ‘int main()’:
main.cpp:11:29: error: ‘b’ was not declared in this scope
   11 |  printf(" b = %d c = %d \n",b,c);
      |                             ^
main.cpp:11:31: error: ‘c’ was not declared in this scope
   11 |  printf(" b = %d c = %d \n",b,c);
      |                               ^
 
 
*/

以上就是关于“c语言中的局部变量怎么定义”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注天达云行业资讯频道。

返回大数据教程...