Where can I find the implementation code for a function in C language?

S

Stegano

I am learning C Programming after working with Java for 5 years. I
want to know where can I find the source files for C language itself.

For example strcat is a function, which concatenates two strings. I
want to read how this function is implemented in its native form,
where can I find its corresponding .c file. I am using gcc version
3.3.1 (Thread model POSIX) on cygwin with Eclipse IDE.

A grep on a function name lists many .h and .c files which declare the
prototype of the function. But how do I efficiently navigate from
there to get to the actual implementation. You know I sound kind a
lost. Any help is greatly appreciated.

Please bear with my unorthodox approach to learning new things.

Thanx in advance.
 
M

Mike Wahler

Stegano said:
I am learning C Programming after working with Java for 5 years. I
want to know where can I find the source files for C language itself.

The language doesn't contain 'source files'. It's a specification,
written in English. Perhaps you mean the source code for a C
implementation.
For example strcat is a function, which concatenates two strings. I
want to read how this function is implemented in its native form,

It doesn't have a 'native form'. Its implementation depends heavily
upon the platform where it is implemented, and upon who wrote it.
where can I find its corresponding .c file. I am using gcc version
3.3.1 (Thread model POSIX) on cygwin with Eclipse IDE.

AFAIK, All (or most) of the source code for the gcc compiler is
available from www.gcc.gnu.org
A grep on a function name lists many .h and .c files which declare the
prototype of the function. But how do I efficiently navigate from
there to get to the actual implementation.

It appears you don't have the source for it. Go to the above link
to find it.
You know I sound kind a
lost. Any help is greatly appreciated.

Please bear with my unorthodox approach to learning new things.

I don't find it unorthodox at all. I suspect you'll have a great
learning experience by picking apart the source to a compiler.
Just like when as a child I took apart my father's radio set.
I got a spanking, but I learned much (from both experiences :))

Just keep in mind that the source for other compilers can and
does differ drastically.

-Mike
 
R

Rouben Rostamian

The language doesn't contain 'source files'. It's a specification,
written in English. Perhaps you mean the source code for a C
implementation.


It doesn't have a 'native form'. Its implementation depends heavily
upon the platform where it is implemented, and upon who wrote it.


AFAIK, All (or most) of the source code for the gcc compiler is
available from www.gcc.gnu.org

Yes, but GNU's strcat() is not distributed with the compiler.
strcat() is a part of the C standard libarary. As such, it is
distributed with glibc. See:

http://directory.fsf.org/all/glibc.html

For whatever it's worth, here is GNU's source to strcat(). I leave
it as an exercise for you to find out which parts of the code are
standard C and which parts rely on gcc's internals.

--- strcat.c -----------------------------------------------------
#include <string.h>
#include <memcopy.h>

/* Append SRC on the end of DEST. */
char *strcat(char *dest, const char *src)
{
char *s1 = dest;
const char *s2 = src;
reg_char c;

/* Find the end of the string. */
do
c = *s1++;
while (c != '\0');

/* Make S1 point before the next character, so we can increment it
* while memory is read (wins on pipelined cpus). */
s1 -= 2;

do {
c = *s2++;
*++s1 = c;
} while (c != '\0');

return dest;
}
 
R

Rich Gibbs

Mike Wahler said the following, on 10/09/04 13:28:
The language doesn't contain 'source files'. It's a specification,
written in English. Perhaps you mean the source code for a C
implementation.




It doesn't have a 'native form'. Its implementation depends heavily
upon the platform where it is implemented, and upon who wrote it.




AFAIK, All (or most) of the source code for the gcc compiler is
available from www.gcc.gnu.org




It appears you don't have the source for it. Go to the above link
to find it.

In addition to Mike's good advice, you might also be interested in P.J.
Plauger's excellent book, _The Standard C Library_, published by
Prentice Hall. It contains source code for an implementation of the
library, as well as a discussion of the relevant parts of the C
standard, and of the implementation. I found it very helpful when I was
learning C.

One other minor point: since 'strcat' is a library function, I think its
source code will be with the rest of the 'glibc' library, not with the
compiler itself. Look for 'glibc' at fsf.org.
 
M

Malcolm

Stegano said:
I am learning C Programming after working with Java for 5 years. I
want to know where can I find the source files for C language itself.

For example strcat is a function, which concatenates two strings.
See Plauger's book "The Standard C library".

The input and output functions cannot be implemented in pure ANSI C, since
they have to interact with hardware at some level. Mathematical functions
like sqrt() can be written in C, but nowadays you would probably want to use
special hardware instructions to speed things up a bit.

Functions like strcat() are however very easy to write

char *strcat(char *dest, const char *src)
{
char *end;

end = dest;

/* advance end to end of dest */
while(*end)
end++;

/* append the contents of src */
while(*src)
*end++ = *src++;

/* add the nul */
*end = 0;

/* uselessly, we return a pointer to dest */
return dest;

}
 
