我在哪里可以找到C语言函数的实现代码? [英] Where can I find the implementation code for a function in C language?

查看:97
本文介绍了我在哪里可以找到C语言函数的实现代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Java 5年后学习C编程。我想知道在哪里可以找到C语言本身的源文件。


例如strcat是一个函数,它连接两个字符串。我想看看这个函数是如何以原生形式实现的,

我在哪里可以找到相应的.c文件。我在使用Eclipse IDE的cygwin上使用gcc版本

3.3.1(线程模型POSIX)。


函数名称上的grep列出了许多.h和.c文件声明了函数的原型

。但是如何从

有效导航到实际实现。你知道我听起来有点丢失了。非常感谢任何帮助。


请用我非正统的方法来学习新东西。


提前使用。

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.

推荐答案

" Stegano" <哒********* @ gmail.com>在消息中写道

新闻:ea ************************** @ posting.google.c om ...
"Stegano" <da*********@gmail.com> wrote in message
news:ea**************************@posting.google.c om...
我在使用Java 5年后学习C编程。我想知道在哪里可以找到C语言本身的源文件。


语言不包含''源文件''。这是一个规范,

用英文写的。也许你的意思是C

实现的源代码。

例如strcat是一个函数,它连接两个字符串。我想读一下这个函数是如何以原生形式实现的,


它没有''原生形式''。它的实现很大程度上取决于它实现的平台以及编写它的人。

我在哪里可以找到相应的.c文件。我在使用Eclipse IDE的cygwin上使用gcc版本
3.3.1(线程模型POSIX)。


AFAIK,gcc编译器的所有(或大部分)源代码是

,可从 www.gcc.gnu.org

函数名称上的grep列出了许多.h和.c文件声明该函数的原型。但是,我如何有效地从那里导航到实际的实现。


看来你没有它的来源。转到上面的链接

找到它。

你知道我听起来有点丢失了。非常感谢任何帮助。

请用我非正统的方法来学习新事物。
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.




我发现它不是非正统的所有。我怀疑通过将源代码分离到编译器中,你将获得很好的学习经验。

就像小时候我把父亲的分开一样收音机。

我得到了打屁股,但我学到了很多东西(来自两次经历:-))


请记住其他编译器的来源可以和

确实大不相同。


-Mike



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


文章< 8d ** *************@newsread3.news.pas.earthlink.ne t>,

Mike Wahler< mk ****** @ mkwahler.net>写道:
In article <8d***************@newsread3.news.pas.earthlink.ne t>,
Mike Wahler <mk******@mkwahler.net> wrote:
" Stegano" <哒********* @ gmail.com>在消息中写道
新闻:ea ************************** @ posting.google。 com ...
"Stegano" <da*********@gmail.com> wrote in message
news:ea**************************@posting.google. com...
我在使用Java 5年后学习C编程。我想知道在哪里可以找到C语言本身的源文件。
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.



该语言不包含''源文件''。这是一个用英语写的规范。也许你的意思是C
实现的源代码。



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


例如strcat是一个函数,它连接两个字符串。我想阅读这个函数是如何以其原生形式实现的,

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.

在哪里可以找到相应的.c文件。我在使用Eclipse IDE的cygwin上使用gcc版本
3.3.1(线程模型POSIX)。
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,gcc编译器的全部(或大部分)源代码可从 www.gcc.gnu.org 获取




是的,但GNU的strcat()不随编译器一起发布。

strcat()是C标准库的一部分。因此,与glibc一起分发

。请参阅:

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


无论它的价值如何,这里都是GNU'strcat()的源代码。我留下

它作为练习让你找出代码的哪些部分是

标准C以及哪些部分依赖于gcc的内部。


--- strcat.c ------------------------------------ -----------------

#include< string.h>

#include< memcopy.h>


/ *在DEST结束时附加SRC。 * /

char * strcat(char * dest,const char * src)

{

char * s1 = dest;

const char * s2 = src;

reg_char c;


/ *查找字符串的结尾。 * /

do

c = * s1 ++;

while(c!=''\ 0'');


/ *在下一个字符之前使S1指向,所以我们可以在读取内存时将其增加

*(在流水线cpus上获胜)。 * /

s1 - = 2;


做{

c = * s2 ++;

* ++ s1 = c;

} while(c!=''\''');


返回dest;

}

-

rr



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




" Stegano" <哒********* @ gmail.com>写了

"Stegano" <da*********@gmail.com> wrote

我在使用Java 5年后学习C编程。我想知道在哪里可以找到C语言本身的源文件。

例如strcat是一个函数,它连接两个字符串。

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.



参见Plauger的书The Standard C library。


输入和输出函数不能用纯ANSI C实现,因为

他们必须在某种程度上与硬件进行交互。数学函数

像sqrt()可以用C语言编写,但是现在你可能想用

特殊的硬件指令来加快速度。


strcat()之类的函数非常容易编写


char * strcat(char * dest,const char * src)

$

char * end;


end = dest;


/ *提前结束dest * /

while(* end)

end ++;


/ *附加src * /

while(* src)

* end ++ = * src ++;


/ *添加nul * /

* end = 0;


/ *无用,我们返回指向dest的指针* /

返回dest;


}


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;

}


这篇关于我在哪里可以找到C语言函数的实现代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