Happy Mittal
Happy Mittal

Reputation: 3747

How to declare printf()?

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 ?

Upvotes: 9

Views: 20519

Answers (5)

Zorayr
Zorayr

Reputation: 24884

Here is another version of the declaration:

extern int printf (__const char *__restrict __format, ...);

Upvotes: 0

lkl
lkl

Reputation: 7

int printf(char *, ...);

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

Upvotes: -3

CB Bailey
CB Bailey

Reputation: 791421

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>.

Upvotes: 19

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272467

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

But it should be const char *.

Upvotes: 4

peoro
peoro

Reputation: 26060

Just:

man 3 printf

It will tell you printf signature:

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

this is the right one.

Upvotes: 13

Related Questions