C语言 局部变量

ChatGPT 3.5 国内中文镜像站免费使用啦

零基础 C/C++ 学习路线推荐 : C/C++ 学习目录 >> C 语言基础入门


一.局部变量简介

定义在函数内部的变量称为局部变量(Local Variable),它的作用域仅限于函数内部, 离开该函数后就是无效的,再使用就会报错。


/******************************************************************************************/
//@Author:猿说编程
//@Blog(个人博客地址): www.codersrc.com
//@File:C语言教程 - C语言局部变量
//@Time:2021/07/18 07:30
//@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
/******************************************************************************************/ 

int f1(int a){
    int b,c;  //a,b,c仅在函数f1()内有效
    return a+b+c;
}
int main(){
    int m,n;  //m,n仅在函数main()内有效
    return 0;
}

二.局部变量注意事项


1.局部变量只能在函数内部使用,离开该函数后就是无效的,再使用就会报错。

/******************************************************************************************/
//@Author:猿说编程
//@Blog(个人博客地址): www.codersrc.com
//@File:C语言教程 - C语言局部变量
//@Time:2021/07/18 07:30
//@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
/******************************************************************************************/ 

#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);
      |                               ^


*/

2.不同函数中的相同局部变量互不影响

/******************************************************************************************/
//@Author:猿说编程
//@Blog(个人博客地址): www.codersrc.com
//@File:C语言教程 - C语言局部变量
//@Time:2021/07/18 07:30
//@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
/******************************************************************************************/ 

#include <stdio.h>

int func(){
    int b = 10;
    int c = 20;  //a,b,c仅在函数f1()内有效
    printf("函数:%s  中 b = %d   c = %d \n",__FUNCTION__,b,c); 
    return b+c;
}
int main(){
    int b = 5;
    int c = 6;  //b,c仅在函数main()内有效
    func();
    printf("函数:%s 中  b = %d    c = %d \n",__FUNCTION__,b,c); 
	
    return 0;
}

/*
输出:
函数:func  中 b = 10   c = 20 
函数:main 中  b = 5    c = 6 
*/

三.猜你喜欢

  1. C语言 数组下标越界和内存溢出区别
  2. C语言 使用指针遍历数组
  3. C语言 指针和数组区别
  4. C语言 指针数组和数组指针区别
  5. C语言 野指针
  6. C语言 函数值传递和址传递
  7. C语言 函数不定长参数
  8. C语言 函数指针
  9. C语言 指针函数
  10. C语言 回调函数 callback
  11. C语言 #pragma once
  12. C语言 #include <> 与 #include “” 区别
  13. C语言 const 修饰函数参数
  14. C语言 const 和 define 区别
  15. C语言 #运算符
  16. C语言 ##运算符
  17. C语言 __VA_ARGS__
  18. C语言 ##__VA_ARGS__
  19. C语言 函数不定长参数 ##__VA_ARGS__经典案例
  20. C语言 va_start 宏
  21. C语言 va_end 宏
  22. C语言 va_arg 宏
  23. C语言 vprintf 函数
  24. C语言 va_start / va_end / va_arg 自定义 printf 函数
  25. C语言 main 函数
  26. C语言 main 函数参数 main(int argc, char *argv[])
  27. C语言 exit 函数
  28. C语言 abort 函数
  29. C语言 assert 函数
  30. C语言 局部变量

ChatGPT 3.5 国内中文镜像站免费使用啦
© 版权声明
THE END
喜欢就支持一下吧
点赞1 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容