C library source code [closed] - c

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 1 year ago.
Improve this question
I want to find the C libraries' source code to find out more about the functions used.Is GCC the only resource I can count on?I couldn't use the doxygen html version of the GCC libraries,to tell the truth it seems complex to me,for example I couldn't find the printf function's source code(Was I looking in the wrong place?).
Thanks in advance.

OTOH, in addition to glibc:
uclibc
dietlibc
BSD libc

Reading the source code is one thing. Reading a good book that includes source code is another thing entirely. And I'm not sure you can do better than The Standard C Library, by PJ Plauger. It's 20 years old, but for me it's still a page-turner.
Man, I feel old.

The GNU version is here:
Info:
http://www.gnu.org/software/libc/
Download:
http://ftp.gnu.org/gnu/glibc/

You may need to narrow down your question a bit. The implementation varies. Not everything related to implementation details (perhaps pretty much all of it - someone with more standard knowledge can chip in) is prescribed by the C/C++ standard.
In the end you may understand how a particular library decided to do it. It's still useful knowledge, but not THE answer.

The simplest and cleanest standard C library I've ever seen is Minix's standard library.
I ported it on at least 3 toolchains with virtually no effort.
I actually grew up with that library.

I couldn't find the printf function's source code
Here is the source code for this function, it seems to be calling to __vbprintf_internal, which is found glibc/stdio-common/vfprintf-internal.c. Glibc source code is also available in bootlin, I think it is more readable and convenient code.
int __printf (const char *format, ...)
{
va_list arg;
int done;
va_start (arg, format);
done = __vfprintf_internal (stdout, format, arg, 0);
va_end (arg);
return done;
}
glibc/stdio-common/printf.c

Related

Is there a "C with classes" language that is not C++? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 months ago.
Improve this question
I'm looking for some kind of C dialect that is as minimalistic as C but has built-in classes support. So I can (and encouraged to) use macros, pointers to arrays and manual memory management but also create classes, add fields and member functions to them etc. This question appeared when I tried to implement some kind of OOP in C and typedef struct and function pointers do something similar to what I want, but "member functions" require to manually pass a pointer to the object as a parameter to them, and that's not what I want to do. I know that I can just write on C++ as on "C with classes" and I would, however C++ encourages a different programming style and I'm curious if there is something that is exactly what I want.
I was searching for "C with classes" but I've only seen C++ in results, so I expect that the answer is "just use C++" and I'm OK with that, but I'm just curious.
C++ encourages a different programming style
You can write C++ in whatever style you like. Just choose not to use the features (and libraries) that don't suit your C-with-classes aesthetic.
"C with classes" was originally compiled to C by Cfront, but that's extremely dead AFAIK.
I doubt there's much demand for a resurrected Cfront when simply choosing a subset of C++, and using a current C++ compiler, already does everything you actually require.
FWIW I have written object-oriented C in the past, and manually passing this isn't that much of a burden. Even in Python you have to declare the self parameter explicitly, and nobody seems upset about that. Having to pass it in explicitly as well isn't so bad.

