#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
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#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
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
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
*/
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 ('.').