Home > Mobile >  If all variable / function names in C must be unique, then how is there no clash in standard library
If all variable / function names in C must be unique, then how is there no clash in standard library

Time:10-30

I cannot understand this about C. Since every variable and function has to be unique (since they are global) then how can this be possible with a huge library like the standard C library and 3rd party libraries? Yes you could make every variable "static" but that does not seem to be the case.

For example I was looking and I found this random_data struct stdlib.h and I am wondering - what are the chances that another library also has a struct called random_data? Won't that be a clash?

stdlib.h

struct random_data {
  int32_t *fptr;
  int32_t *rptr;
  ...
};

I mean what if both stdlib and stdio have a struct called random_data and I was to include both libraries in my program? The standard library has thousands of functions and variables and it seems impossible that they all would be unique. Also what about 3rd party libraries? What if a 3rd party library also has a struct called random_data?

#include <stdio.h>
#include <stdlib.h>

int main() {

}

CodePudding user response:

The C standard library was designed specifically so that there are not any clashes. You are right, that's kinda delicate, that's why C introduced the concept of namespaces (So that you can have a variable called cout without it clashing with std::cout).

As for a more general case, a simple way to make a library in C while making sure no name collision ever happen is simply by using a naming convention where you prefix all your variables and functions with a particular word pertinent to the library.

For example most SDL function are prefixed by SDL_ like SDL_CreateThread. Similarly OpenGL prefixes most functions with gl like glViewport(), and so on. Naming conventions for C are a whole topic, and they are the way people make sure collisions do not become a problem in general.

Edit: Some more examples and extra miscellaneous info

This even occurs in the standard library (Or at least the POSIX standard). All function to come out of the pthread.h header are prefixed by pthread_ like pthread_create().

And you get the points, naming conventions in c are kinda important. You might have come across some other interesting naming conventions. for example, there are a lot of types in c that end in _t like size_t which are a way to make it explicit that said type comes from a typedef and it is NOT a primitive of the language. The Window's win32 api is filled with it's own very complex naming convention called the Hungarian Convention in which you basically have to specify the type of every function and variable directly in it's name, for example pszWindowTitle is prefixed by psz where the p stands for "pointer" and the s means is pointing to a "string", and the z means the string is null terminated, meaning it ends with a "zero".

CodePudding user response:

Not everything in your library will be kept in its header file for the reason you've thought of. I was experimenting with the example of the struct you gave, and the following program compiles without error.

#include <stdio.h>
#include <stdlib.h>

struct random_data {
    int32_t *fptr;
    int32_t *rptr;
};

int main(){
    printf("Hello, world\n");
}

Most likely the struct random_data isn't in the header file although it might be present in the .c source file.

However, note that name collision does cause issue. This example won't compile.

#include <stdio.h>

char *getchar(void);

int main(){
    printf("Hello, world\n");
}
  •  Tags:  
  • c
  • Related