Presentation is loading. Please wait.

Presentation is loading. Please wait.

Scope (visibility) scope in C is simple:

Similar presentations


Presentation on theme: "Scope (visibility) scope in C is simple:"— Presentation transcript:

1 Scope (visibility) scope in C is simple:
normal declaration inside a procedure is visible only within that procedure normal declaration outside a procedure is visible to all code below that point in that source file external declaration (extern) informs the linker of a reference to a non-static/non-local variable or function in another source file

2 Scope (visibility) scope in assembly is similar: unless otherwise specified, symbols are visible only within the source file (thus, you can't use "loop" as a label twice within the same source file, but each separate source file can use "loop" as a local label)

3 Scope (visibility) scope in assembly is similar:
for external references (i.e., between source files), a pseudo-op is used in one source file to indicate the global availability of a symbol defined in that file (i.e., exporting the symbol) and another pseudo-op is used in another source file to indicate use of that symbol (i.e., importing that symbol); up to the linker to match the uses with the definitions

4 Scope (visibility) In MASM, the two pseudo-ops PUBLIC and EXTERN are used for these purposes In ARM assembly, .global is used both in the exporting file and in the importing file

5 Scope (visibility) Using MASM, the two pseudo-ops: -- example correspondint C statements -- define(EXTERN, global) define(PUBLIC, global) file a: .EXTERN sqrt file d: extern double sqrt(double); /* usually omit "extern" here */ /* "static" means not public */ .extern x extern int x; /* "referencing" */ /* declaration */ .section ".data" w: .word 0 int w; .section ".text" bl sqrt z = sqrt (y); ldr r1, =x w = x; ldr r2, [r1] ldr r1, =w ... str r2, [r1]

6 Scope (visibility) file b: .global sqrt file e: double sqrt(double argument) { sqrt: ... … } ... file c: .global x file f: int x; /* "defining" decl. */ .section ".data" x: .word 0

7 Scope (visibility) Many C compilers/linkers assume that global variables with the same name in multiple "defining declarations" (i.e., no extern keywords) are identical. The exception is when more than one of these identically-named variables is initialized.

8 Scope (visibility) -- file global_a_part_1.c file global_a_part_2.c -- #include<stdio.h> #include<stdio.h> int a; int a; void subr( void ); void subr() { a = 2; int main() { printf("a in subr is %d\n",a); a = 1; } printf("a in main is %d\n",a); subr(); return 0; } gcc global_a_part_1.c global_a_part_2.c ./a.out Output: a in main is 1 a in subr is 2 a in main is 2

9 Scope (visibility) C has static scoping, but there are languages with dynamic scoping, such as Perl, in which the variable names have to be resolved at run time based on the current nesting level or call chain Consider the examples on the next three slides.

10 Scope (visibility) in C
cat scope_test.c #include<stdio.h> char str[] = "Global!"; void print_it() { printf("%s\n",str); } int main() { { char str[] = "Local!"; print_it();

11 Scope (visibility) gcc scope_test.c ./a.out Global!

12 Scope (visibility) in Perl
cat scope_test.pl $str = "Global!\n"; sub print_it { print $str; } { local $str = "Local!\n"; print_it();p print_it(); perl scope_test.pl Local! Global! [adapted from


Download ppt "Scope (visibility) scope in C is simple:"

Similar presentations


Ads by Google