The static function in C language

Source: Internet
Author: User

C language is organized as a file file, and in all files of a source program, an external variable and function can only be defined once in the source program. The static and extern keywords appear because different functions and variables in different files often refer to each other. The static function is mainly introduced here, which is a summary of today's compilation problem.


Have learned C + + should know, class member has three kinds of types: public,protected,private. Where the private keyword indicates that the field can only be accessed by the member function of the class and is not visible outside the class. The static keyword in C is similar to this: whenever a function with the static keyword is added, it is visible only to the file that contains the static function, and is invisible outside the file. Let me give you an example to illustrate


Here is the main function, which calls the Hello function to print the word group buf, located in the file test.c.


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main ()
{
       Char  buf[] = "Hello world!\n";
       Hello (buf);                                                         
          
       return 0;
 }
                  

The Hello function is in hello.c
#include <stdio.h>                                                           
#include <stdlib.h>
 
static void Hello (char *buf)
{     
        Write 1,buf, sizeof buf);
}    

Note that the Hello function contains the static keyword

After compiling

GCC hello.c test.c-o Test

The following error will appear

/tmp/ccu7gziz.o:in function ' main ':
test.c: (. text+0x3f): Undefined reference to ' hello '
collect2:error:ld Returned 1 exit status

Indicates that the Hello function cannot be found, and then we try to change the Hello function to a non static type. Repeat the above process to successfully generate the target code to run.

As you can see, the function containing the static keyword is visible only in the file in which it is located and is not visible in other files, resulting in no definition being found.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.