M

Mike Wahler

Malcolm said:
See Plauger's book "The Standard C library".

The input and output functions cannot be implemented in pure ANSI C, since
they have to interact with hardware at some level. Mathematical functions
like sqrt() can be written in C, but nowadays you would probably want to use
special hardware instructions to speed things up a bit.

Functions like strcat() are however very easy to write

char *strcat(char *dest, const char *src)
{
char *end;

end = dest;

/* advance end to end of dest */
while(*end)
end++;

/* append the contents of src */
while(*src)
*end++ = *src++;

/* add the nul */
*end = 0;

/* uselessly, we return a pointer to dest */
return dest;

}

I don't find the custom of having string handling functions
return a pointer to a destination string to be 'useless',
but often convenient, and of course when desired, the return
value can be ignored (as is almost always the case when calling
'printf()').

printf(strcat(a,b));

-Mike
 
R

Ryan Reich

I don't find the custom of having string handling functions
return a pointer to a destination string to be 'useless',
but often convenient, and of course when desired, the return
value can be ignored (as is almost always the case when calling
'printf()').

printf(strcat(a,b));

True enough, but as a matter of aesthetics I find that particular type of
call to be just a little lacking. I suppose I really just have a problem
with the idea of 'errno', as it seems to me that any function which can
experience an internal error ought to indicate such as its return value,
rather than employing a global variable for no good reason. It does mean
that you can't chain calls together, but I think that just makes the code
more readable anyway. Something like

if(strcat(a,b) != 0)
indicate_error();
printf("%s", a);

has the stylistic advantage of indicating clearly that you intend to
concatenate 'a' and 'b' (rather than, for example, return a string which is
their concatenation but leave them unchanged. We all know what 'strcat()'
does, of course, but it still looks that way and using the return value
turns the actual task of the function into a side-effect, which is not a
good idea to me), of checking whether it was done cleanly (maybe 'strcat()'
isn't the best function to illustrate this with, since it can't really fail
without crashing the program entirely due to buffer overflow), and of
handling the error before you do anything with the result. Plus it has
each line do one well-defined action rather than, in the example you gave,
four ill-defined ones (concatenate, NOT check error conditions, print, and
possibly fail anyway because you may have been distracted by all the typing
and forgot to include the format string).
 
M

Mike Wahler

Ryan Reich said:
True enough, but as a matter of aesthetics I find that particular type of
call to be just a little lacking.

What does it lack? I've never encountered anyone with
a reasonable amount of C experience that had trouble
grasping it at a glance.
I suppose I really just have a problem
with the idea of 'errno',
as it seems to me that any function which can
experience an internal error ought to indicate such as its return value,

And what kind of 'internal error' would e.g. 'strcat()' encounter?
(I would not classify receiving bad input, or insufficient storage
for output as an 'internal' error, but a coding error.)
rather than employing a global variable for no good reason.

The 'str...()' functions don't use errno.
It does mean
that you can't chain calls together, but I think that just makes the code
more readable anyway. Something like

if(strcat(a,b) != 0)
indicate_error();

And what would you consider to comprise an 'internal error' in a 'str...()'
function?
printf("%s", a);

has the stylistic advantage of indicating clearly that you intend to
concatenate 'a' and 'b'

IMO the very appearance of 'strcat()' as an expression or subexpression
indicates it very clearly.
(rather than, for example, return a string which is
their concatenation but leave them unchanged.

Returning a separate string poses problems, since an array cannot be
returned from a function, meaning that memory management becomes
even more of an issue that it already is.
We all know what 'strcat()'
does, of course, but it still looks that way

Looks what way? That it 'returns a string'? Well, yes it does,
a pointer to the destination. To what should this pointer point?

and using the return value
turns the actual task of the function into a side-effect,

It already is. Returning a value or not doesn't change this.
which is not a
good idea to me), of checking whether it was done cleanly

