In the next tutorial we will use what we learned in tutorial 16 to drag
a file onto our program, encrypt the data, create a new (encrypted) file,
and delete the original file. And then we'll decrypt the file returning
the data back to it's original format.
First let's look at another way to manipulate bytes.
Normally when you write a function, the variables are created on the "stack".
"Stack" is a term for the local memory they are created in. As soon as the
function exits they go away.
Consider this..
int Value( void )
{
int v = 5;
v++;
return v;
}
the above function will return the value of 6 every time it is called
void main( void )
{
int myVal = Value();
printf( "%d, ", myVal );
myVal = Value();
printf( "%d, ", myVal );
myVal = Value();
printf( "%d", myVal );
}
the above program will output:
6, 6, 6
There are certain time when you want a variable to maintain its state
whether the function is called or not. This word is "static". "Static"
means the variable is initialized once and then kept (even when the function goes away).
If we do
int Value( void )
{
static int v = 5;
v++;
return v;
}
Here v is initialized and set to 5 just one time. Everytime the function
is called, the v++ will happen and v will continue to increment.
now if we do
void main( void )
{
int myVal = Value();
printf( "%d, ", myVal );
myVal = Value();
printf( "%d, ", myVal );
myVal = Value();
printf( "%d", myVal );
}
it will output:
6, 7, 8
remember, the "static" keyword tells the compiler to keep this
variable around and remember the last used value in it.
__________________________________________________________
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <conio.h>
int Value( void )
{
static int v = 5;
v++;
return v;
}
void main( void )
{
int i;
int myVal = Value();
char name[5] = "aaaa";
char name2[5] = "dave";
printf("\n aaaa is now : ");
for (i = 0; name[i]!=0; i++)
{
name[i] += myVal = Value();
printf("%c",name[i]);
}
printf("\n");
printf("\n dave is now : ");
for (i = 0; name2[i]!=0; i++)
{
name2[i] += myVal = Value();
printf("%c",name2[i]);
}
getch();
}