-3

Is it possible to add my own functions to a C existing library? For example, I want to add this function:

#include <stdio.h>
int main()
{
    int prices[5] = { 1, 2, 3, 4, 5 };
    int size = sizeof prices / sizeof prices[0];
    return size;
}

to the stdio.h library.

Is it possible? If yes, how can I do that?

Daniel
  • 5
  • 1
  • 6
  • 3
    Maybe, but it's a bad idea. What problem are you trying to solve? – HolyBlackCat Apr 26 '21 at 11:40
  • 2
    Total X/Y question. What, you want to add `main()` to a library? or you want a reusable way to get the number of elements in an array? then just use a macro...? – underscore_d Apr 26 '21 at 11:41
  • I'm trying to make a function that finds the array length (similar to the ```strlen``` function but for arrays). I want to call this function ```arrlen``` and to add it to the `````` library but IDK how to do that. – Daniel Apr 26 '21 at 11:43
  • 2
    No, you don't *really* want that... You should either use another library that already provides this, or just declare/define your own macro (since a function cannot work for this) and `#include` that. – underscore_d Apr 26 '21 at 11:45
  • 1
    I would now instead vote to close as a dupe of [Is there a standard function in C that would return the length of an array?](https://stackoverflow.com/questions/1598773/is-there-a-standard-function-in-c-that-would-return-the-length-of-an-array) – underscore_d Apr 26 '21 at 11:45
  • 5
    Why would you want to add it to ``? Use your own header file instead. – Ian Abbott Apr 26 '21 at 11:46
  • Is there another library that provides this function? And why don't I really want that? – Daniel Apr 26 '21 at 11:46
  • @Daniel [How do I determine the size of my array in C?](https://stackoverflow.com/questions/37538/how-do-i-determine-the-size-of-my-array-in-c "How do I determine the size of my array in ?"). See 2nd answer. – alex01011 Apr 26 '21 at 11:49
  • I want to add it to `````` because I want to help people to find an array length. – Daniel Apr 26 '21 at 11:49
  • 5
    It won't be helpful to put an array length calculator to a library whose only purpose is to read from and write to terminals and files. – mouviciel Apr 26 '21 at 11:58
  • 1
    Adding miscellaneous extra stuff to standard header files doesn't help people. It hinders the creation of portable code. – Ian Abbott Apr 26 '21 at 12:01
  • 2
    You can't provide this as a function because you can't pass an array to a function, only a pointer to an array. strlen works because it looks for the 0 at the end. – stark Apr 26 '21 at 12:09
  • 3
    Apart from the array length calculation not being implementable as a function, the functions declared in `stdio.h` are part of the C standard. No one will appreciate (to put it mildly) that you publish a version with your own additions. This has to go through the official standardization work. You are free to publish your own utilities, but keep them out of the standard library. – nielsen Apr 26 '21 at 12:10
  • 1
    It is legal that an implementation does not have a (editable) file as basis for ``. It's perfectly ok for a compiler to *just know* what to do when you `#include `. – pmg Apr 26 '21 at 12:20

3 Answers3

3

In general, it is possible to create your own library that contains all of another library plus routines you add, and it is possible to install that library in your system in place of the original library.

You should not do this.

Generally, when one wants to provide a function, one writes their own library and publishes it as a separate product.

stdio.h is not a library. It is a header file. It declares objects and functions that are defined elsewhere, and it may declare related things such as macros and type names. The term “library” is generally used either to refer to the associated packaged object modules or to the entire product that is the software library: The object modules, the header files, and their documentation.

In C, a function cannot calculate the length of an array because it is not possible to pass an array to a function. When an array is used as an argument to a function, the compiler automatically converts it to a pointer to the first element of the array. This pointer does not have any information about the size of the array. For example this function:

size_t foo(int array[])
{
    return sizeof array / sizeof array[0];
}

will return the size of a pointer to an int divided by the size of an int. It will return this same value for all arrays passed to it of any size, because its int array[] parameter is automatically adjusted to be int *array.

One can define a macro to calculate the size of an array:

#define NumberOf(a) (sizeof (a) / sizeof *(a))

This works as a macro because macros are different from functions. The macro argument is not passed like a function argument, and the sizeof operator uses its argument directly—the conversion of an array to a pointer is not performed for a sizeof operand.

This macro could be published in a header file, although it is sufficiently simple that it is usually merely defined in source files where it is used rather than provided in a header file. If you did publish it in a header file, you should publish it in your own header file. You should not modify standard C header files until you are creating your own C implementation.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
0

It is possible at your local level:

It will be highy unlikely that your update someday appears in any public distribution (e.g. MacOS or Ubuntu): As you can see in the comments, the community of developers is not yet ready to welcome such a change.

A better way to help people is to create your own library and spread it to the World.

mouviciel
  • 66,855
  • 13
  • 106
  • 140
0

to the stdio.h library.

stdio.h is not the library only a header file.

Is it possible to add my own functions to a C existing library?

Of course, it is, but it does not make too much sense. Simply create your own library.

Changing the standard library is a very bad idea unless you know what are you doing (read: you are a very experienced programmer) and you port it to another target or work on your own implementation.

I want to add this function: int main()

Adding main function to the library is a very bad idea.

0___________
  • 60,014
  • 4
  • 34
  • 74