Source code is generally understood to mean programming statements that are created by a programmer with a text editor or a visual programming tool and then saved in a file. Object code generally refers to the output, a compiled file, which is produced when the Source Code is compiled with a C compiler.
A function is a block of code which only runs when it is called. You can pass data, known as parameters, into a function. Functions are used to perform certain actions, and they are important for reusing code: Define the code once, and use it many times.
Source code is the language or string of words, numbers, letters and symbols that a computer programmer uses. An example of source code is someone using HTML code to create a screen.
You could check out a copy of glibc, which will have the source code for all C functions in the C standard library. That should be a good starting place.
It's worth pointing out that the source code for the C standard library does not necessarily highlight good coding practices. As the standard library, it has a special status where, for example, it can rely on a lot of unspecified or non-portable behavior simply because they know the exact compiler with which it is used.
And of course, this only tells you about C coding practices.
That has nothing to do with C++ coding practices, which are very different. Don't treat them as one language. There is no such thing as C/C++. C is a language with its own coding practices and common idioms, and C++ is a separate, independent language which also has its own practices and idioms.
Good C code is rarely good C++ code. strcpy
and other C library functions are certainly not good C++ code.
Most compilers provide source code for the library - however that source code is usually rather more complex and convoluted than you might expect.
A good resource for this is P.J. Plauger's book, "The Standard C Library", which can be had pretty cheaply if you go for a used one. The code presented is not always straight-forward, but Plauger explains it quite well (and gives the reasons why it can't always be straight-forward and still follow the standard).
Many C standard library functions have source code listings & discussions in The C Programming Language by Kernighan & Ritchie. The discussions are a helpful way of learning more about the specifics of the C language & how functions in the standard library work under the hood.
Look at an implementation of the libc standard C library. To see how a real, popular C library is implemented, try looking at the glibc code. You can access the code using git:
git clone git://sourceware.org/git/glibc.git
As for C++, you can find the glibc++ standard library on one of these mirrors:
http://gcc.gnu.org/mirrors.html
You can also check out uLibc, which will probably be simpler than the GNU library:
http://git.uclibc.org/uClibc/tree/
To give you a flavour, here's the strncpy implementation from uLibc:
Wchar *Wstrcpy(Wchar * __restrict s1, const Wchar * __restrict s2)
{
register Wchar *s = s1;
#ifdef __BCC__
do {
*s = *s2++;
} while (*s++ != 0);
#else
while ( (*s++ = *s2++) != 0 );
#endif
return s1;
}
Here is strcmp and strstr
"The Standarc C Library" is a specification. You want the sources for an implementation of the specification. Your C compiler may or not provide such sources - one that does is GCC.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With