How to declare printf()?

2020-07-10 10:10发布

I wanted to print something using printf() function in C, without including stdio.h, so I wrote program as :

int printf(char *, ...);
int main(void)
{
        printf("hello world\n");
        return 0;
}

Is the above program correct ?

标签: c printf
5条回答
手持菜刀,她持情操
2楼-- · 2020-07-10 10:43
int printf(char *, ...);

works just fine, I don't know why people are telling you that char needs to be a const

查看更多
\"骚年 ilove
3楼-- · 2020-07-10 10:47

Just:

man 3 printf

It will tell you printf signature:

int printf(const char *format, ...);

this is the right one.

查看更多
ゆ 、 Hurt°
4楼-- · 2020-07-10 10:50

Here is another version of the declaration:

extern int printf (__const char *__restrict __format, ...);
查看更多
我想做一个坏孩纸
5楼-- · 2020-07-10 10:52

The correct declaration (ISO/IEC 9899:1999) is:

int printf(const char * restrict format, ... );

But it would be easiest and safest to just #include <stdio.h>.

查看更多
等我变得足够好
6楼-- · 2020-07-10 10:59

I have no idea why you'd want to do this.

But it should be const char *.

查看更多
登录 后发表回答