Why do some functions in C have an underscore prefix?

I recently started exploring networks in C, and I saw some functions that start with underscores like _functions () - what does that mean? I also saw this:

 struct sockaddr_in  {  

__SOCKADDR_COMMON (sin_);  

 in_port_t sin_port;    

 struct in_addr sin_addr;    

 unsigned char sin_zero[sizeof (struct sockaddr) - 

 __SOCKADDR_COMMON_SIZE -  

sizeof (in_port_t) -         

sizeof (struct in_addr)];  

};

what these parts of the code mean:

__SOCKADDR_COMMON (sin_);

unsigned char sin_zero[sizeof (struct sockaddr) - 

 __SOCKADDR_COMMON_SIZE -  

sizeof (in_port_t) -         

sizeof (struct in_addr)];
+4
source share
2 answers

The underscore prefix is ​​reserved for functions and types used by the compiler and the standard library. The standard library can use these names freely because they never conflict with the correct user programs.

The other side of this is that you are not allowed to define names starting with an underscore.

, . :

  • - , , () . :

    #ifndef _my_header_h_
    #define _my_header_h_ // wrong
    int _x; // wrong
    float _my_function(void); // wrong
    #endif
    

    :

    #ifndef my_header_h
    #define my_header_h // ok
    int x; // ok
    float my_function(void) { // ok
        int _x = 3; // ok in function
    }
    struct my_struct {
        int _x; // ok inside structure
    };
    #endif
    
  • - , , . :

    struct my_struct {
        int _Field; // Wrong!
        int __field; // Wrong!
    };
    void my_function(void) {
        int _X; // Wrong!
        int __y; // Wrong!
    }
    

    :

    struct my_struct {
        int _field; // okay
    };
    void my_function(void) {
        int _x; // okay
    }
    

, , , , .

+7

:

  • C,
  • , , .

__SOCKADDR_COMMON (2): , struct sockaddr_in, .

+2

Source: https://habr.com/ru/post/1655391/


All Articles