sh-5.2$ ls
main main.c main.i
sh-5.2$ grep -rn 'printf'
main.c:7:#define PRINT(A, B, C) printf(STR(INT NL), LINK(A, B, C))
main.c:12: printf("\n");
main.i:469:extern int fprintf (FILE *__restrict __stream,
main.i:475:extern int printf (const char *__restrict __format, ...);
main.i:477:extern int sprintf (char *__restrict __s,
main.i:484:extern int vfprintf (FILE *__restrict __s, const char *__restrict __format,
main.i:490:extern int vprintf (const char *__restrict __format, __gnuc_va_list __arg);
main.i:492:extern int vsprintf (char *__restrict __s, const char *__restrict __format,
main.i:497:extern int snprintf (char *__restrict __s, size_t __maxlen,
main.i:499: __attribute__ ((__nothrow__)) __attribute__ ((__format__ (__printf__, 3, 4)));
main.i:501:extern int vsnprintf (char *__restrict __s, size_t __maxlen,
main.i:503: __attribute__ ((__nothrow__)) __attribute__ ((__format__ (__printf__, 3, 0)));
main.i:509:extern int vasprintf (char **__restrict __ptr, const char *__restrict __f,
main.i:511: __attribute__ ((__nothrow__)) __attribute__ ((__format__ (__printf__, 2, 0))) ;
main.i:512:extern int __asprintf (char **__restrict __ptr,
main.i:514: __attribute__ ((__nothrow__)) __attribute__ ((__format__ (__printf__, 2, 3))) ;
main.i:515:extern int asprintf (char **__restrict __ptr,
main.i:517: __attribute__ ((__nothrow__)) __attribute__ ((__format__ (__printf__, 2, 3))) ;
main.i:522:extern int vdprintf (int __fd, const char *__restrict __fmt,
main.i:524: __attribute__ ((__format__ (__printf__, 2, 0)));
main.i:525:extern int dprintf (int __fd, const char *__restrict __fmt, ...)
main.i:526: __attribute__ ((__format__ (__printf__, 2, 3)));
main.i:816: printf("INT NL", 520);
main.i:817: printf("\n");
grep: main: binary file matches
sh-5.2$ vim main.i
sh-5.2$ cat main.i
extern int printf (const char *__restrict __format, ...);
# 9 "main.c"
int main(void)
{
printf("INT NL", 520);
printf("\n");
return 0;
}
sh-5.2$ gcc -o main main.i
sh-5.2$ ./main
INT NL
sh-5.2$
因为预处理之后的结果中
第475行你没有复制上,这个是printf函数的声明
最前面的 #include <stdio.h>
这个stdio.h里面有一行这样的代码extern int printf (const char *__restrict __format, ...);
对于你这个程序,其实#include <stdio.h>
只有一个作用,就是把printf函数的这个声明复制到你的源代码里面
如果你自己复制了,那就不需要#include <stdio.h>了
像下面这样// 这里没有#include <stdio.h>
// 但是使用了printf函数
// 所以要手动声明一下printf函数
extern int printf (const char *__restrict __format, ...);
int main(void) {
printf("hello world!\n");
return 0;
}
|