Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do some functions in C have an underscore prefix?

I recently started learning networking in C and I saw some functions that start with an underscore- like _function()- what does that mean exactly? 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 does this 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)];
like image 412
pedro santos Avatar asked Sep 21 '16 19:09

pedro santos


People also ask

Why are underscores used in C?

It's just an identifier, it's valid. You can use _ by itself as an identifier. that makes _ a variable of type int .

What does an underscore before a function mean?

Single Underscore In a class, names with a leading underscore indicate to other programmers that the attribute or method is intended to be be used inside that class. However, privacy is not enforced in any way. Using leading underscores for functions in a module indicates it should not be imported from somewhere else.

What does an underscore before a variable mean in C?

The underscores are often used to show that the variables are instance variables. It is not really necessary, as ivars can have the same name as their properties and their accessors.

Can a function name start with _ in C?

Function name of C follow the same naming conventions as declaring variable. It can either start with an alphabet or an underscore.


1 Answers

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

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

Well, that is the gist of the rule. The actual rule is this:

  • You cannot define any identifiers in global scope whose names begin with an underscore, because these may conflict with hidden (private) library definitions. So this is invalid in your code:

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

    But this is valid:

    #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
    
  • You cannot define any identifiers in any scope whose names begin with two underscores, or one underscore followed by a capital letter. So this is invalid:

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

    But this is okay:

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

There are actually a few more rules, just to make things complicated, but the ones above are the most often violated and the easiest to remember.

like image 122
Dietrich Epp Avatar answered Sep 21 '22 10:09

Dietrich Epp