Instructor: James Riely
if
void f (int x) {
int y = x + 1;
if (x > y) {
int z = y + 1;
printf ("z = %d\n", z);
}
}
y = 5*x; // Free occurrences of x and y
int y; // binding occurrence of y
int y; // Binding occurrence of y
int x; // Binding occurrence of x
x=6; // Bound occurrence of x
y = 5*x; // Bound occurrences of x and y
char f (int x);
char g (int x) {
return f (x) + f (x);
}
char f (int x) {
if (x > 0) {
return g (x >> 8);
} else {
return 1;
}
}
public class C {
static void f () {
int x = 1;
{
int y = x + 1;
{
int x = y + 1;
System.out.println ("x = " + x);
}
}
}
}
$ javac C.java
C.java:7: error: variable x is already defined in method f()
int x = y + 1;
^
1 error
public class C {
static int x = 1;
static void f () {
int y = x + 1;
{
int x = y + 1;
System.out.println ("x = " + x);
}
}
public static void main (String[] args) {
f ();
}
}
$ javac C.java
$ java C
x = 3
int main () {
int x = 1;
{
int y = x + 1;
{
int x = y + 1;
printf ("x = %d\n", x);
}
}
}
$ gcc -o scope scope.c
$ ./scope
x = 3
fun f () {
var x = 1;
{
var y = x + 1;
{
var x = y + 1;
print x;
}
}
}
f();
3
> var x = 1;
> fun f(a) { return x + a; }
> print f(10);
11
> var x = 1000;
> print f(10);
1010
x
in scope?
int main (void) {
int x = 10;
{
int x = x + 1;
printf ("x = %08x\n", x);
}
return 0;
}
$ gcc -o scope scope.c
$ gcc -Wall -o scope scope.c
scope.c: In function ‘main’:
scope.c:5:7: warning: unused variable ‘x’ [-Wunused-variable]
scope.c:7:9: warning: ‘x’ is used uninitialized in this function [-Wuninitialized]
$ ./scope
x = 00000001
x
in scope?
> var x = 1;
> fun f(a) { var x = x + 1; return x; }
[line 1] Error at 'x': Can't read local variable in its own initializer.