How could it not be done 'cleanly'? (imo misuse, e.g. array overflow
doesn't qualify as a failure of 'strcat()', but of the coder).
(maybe 'strcat()'
isn't the best function to illustrate this with, since it can't really fail
without crashing the program entirely due to buffer overflow), and of
handling the error before you do anything with the result. Plus it has
each line do one well-defined action rather than, in the example you gave,
four ill-defined ones

All the actions are well-defined.
(concatenate, NOT check error conditions,

What error conditions? It's simply impossible to check for every
possible programmer mistake. E.g. how could 'strcat()' check for
overflow when it cannot know the size of the input or output buffers?
print, and
possibly fail anyway because you may have been distracted by all the
typing

All the typing? Huh? It's actually more typing to break it
into more lines. But I wouldn't call doing so 'inferior' to
doing it all in one expression, but simply a stylistic issue.

and forgot to include the format string).

'printf()' requires at least one argument. If one is not
supplied, the compiler is required to diagnose it.

-Mike
 
C

CBFalconer

Mike said:
.... snip ...

I don't find the custom of having string handling functions
return a pointer to a destination string to be 'useless',
but often convenient, and of course when desired, the return
value can be ignored (as is almost always the case when calling
'printf()').

There have been discussions about this before. In functions such
as strcat the function return encourages writing valid, but faulty,
code. Functions such as the (non-standard) strlcat avoid this, and
return something useful, such as the length of the final string.
 
M

Malcolm

Mike Wahler said:
printf(strcat(a,b));
Doesn't make it obvious that a is modified by the call. Of course a reg
would be familiar enough with strcat() for this not to be a problem, but
someone with a bit of C casually glancing at the code would assume that a
and b are concatenated, the result passed to printf(), but neither modified.
 
P

pete

Stegano said:
I am learning C Programming after working with Java for 5 years. I
want to know where can I find the source files for C language itself.

For example strcat is a function, which concatenates two strings. I
want to read how this function is implemented in its native form,
where can I find its corresponding .c file. I am using gcc version
3.3.1 (Thread model POSIX) on cygwin with Eclipse IDE.

A grep on a function name lists many .h and .c files which declare the
prototype of the function. But how do I efficiently navigate from
there to get to the actual implementation. You know I sound kind a
lost. Any help is greatly appreciated.

Please bear with my unorthodox approach to learning new things.

Two things that I would watch out for:

1 Standard header files use reserved identifier conventions.
Don't you use reserved identifier conventions too.

2 Some aspects of standard functions are implementation defined,
unspecified, or undefined by the standard.
That means that some versions of some standard functions
won't do exactly the same thing,
as other versions on different implementations,
under various circumstances.
 
S

Stegano

Malcolm said:
See Plauger's book "The Standard C library".

The input and output functions cannot be implemented in pure ANSI C, since
they have to interact with hardware at some level. Mathematical functions
like sqrt() can be written in C, but nowadays you would probably want to use
special hardware instructions to speed things up a bit.

Functions like strcat() are however very easy to write

char *strcat(char *dest, const char *src)
{
char *end;

end = dest;

/* advance end to end of dest */
while(*end)
end++;

/* append the contents of src */
while(*src)
*end++ = *src++;

/* add the nul */
*end = 0;

/* uselessly, we return a pointer to dest */
return dest;

}


First of All Thank you everybody for being so generous in guiding me.
The idea was to come up with a better way to learn pointer arithmetics
and I think comparing my implementation with code for standard C
functions.

The other thing that I am planning to do is to implement standard data
structures that I know very well in Java in C.

Thanks Again everybody.

-daya
 
M

Mike Wahler

Malcolm said:
Doesn't make it obvious that a is modified by the call. Of course a reg
would be familiar enough with strcat() for this not to be a problem, but
someone with a bit of C casually glancing at the code would assume that a
and b are concatenated, the result passed to printf(), but neither
modified.

IMO a very poor assumption, given that passing pointers
as arguments is not at all uncommon in C.

-Mike
 
K

Kenny McCormack

/* uselessly, we return a pointer to dest */
return dest;

}

I don't find the custom of having string handling functions
return a pointer to a destination string to be 'useless',
but often convenient, and of course when desired, the return
value can be ignored.[/QUOTE]

There should be a convention on Usenet that would indicate when an
editorial comment is being inserted "under cover" - that, is in a context
that makes it sound like the thing being claimed is accepted fact.

Clearly, when the previous poster used the word "uselessly", it was such an
editorial comment (based, no doubt, on some previous threads in this ng).
Had the poster posted it as:

Then Mike would probably have (properly, IMHO) ignored the comment.

Two notes:
1) The "IMHO" convention goes some way towards this, but people
often respond to IMHOs anyway. Something stronger is needed.
2) Obviously, the original implemntors of C and the C string
functions knew that returning the destination string was useful and so
that's why they did it.
 
J

John Bode

I am learning C Programming after working with Java for 5 years. I
want to know where can I find the source files for C language itself.

For example strcat is a function, which concatenates two strings. I
want to read how this function is implemented in its native form,
where can I find its corresponding .c file. I am using gcc version
3.3.1 (Thread model POSIX) on cygwin with Eclipse IDE.

A grep on a function name lists many .h and .c files which declare the
prototype of the function. But how do I efficiently navigate from
there to get to the actual implementation. You know I sound kind a
lost. Any help is greatly appreciated.

Please bear with my unorthodox approach to learning new things.

Thanx in advance.

Someone else pointed out the link to the GNU site, but be aware that
most C compiler implementations are proprietary, and as such do not
ship any library source code. Plauger's book is good for showing how
the libraries *may* be implemented, but it's not guaranteed to be the
same for every platform or compiler.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top