Library functions - where can i find them [duplicate] - c

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Where to find stdio.h functions implementations?
In C i have been using headers that contain prototypes and declarations of functions provided by the libraries, but where are functions like printf, scanf, etc. stored?
Where are they stored?
In which directory?
Why can't i find them? Are they stored as object files?

As everyone said, its int libc. You can search google for source code browser for the OS you are interested in, for linux I could find: http://lxr.linux.no/linux/
For netbsd, you can find printf and scanf here:
http://cvsweb.netbsd.org/bsdweb.cgi/src/lib/libc/?only_with_tag=MAIN#dirlist

These functions are provided by the standard C library, often called libc.
In linux, you can find it under /lib/libc.so
In Windows they call it C Run-Time Libraries.

On Mac OS X, those functions are in stdio.h and is located in /usr/include. The library libc.dylib is in /usr/lib.

One place to check out is the section 3 of the manual. Then each individual function manual page lists the required header files and what additional libraries, if any, you need to link with.

Related

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

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.

<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).

Why do I have to explicitly link with libm? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Why do you have to link the math library in C?
When I write a program that uses functions from the math.h library, why is it that I have to explicitly link to libm even though they are part of the C standard library?
For instance, when I want to use the sin() function I need to #include <math.h> but I also need to pass -lm to GCC. But for any other library from the standard library, I don't have to do that. Why the difference?
In the old days, linkers were slow and separating the mostly unused math code from the rest made the compilation process go faster. The difference is not so great today, so you can add the -lm option to your default compiler configuration.
Note that the header <math.h> (or any other header) does not contain code. It contains information about the code, specifically how to call functions. The code itself is in a library. I mean, your program does not use the "<math.h> library", it uses the math library and uses the prototypes declared in the <math.h> header.
It's the same reason you have to explicitly link to libpthread on most implementations. When something new and scary is added to the standard library, it usually first gets implemented as a separate add-on library that overrides some of the symbols in the old standard library implementation with versions that conform to the new requirements, while also adding lots of new interfaces. I wouldn't be surprised if some historical implementations had separate versions of printf in libm for floating point printing, with a "light" version in the main libc lacking floating point. This kind of implementation is actually mentioned and encouraged for tiny systems in the ISO C rationale document, if I remember correctly.
Of course in the long-term, separating the standard library out like this leads to a lot more problems than benefits. The worst part is probably the increased load time and memory usage for dynamic-linked programs.
Actually, the reason you normally don't need to link against libm for most math functions is that these are inlined by your compiler. Your program would fail to link on a platform where this is not the case.

seeing the header files in c in linux

i want to know how can one know the functions present in header files in c when running in linux systems
Here: http://www.gnu.org/s/libc/manual/html_node/index.html
If you are writing C on Linux, your standard C library is nearly always the GNU C library. All standard C functions are present in this library, and the link above is to extensive documentation for them.
If you are planning to use libraries other than the standard C library, you should find their documentation as well.
And don't forget, if you know the name of a function you can type man 3 funcname for help on that function, including the name of the header file it appears in. Use the 3 to specify the C Library Function section of the manual. If you forget it and there's a command-line program (section 1) or system call (section 2) with the same name, you'll get that page instead.
By looking at them. Header files are usually in /usr/include and you can easily browse that folder.
However, some functions might be in different header files depending on the system (linux/bsd/other unixes).

Resources