473,594 Members | 2,757 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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.
Nov 14 '05 #1
14 2305
"Stegano" <da*********@gm ail.com> wrote in message
news:ea******** *************** ***@posting.goo gle.com...
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
Nov 14 '05 #2
In article <8d************ ***@newsread3.n ews.pas.earthli nk.net>,
Mike Wahler <mk******@mkwah ler.net> wrote:
"Stegano" <da*********@gm ail.com> wrote in message
news:ea******* *************** ****@posting.go ogle.com...
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


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;
}
--
rr
Nov 14 '05 #3

"Stegano" <da*********@gm ail.com> wrote

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;

}
Nov 14 '05 #4

"Malcolm" <ma*****@55bank .freeserve.co.u k> wrote in message
news:ck******** **@news8.svr.po l.co.uk...

"Stegano" <da*********@gm ail.com> wrote

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;

}


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

Nov 14 '05 #5
On Saturday 09 October 2004 18:07, Mike Wahler wrote:
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).

--
Ryan Reich
ry***@uchicago. edu
Nov 14 '05 #6

"Ryan Reich" <ry***@uchicago .edu> wrote in message
news:2s******** *****@uni-berlin.de...
On Saturday 09 October 2004 18:07, Mike Wahler wrote:
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.


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
Nov 14 '05 #7
Mike Wahler said the following, on 10/09/04 13:28:
"Stegano" <da*********@gm ail.com> wrote in message
news:ea******** *************** ***@posting.goo gle.com...
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.


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.
--
Rich Gibbs
rg****@alumni.p rinceton.edu

Nov 14 '05 #8
Mike Wahler wrote:
.... 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.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!

Nov 14 '05 #9

"Mike Wahler" <mk******@mkwah ler.net> wrote

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.
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
5022
by: Jonas Rundberg | last post by:
Hi I just started with c++ and I'm a little bit confused where stuff go... Assume we have a class: class test { private: int arr; };
11
7292
by: Neo | last post by:
Where does global, static, local, register variables, free memory and C Program instructions get stored? -Neo
5
3008
by: Mike Labosh | last post by:
In VB 6, the Form_QueryUnload event had an UnloadMode parameter that let me find out *why* a form is unloading, and then conditionally cancel the event. In VB.NET, the Closing event passes a CancelEventArgs that lets me cancel the Close() operation, but is there still any way to find out *why* a form is closing? This app as a form that needs to be loaded at startup, closed only at shutdown, and then Show() / Hide() for the user. If...
6
2760
by: bobueland | last post by:
The module string has a function called translate. I tried to find the source code for that function. In: C:\Python24\Lib there is one file called string.py I open it and it says
29
51733
by: Ajay | last post by:
Hi all,Could anybody tell me the most efficient method to find a substr in a string.
18
33864
by: Jack | last post by:
Thanks.
21
2618
by: Chen ShuSheng | last post by:
Hey, In head file 'stdio.h', I only find the prototype of these two function but no body. Could someone pls tell me where their bodies are ? -------------- Life is magical.
15
2277
by: amit.man | last post by:
Hi, i have newbie qestion, when i write #include <somthing.h> the precompiler subtitue that line with the lines from "somthing.h" header file. when, where and how the compiler insert the lines in "somthing.c" implementation file into my source code?
41
18135
by: Miroslaw Makowiecki | last post by:
Where can I download Comeau compiler as a trial version? Thanks in advice.
0
7947
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7880
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8374
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8010
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8242
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5739
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5413
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2389
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1486
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.