ch3-Scope

Chapter_3 UnInitialized







CONTENTS:     scope.c     redef.c     Scope.cs




Note:  See also Scope on the blog Thinking_in_Java, Chapter_3.




scope.c     TCS, p. 53-54


#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.cs


public class Scope
{
public static void Main()
{
int x = 12;
System.Console.WriteLine("out: " + x); // + here means string concatenation
{
x = 15; // int x = 15; // compile error
System.Console.WriteLine("in: " + x);
}
System.Console.WriteLine("out: " + x);
}
}
/*
mcs Scope.cs // compile the source file
mono Scope.exe // run program
mono ./Scope.exe
out: 12
in: 15
out: 15
*/





Notes:

Do not write the comments in the terminal window. For instance, write:
mcs Scope.cs
mono Scope.exe
You can use the standard namespace with
using System;
and then write Console.WriteLine instead of System.Console.WriteLine, but then
all the names in System become available, increasing the risk of name collision.
./Scope.exe says that Scope.exe is in the current directory('.').









Chapter_3 BACK_TO_TOP UnInitialized



Comments

Popular posts from this blog

Contents