ch3-Scope

Chapter_3 Exercise_3-1







CONTENTS:     scope.c     redef.c     Scope.java




Note:  See also Scope on the blog Thinking_in_C#, Chapter_3.




scope.c     TIJ, p. 45-46


#include <stdio.h>

int main()
{
int x = 12;
printf ("out: %d\n", x);
{
x = 15;
printf ("in: %d\n", x);
}
printf ("out: %d\n", x);
}
/*
gcc scope.c -o scope
./scope
out: 12
in: 15
out: 15
*/











redef.c


#include <stdio.h>

int main()
{
int x = 12;
printf ("out: %d\n", x);
{
int x = 15; // redefine
printf ("in: %d\n", x); // new x
}
printf ("out: %d\n", x); // old x
}
/*
gcc redef.c -o redef
./redef
out: 12
in: 15
out: 12
*/











Scope.java


public class Scope
{
public static void main(String[] args)
{
int x = 12;
System.out.println("out: " + x); // + here means string concatenation
{
// int x = 15; // compile error
x = 15;
System.out.println("in: " + x);
}
System.out.println("out: " + x);
}
}
/*
javac Scope.java
java Scope
out: 12
in: 15
out: 15
*/









Chapter_3 BACK_TO_TOP Exercise_3-1



Comments

Popular posts from this blog

Contents