hi there .
i was wondering if anyone could explain me how the static keyword works.
as i understand it as explained in my books to be restricted to function only with in the actual source it is declared in. this little experiment to see how you make header files i would also be happy if someone could verify as a starting point is done right.
sorry that it is not more proff looking code but i just needed to see how it works
so it is not anything more than a hello world header experiment.'
thanks for the time.
Code:
/* experiment_main.c */
#include <stdio.h>
#include "experiment.h"
void testfunction(int one,int two);
int main(int argc, char** argv)
{
extern int x,y;
int a,b;
y=22;
a=b=55;
printf("%s\n",msg);
test_run();
testrun2();
testfunction(a,b);
testfunction(x,y);
/* question: howcome an static which my book says should be restricted
* to the source.c file it is defined in is callable from main here
* and the funktions in main ?
*/
an_int = 10;
printf(" the an_int = %d is static in another file\n",an_int);
return 0;
}
void testfunction(int one,int two)
{
printf("%d\n",min(one,two));
printf("%d\n",max(one,two));
printf("%d",equal(one,two));
printf("\n-----------------------\n");
printf("an_int is static so this should not happen \?%d",an_int);
}
experiment1.c
Code:
/* experiment1.c */
/* a function that return the min of two int*/
int min(int x,int y)
{
if(x < y)
return x;
else
return y;
}
/* a function that returns the max of two int*/
int max(int x,int y)
{
if(x > y)
return x;
else
return y;
}
/* a function that test for if two int is equal */
int equal(int x,int y)
{
if(x != y)
return 1;
else
return 0;
}
void test_run()
{
/* i have a static char msg[] declared elsewhere in the program */
char msg[]="hello world again ";
printf("%s",msg);
}
/* is not working since an_int is static which i assume to be right:
void print_int()
{
printf(" the an_int = %d is static in another file\n",an_int);
}
*/
experiment2.c
Code:
/* experiment2.c */
int x=0;
int y=0;
static char msg[]="hello world";
static int an_int=255;
int testrun2()
{
int i=0;
while(i <= an_int)
{
printf("counting i %d\n",i);
i++;
}
return 0;
}
experiment.h
Code:
/* experiment.h */
#include "experiment1.c"
#include "experiment2.c"