Thread: static keyword and headers. help me understand.

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    53

    static keyword and headers. help me understand.

    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"

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You are including the actual .c file, not compiling and linking them. Basically, you're copy-paste-ing the entire file into your experiment_main.c file. That's why you can call them.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    53
    thanks for the answer.

    so what i am doing is wrong if it is supposed to be a header filer for two or more source files that is seperated into functions and declarations and is to be used as a header for my tutorials i follow.


    i was thinking that i wanted to make a library with all the functions from tutorials seperated as .c file for each tutorial and one for common file handling rutines so when i work my way trough tutorials i can use them and build more on it as i go along but that is a wrong way of doing it then i guess.

    this explains why there are actual small functions in one of the .c files as i started to create the project but i am just not sure if i get it right. i just dont want to waste a lot of time doing something wrong.

    it should be compiled and linked by using gcc ./ main.c alone in this context.
    otherwise then i should compile it first as gcc file1.c file2.c file.h
    and then when it should be used just include the file.h or is that also not right to do it this way.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    /* myprog.c */
    #include "myheader.h"
    ...
    int main( void )
    {
        myfunction( );
        mystaticfunction( );
        return 0;
    }
    
    /* myheader */
    void myfunction( void );
    static void mystaticfunction( void );
    
    /* myfile2 */
    static void mystaticfunction( void )
    {
        printf( "my static function\n" );
    }
    
    void myfunction( void )
    {
        printf( "my function: " );
        mystaticfunction( );
    }
    
    /* compile it... */
    gcc -o myprog myprog.c myfile2.c -Wall -pedantic
    Now, you shouldn't be able to make that red line in your first function, because the function is static, and local to myfile2. However, if you comment that out (in main) and compile again, you should see that it is being called by the function you're allowed to call.

    Other functions in the same file can call it, but you can't make it available to other files directly.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    53
    thanks so much for this example .
    i get it now. i also found a instruction in how to create a library so i will try make a small library for the tutorials i follow later on tonight.

    thanks alot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM