How to find definition of library functions in C [duplicate] - c

This question already has answers here:
Where are the functions in the C standard library defined?
(5 answers)
Closed 7 years ago.
Functions like printf() , scanf() , memset() , puts() etc have their declaration in header files but is there any mechanism to see the definition of these function..?
This might not be a new question but i could not find the appropriate solution for this.

Find your compilers include path (e.g. GCC solution)
Search for the header you are interested in (e.g. printf should be in stdio.h or more likely another header included by stdio.h)
Correctly configured, some IDEs will help you with that, e.g. Eclipse
The method has its limits though, because at some point the include files will get less and less Standard-C, but more and more compiler dependent. The C-standard does not prescribe the contents of standard headers. It merely states that if you write #include <stdio.h>, you can use printf(). That does not necessarily mean that stdio.h has some form you might expect.

Related

Should Mac OSX have a "malloc.h" file? [duplicate]

This question already has answers here:
difference between <stdlib.h> and <malloc.h>
(6 answers)
Closed 3 years ago.
A customer's code is expecting to find an include file malloc.h in one of the "usual suspect" locations. On my Mac, AFAICT, there is no malloc.h, at least not in any place you would expect to find it, such as /usr/include, /usr/local/include, or /opt/local/include. Since the malloc() is usually defined in stdlib.h, and since the code includes stdlib.h anyway, I was able to get the code to build by just commenting out the few includes of malloc.h. I am building with gcc.
But two questions: Is my gcc messed up somehow? Should that file be there? Also, the code bombs almost immediately with a seg fault that I haven't been able to track down yet. Could this be the consequence of using the wrong malloc()?
The malloc.h is deprecated and should not be used. It contains some non-standard functions too. If you want to use malloc, then include stdlib.h. Not even the C89 standard mentions malloc.h
If it's the cause of your problems, I don't know, but it's quite probable.
The listing below can provide ideas on where the file should/could be found. I can't explain the segfault without more details, though. I do not see how it could be related to malloc() in any way.
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/malloc.h
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/malloc/malloc.h
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/Kernel.framework/Versions/A/Headers/sys/malloc.h
/Applications/Xcode.app/Contents/Developer/Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS.sdk/usr/include/malloc/malloc.h
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/usr/include/malloc/malloc.h
/Applications/Xcode.app/Contents/Developer/Platforms/WatchOS.platform/Developer/SDKs/WatchOS.sdk/usr/include/malloc/malloc.h
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/sys/malloc.h
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/malloc/malloc.h
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks/Kernel.framework/Versions/A/Headers/sys/malloc.h
/Applications/Xcode.app/Contents/Developer/Platforms/WatchSimulator.platform/Developer/SDKs/WatchSimulator.sdk/usr/include/malloc/malloc.h
/Applications/Xcode.app/Contents/Developer/Platforms/AppleTVSimulator.platform/Developer/SDKs/AppleTVSimulator.sdk/usr/include/malloc/malloc.h
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/usr/include/malloc/malloc.h
$

#include in header files in embedded [duplicate]

This question already has answers here:
Should I use #include in headers?
(9 answers)
Closed 6 years ago.
I am reading through an embedded C standard book and I noticed the following:
No header file shall contain an #include statement
What should I do with function declarations that have non-standard types?
example: void function(some_funky_type x);
Throw that book away; it is absolute garbage. In fact, you should burn it, to make sure no other poor soul ever picks it up.
Header files should absolutely include all of the header files necessary for them to be self-sufficient. There is nothing worse than trying to carefully massage the order of your #include statements to be sure that the types needed by one are already defined before it is included.
This is a stupid and counterproductive rule for precisely the reason you identified. The alternative is for every .c file to include all of the .h files that a subsequently included header will require. You can imagine that if you introduce a new dependency in a commonly included header that you will now have to update every C file that includes that header.

How to find, from which include file a C preprocessor definition was taken? [duplicate]

This question already has answers here:
How to know (in GCC) when given macro/preprocessor symbol gets declared?
(6 answers)
Closed 7 years ago.
How can I find the include file, from which a certain preprocessor definition was found by GCC?
In a successfully compiling C file, I have a strange macro which I do not understand. For a start, I want to see the file where it comes from. The include hierarchy is very deep; is there an easy way to find the source of the macro?
A wider question has been asked, but the answers tell how to find the definition itself, not its source file.
redefine it before including anything else, the compiler will complain about a redefinition when it encounters it in your header hierachy:
#define MY_PROBLEMATIC_MACRO
#include <the_header.h>
/* code */

<stdio.h> vs <math.h> - Why do you have to link one and not the other? [duplicate]

This question already has answers here:
Why do you have to link the math library in C?
(14 answers)
Closed 9 years ago.
I'm confused why you have to type -lm to properly link math to your code, but don't have to do the same for stdio. I've only just started using C, so I apologize if this is a stupid quesiton or I'm missing something obvious.
In short, because of historical reasons,
The functions in stdio.h are in libc, while the functions in math.h are in libm. libc is linked by default but libm isn't.
There are two different things:
header files (stdio.h and math.h) - they contain only function prototypes and some definitions and data; they are #included in your source code
libraries (libm.so) - they contain binary code which will be linked back into your application (binary code). Also, for a library named libname.so the linker flag is -lname - for libm.so the flag is -lm.
Take also in consideration that there are libc.so and libstdc.so which are always linked into your application. Code for functions in stdio.h and stdlib.h and several others is found on those libraries - thus, it is always included.
PS: I'm assuming Linux/UNIX here, thus the names are very specific. On Windows things are similar but with other names (DLLs instead of .so files, etc.)

Where to find the implementation of stdio.h? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Where to find stdio.h functions implementations ?
Hi, I am trying to find the function definitions of the functions defined in stdio.h header file, I want to learn how functions like printf() is achieved, but I can't find any preprocessor directives link in stdio.h to the implementation file elsewhere. How can a C Compiler know where to find the implementations when there are no direct references to the function definition file? (I learned that .h file may accompany with a same name .c implementation file from an objective-c book.) Could you help me? Thanks! I am using GCC on Mac OS X.
FreeBSD's libc is pretty well laid out in its src repository.
http://www.freebsd.org/cgi/cvsweb.cgi/src/lib/libc/
e.g. for printf(3):
http://svnweb.freebsd.org/base/head/lib/libc/stdio/printf.c?view=markup
http://svnweb.freebsd.org/base/head/lib/libc/stdio/vfprintf.c?view=markup
Try downloading source code for GLIBC library project. That's where definitions for standard functions are when using GCC compiler (and derivates).

Resources