C语言中printf是库函数,那么printf的代码到底在哪里呢?

书上说:printf是库函数,LCC软件的目录下,有一个lib子目录,里面全是lib,就是C语言提供的库函数目标文件,在连接的时候,系统提供的函数实现就在这些文件里去找了。

那么既然如此,为什么我把lib子目录下的文件都找遍了,就是找不到printf呢?????
printf是C语言系统提供的库函数,可我在lib目录下,怎么也找不到printf这个词语。
有人说,到include目录下找,可是include目录下都是函数声明(printf的函数声明我能找到),可这和在书上说的,在lib下找库函数,简直是两码事。到底是怎么回事呢???printf的代码到底在哪里????书上明明说的,printf库函数在lib目录里找啊!!书上是不是在误导读者。
书上说,printf是C语言提供的,其编译后的目标文件读者可以找到,那么printf编译后的目标文件在在哪??

书上说的没错,lib文件中存放的就是被调用系统函数的目标代码,但是和声明文件一样不是一个函数一个文件,而是一批函数放在一个文件里。并且文件是二进制的格式,你也查看不了。
对于.h头文件你理解的没错,头文件是只是函数的声明,里面不放函数的具体代码,具体代码比如VC是在C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\crt\src 目录下,你可以找到printf.c文件的源码,如果你是想看源码就看这个。我看了其它人的回复,C编译器的不同位置放的是不同的函数部分,以VC为例,include放的是调用函数的声明部分,并且只有声明部分;src目录放的是函数的源码;lib放的是函数的编译后目标文件,但是是打捆放的,代码只有在链接时,才会将库函数进行连接,并生成最后的EXE可执行文件。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-06-19
printf()已经被编译成目标代码了,那是找不到的.但是可以在include目录下stdio.h中找到它的声明:
......
char *_Cdecl gets(char *__s);
void _Cdecl perror(const char *__s);
int _Cdecl printf(const char *__format, ...);
int _Cdecl puts(const char *__s);
int _CType remove(const char *__path);
int _CType rename(const char *__oldname,const char *__newname);
void _Cdecl rewind(FILE *__stream);
int _Cdecl scanf(const char *__format, ...);
.....本回答被网友采纳
第2个回答  2012-06-27
你看的是“零基础学C语言书”吧?
翻开第26页左右,可能有点偏差,书上明确写着,系统本身就定义好printf函数了,并且是读者是看不到和找不到的!你不用去理会他的定义,编程时写上他的声明就够了。本回答被提问者采纳
第3个回答  2012-09-07
什么找不到,我贴出来给你看:
/***
*printf.c - print formatted
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
* defines printf() - print formatted data
*
*******************************************************************************/
#include <cruntime.h>
#include <stdio.h>
#include <dbgint.h>
#include <stdarg.h>
#include <file2.h>
#include <internal.h>
#include <mtdll.h>
#include <stddef.h>
#include <process.h>
/***
*int printf(format, ...) - print formatted data
*
*Purpose:
* Prints formatted data on stdout using the format string to
* format data and getting as many arguments as called for
* Uses temporary buffering to improve efficiency.
* _output does the real work here
*
*Entry:
* char *format - format string to control data format/number of arguments
* followed by list of arguments, number and type controlled by
* format string
*
*Exit:
* returns number of characters printed
*
*Exceptions:
*
*******************************************************************************/
int __cdecl printf (
const char *format,
...
)
/*
* stdout 'PRINT', 'F'ormatted
*/
{
va_list arglist;
int buffing;
int retval;
_VALIDATE_RETURN( (format != NULL), EINVAL, -1);
va_start(arglist, format);
_lock_str2(1, stdout);
__try {
buffing = _stbuf(stdout);
retval = _output_l(stdout,format,NULL,arglist);
_ftbuf(buffing, stdout);
}
__finally {
_unlock_str2(1, stdout);
}
return(retval);
}
int __cdecl _printf_l (
const char *format,
_locale_t plocinfo,
...
)
{
va_list arglist;
va_start(arglist, plocinfo);
return _vprintf_l(format, plocinfo, arglist);
}
int __cdecl _printf_s_l (
const char *format,
_locale_t plocinfo,
...
)
{
va_list arglist;
va_start(arglist, plocinfo);
return _vprintf_s_l(format, plocinfo, arglist);
}
int __cdecl printf_s (
const char *format,
...
)
{
va_list arglist;
va_start(arglist, format);
return _vprintf_s_l(format, NULL, arglist);
}
int __cdecl _printf_p_l (
const char *format,
_locale_t plocinfo,
...
)
{
va_list arglist;
va_start(arglist, plocinfo);
return _vprintf_p_l(format, plocinfo, arglist);
}
int __cdecl _printf_p (
const char *format,
...
)
{
va_list arglist;
va_start(arglist, format);
return _vprintf_p_l(format, NULL, arglist);
}
static UINT_PTR __enable_percent_n = 0;
/***
*int _set_printf_count_output(int)
*
*Purpose:
* Enables or disables %n format specifier for printf family functions
*
*Internals:
* __enable_percent_n is set to (__security_cookie|1) for security reasons;
* if set to a static value, an attacker could first modify __enable_percent_n
* and then provide a malicious %n specifier. The cookie is ORed with 1
* because a zero cookie is a possibility.
******************************************************************************/
int __cdecl _set_printf_count_output(int value)
{
int old = (__enable_percent_n == (__security_cookie | 1));
__enable_percent_n = (value ? (__security_cookie | 1) : 0);
return old;
}
/***
*int _get_printf_count_output()
*
*Purpose:
* Checks whether %n format specifier for printf family functions is enabled
******************************************************************************/
int __cdecl _get_printf_count_output()
{
return ( __enable_percent_n == (__security_cookie | 1));
}
第4个回答  2012-06-19
我的printf.c是在这个目录下,你去那里看看。
C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\printf.c
如果你还想看VC的头文件的话,推荐一个方法,你写个测试程序,包含那个文件,调用那个文件中的函数,在调试时跟进去,系统就会自动打开那个文件了。追问

我在LCC软件下,没有找到printf生成的目标文件啊

追答

所以叫你调试时跟进去看了。

追问

我没学调试,不懂啊。书上说,printf是C语言提供的,其编译后的目标文件读者可以找到,那么printf编译后的目标文件在在哪??

相似回答