7
votes

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 ?

5
Your format string should be const char*. You could just locate stdio.h and read the definition there. Why, out of curiosity, don't you want to #include <stdio.h>? - meagar♦
how do you expect to print anything on the screen without including stdio.h ?? You'll have to write your own libraries .. it's suicidal :) - sdadffdfd
@bemace @Vic The act of including stdio.h doesn't link anything, header files don't work that way. This question is completely valid, and will work just fine. - meagar♦
@meager : It was just a quiz question. - Happy Mittal

5 Answers

18
votes

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

12
votes

Just:

man 3 printf

It will tell you printf signature:

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

this is the right one.

4
votes

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

But it should be const char *.

0
votes

Here is another version of the declaration:

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

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