Demo on the uses of the static reserved word

This commit is contained in:
kdeng00
2020-11-04 21:20:39 -05:00
parent faa919d6ed
commit 47e0041c86
4 changed files with 86 additions and 0 deletions
+44
View File
@@ -0,0 +1,44 @@
#include <stdlib.h>
#include "number.h"
/* Variable will only be accessible from this file because
* it's static
*/
static int number = 1;
static void do_something()
{
/* Retains the value outside of the scope
* because it's an internal static variable.
*/
static int changer = 1;
number += changer;
if (changer % 10 >= 4)
{
changer += 5;
}
else if (changer % 10 < 4)
{
changer += 3;
}
else
{
changer *= 2;
}
}
int Number_get_number()
{
extern int number;
do_something();
return number;
}