Making a portable C library interface: extern declaration vs function pointers [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I'm building a portable C library that needs to interact with a user-defined peripheral.
Here's an example,
My library needs to use a user-defined putc() and a getc().
To my understanding, there's two way to do this cleanly:
Using an "install" function that requires user to define interface with function pointers
// Inside mylib.h
typedef int (*mylib_port_putc)(char c);
typedef int (*mylib_port_getc)(char *c);
void mylib_install_port(mylib_port_putc, mylib_port_getc);
Using the extern keyword to let the user decide where to define the interfaces.
// Inside mylib.h
extern int mylib_port_putc(char c);
extern int mylib_port_getc(char *c);
What's the best way to do this?
I understand that "best" is difficult to define, but your opinion would be greatly appreciated.
EDIT:
I disagree with the StackOverflow maintainers to flag this question as inappropriate. I don't think this website should be limited to "how do I do X?" questions. I understand that asking for opinions will probably not generate a clear answer, but, to me, this is what mentorship looks like.
When you're starting, there's a lot of value in studying knowledgeable people debating tradeoffs.
Sorry for the rent. I know your job is hard. Happy holidays.
I think that the only correct way is the first -
you provide a library initialize(...) function for registering user callback functions - port_putc, port_getc, maybe also callbacks for you library logging, etc.
Such an approach is flexible, and very common.
In the second case, you require the library users to define 2 functions with predefined names in their code.
What happens if he didn't?

Why does ISO/IEC 9899 not standardize the definitions of the functions in the C standard library? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
ISO:IEC 9899 standardizes the prototypes of the functions of the C standard library and describes their behavior. It specifies the identifier, the return type and the parameter(s) with its matching type(s) of a certain C standard function.
But why it does not specify the definitions - (the core how the specific functions actually do work)?
Why can a C standard library function X differ in its actual source code between f.e. the gcc compiler suite on Linux (GNU C Library), clang suite on macOS and the core system dynamic libraries for Microsoft Visual C++ on Windows? Why is it dependent upon the implementation, the operation system and the relative compiler design?
Edit:
I know the question seems bad for the most of yours at the first sight but it has definitely a right to get answered, since I don´t know the reason for that yet.
I do not suggest that the ISO shall standardize the definitions because the question was closed as opinion-based - don´t get me wrong. I just ask why are things that way and want to learn from your knowledge and experience.
Take strlen for example. If the ISO C standard standardized the definition of this function, it would probably look like this:
size_t strlen(char *s)
{
size_t l = 0;
while(s[l]) l++;
return l;
}
This is highly inefficient. The GNU C library has implementations written in assembly and C that are very fast, but aren't portable.
Some functions may be impossible to standardize. For example, how would it define putchar, vfprintf, and fwrite? What about assembly functions like longjmp? Or "macros" like setjmp?
Other definitions may be exploited. For example, if the Standard C committee standardizes memcpy, two things would happen:
people can abuse the copy order, and
existing implementations would be invalidated.

Where is malloc defined in code? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
I'm learning about malloc and understand what it's used for. I'm curious if malloc is written in C. If so, I'd like to see the code that defines it. Anyone know where I can find the definition of malloc?
EDIT:
I'm aware of this link (and many like it on google) http://pubs.opengroup.org/onlinepubs/009695399/functions/malloc.html but that's not the code definition of malloc. I'm looking for the source file (if it exists) where malloc is defined. Something that looks like this
void *malloc(size_t size) {
// code for how malloc is implemented
}
I recommend you to check the GNU C library: glibc.
http://www.gnu.org/software/libc/download.html
You can read the code there. In the malloc folder.
malloc() is defined in Standard Library, as far as all unix flavors are concerned, and probably more, since Standard Library belongs to the C library. Whichever system has a C library and C API implemented, one could at least expect it to have a malloc.
Here are few more (beside above mentioned GNU) links with source code:
NET BSD malloc
OSX malloc
Some legacy unix systems (IRIX for example), beside having Standard Library malloc() also used to have a fast libmalloc implementation.
Please, also take a look at this SO post.
Code for malloc and free
https://code.woboq.org/userspace/glibc/malloc/malloc.c.html
https://fossies.org/dox/glibc-2.23/malloc_8c_source.html
Above are link for code of malloc.
http://pubs.opengroup.org/onlinepubs/009695399/functions/malloc.html
You can find many of such definitions. You should try to google it first :)

Automatic create function block diagram from ansi c code [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 8 years ago.
Improve this question
Does anyone know a tool that is able to create "function diagrams" from ansi c code?
By "function diagram" I mean a chart that represents an overview of files, functions and their relations. I imagine it to be something like a circut diagram.
Eg. if have the following code:
//MyProgram.c
int main(int argc, char** argv)
{
Foo();
Bar();
return 0;
}
//Slave.h
void Foo();
void Bar();
The chart would be something like the following picture:
Does it have an official name? Dependency diagram, perhaps?
I've looked at bit on Doxygen. But that one clearly states that:
Doxygen has built-in support to generate inheritance diagrams for C++ classes.
Same thing with many UML tools. I don't have any classes. Although my c files may come close.
What you refers is called Call Graph.
There's a list of tools to generate them: http://en.wikipedia.org/wiki/Call_graph#Software
There couple of software which are not free anymore e.g www.scitools.com which has a tool called Understand C. This I'm sure will do what you are looking for, there are other tools like BOUML ( this is free tool). Other than this the list provided by qiao is good reference.
I think I've found the origin of my idea. Displaying code as "components with connected input/output" is a LabVIEW concept. (I'm not a complete nutter for seeking out this kind diagram then ;)
However as far as I know, LabVIEW doesn't do what I want it to. LabVIEW is for building and connecting functionality. Widely used by hw/fw people. (Which are also my target audience.) It will not reverse engineer code into diagrams.
Strange that these diagrams doesn't really exists. It seems to me that people such as embedded programmers, ansi c, low level, hw etc. would love them.

Resources