1 / 15

变量的作用域

变量的作用域. 例 void f1( ) { int t=2; a *= t; b /= t; } void main( ) { int a, b; printf( ” Enter a,b: ” ); scanf( ” %d,%d ” , &a, &b); f1( ); /* 调用函数 f1( ) */ printf ( ” a=%d,b=%d ” , a, b); }. 编译程序会提示出错 : Undefined symbol ‘a’ 和 Undefined symbol ‘b’ 。 为什么 ?.

allene
Download Presentation

变量的作用域

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 变量的作用域 • 例 • void f1( ) • { int t=2; a *= t; b /= t; • } • void main( ) • { int a, b; • printf(”Enter a,b:”); • scanf(”%d,%d”, &a, &b); • f1( ); /* 调用函数f1( ) */ • printf (”a=%d,b=%d ”, a, b); • } 编译程序会提示出错: Undefined symbol ‘a’ 和 Undefined symbol ‘b’ 。 为什么?

  2. 变量的生命 { 我的生命开始了 我在这里生存,这是我的作用范围 } 我的生命结束了

  3. 变量的长命者—全局变量 int global; { 我的生命不会结束,除非程序结束 }

  4. 全局变量的作用范围一 int global; 整个工程 .c .c .c .c .c .c .c .c .c .c .c .c .C .C

  5. 全局变量的作用范围二 static int global; 单个文件 .C

  6. 所有分配在全局数据区的变量,只随程序的消亡而消亡,而存储于堆栈中的变量,出了其所在的括弧范围既亡 变量 生命期的总结 int global;//工程全局变量,存储于全局数据区 static int myglobal; //静态全局变量, 存储于全局数据区 void main(void) { int iarea = 100; //局部变量,存储于堆栈区 { int iarea = 10; //局部变量,存储于堆栈区 int iarea1 = 1000 ; //局部变量,存储于堆栈区 } } void show() { int iarea; //局部变量,存储于堆栈区 static int siarea; //静态局部变量,存储于全局数据区 }

  7. 练习 int a=10; fun(int i) { a+=2*i; return a;} main() { int a=10; printf("\n%d,%d",fun(a),a); printf("\n%d,%d",fun(a),a); } 运行结果: 20,10 40,10

  8. int a=1;/在s.c文件定义了全局变量a*/ void fun() { … /*在函数fun中引用变量a不必声明*/ } /*源文件s.c*/ void main() { … /*在函数main中引用变量a不必声明*/ } extern int a; /*声明变量a为外部型变量*,不会分配空间*/ void fac(int n) { … a=a*n; /*在函数fac外已声明a为外部变量,函数fac可以引用a*/ …} /*源文件d.c*/

  9. 预处理命令 • 预处理命令不是C语言本身的组成部分,必须在正式编译前进行处理。 • C提供的预处理功能主要有一下3种: 1、宏定义 2、文件包含 3、条件编译

  10. 宏定义的作用: • 宏定义后, 该程序中宏名就代表了该字符串。 例7-1 定义宏PRICE,宏名PRICE代表某商品单价,根据输入的数据求总值。 #include <stdio.h> #define PRICE 500 void main() { float x,y; scanf("%f",&x); y=PRICE*x; printf("The value is %f\n",y); } (1)使用宏名代替一个字符串,可以减少程序中重复书写某些字符串的工作量,增加程序的可读性,而且用宏名代替不易出错。 (2)编译预处理时,将程序中PRICE用500代替,与宏调用的过程相反,这种将宏名替换成字符串的过程称为“宏展开”。

  11. 宏就是Ctrl_C Ctrl_V 函数调用 使用宏 程序运行时 程序编译前 #define max(a,b) (a>b)? a: b; void main() { int ibig; ibig = max(10,30) …… } int max(int a, int b) { int big; big = (a>b)? a:b; return big; } void main() { ….. int ibig; ibig = max(10,30); …… } max 函数 void main() { int ibig; ibig = (10>30)? 10:30; …… }

  12. 文件包含处理 • “文件包含”处理是指一个源文件可以将另外一个源文件的全部包含进来。 • 一般形式 #include"文件名" 或 #include<文件名>

  13. 文件包含的含义 file1.c file2.c file1.c #include<file2.c> B B A A

  14. “文件包含”的实质 • 被包含的文件与其所在的文件,在预编译后已经成为同一个文件(而不是两个文件)

  15. 条件编译 • 一般情况下,源程序中所有的行都参加编译。但如果对其中一部分内容只在满足一定条件才进行编译,这就是“条件编译”。

More Related