#include <stdio.h>
// uninitialized vars
int main()
{
int i;
char c;
printf("uninitialized int: %d\n", i);
printf("uninitialized char: %d\n", c);
}
/*
gcc uninit.c -o uninit
./uninit
uninitialized int: 0
uninitialized char: 0
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
public class UnInit // uninitialized
{
static int i; // uninitialized fields are set to 0
static char c; // or null ('\0')
int ii;
char cc;
public static void Main()
{
// int i; // uninitialized local vars used later
// char c; // (compile error)
// access static fields in a static context, Main():
System.Console.WriteLine("uninitialized static int: " + i);
System.Console.WriteLine("uninitialized static char: " + c);
UnInit uninit = new UnInit();
// System.Console.WriteLine("uninitialized static int: " + uninit.i); // compile error
System.Console.WriteLine("uninitialized int field: " + uninit.ii);
// System.Console.WriteLine("uninitialized static char: "+new UnInit().c); // compile error
System.Console.WriteLine("uninitialized char field: " + new UnInit().cc);
}
}
/*
mcs UnInit.cs
// 4 warnings for uninitialized fields
mono UnInit.exe
uninitialized static int: 0
uninitialized static char: // null
uninitialized int field: 0
uninitialized char field: // null
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
public class Shadow // variable shadowing or name masking
{
static int i; // uninitialized fields are set to 0
public static void Main()
{ // scope of Main(), outer scope
// access static field in a static context, Main():
System.Console.WriteLine("uninitialized static int: " + i); // Shadow.i
System.Console.WriteLine("uninitialized static int: " + Shadow.i);
// int i = 1; // compile error: used before this definition (in outer scope)
{ // begin local scope
int i = 1; // shadow Shadow.i (local i not used before, in outer scope)
System.Console.WriteLine("local variable: " + i);
System.Console.WriteLine("static field: " + Shadow.i);
// System.Console.WriteLine("static field: " + new Shadow().i); // compile error
} // end local scope
} // end scope of Main(), outer scope
}
/*
mcs Shadow.cs
// 1 warning: `Shadow.i' is never assigned to, defaults to 0
mono Shadow.exe
uninitialized static int: 0
uninitialized static int: 0
local variable: 1
static field: 0
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
// file name can be different from the name of public class
public class Shadow // variable shadowing or name masking
{
static int i; // uninitialized fields are set to 0
public static void Main()
{
int i = 1; // shadow Shadow.i
System.Console.WriteLine("local variable: " + i);
System.Console.WriteLine("static field: " + Shadow.i);
// System.Console.WriteLine("static field: " + new Shadow().i); // compile error
}
}
/*
mcs SimpleShadow.cs
// 1 warning: `Shadow.i' is never assigned to, defaults to 0
mono SimpleShadow.exe
local variable: 1
static field: 0
*/