9

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 ?

4
  • 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>? Commented Nov 14, 2010 at 18:30
  • 1
    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 :) Commented Nov 14, 2010 at 18:31
  • 12
    @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. Commented Nov 14, 2010 at 18:32
  • @meager : It was just a quiz question. Commented Nov 14, 2010 at 18:34

5 Answers 5

19

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

Sign up to request clarification or add additional context in comments.

Comments

13

Just:

man 3 printf

It will tell you printf signature:

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

this is the right one.

Comments

4

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

But it should be const char *.

8 Comments

@Oli : Does only char* affect anything ?
@Charles: In C99, yes. That won't compile for anything earlier.
@Charles Just const char* here.
@meagar: This is not C++. I agree it's wrong to omit the const, but it does not "define a completely different function", only an incorrect prototype for the same function. (I suspect this wrong prototype is actually guaranteed to work anyway, but I'm not sure.)
@Happy Mittal: If you don't include all the required type qualifiers then you have provided a function prototype that isn't compatible with the function definition. C doesn't have function overloading so you can't be referring to any other function but attempting to call it with an incompatible prototype in scope technically leads to undefined behaviour.
|
0

Here is another version of the declaration:

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

Comments

-3
int printf(char *, ...);

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

1 Comment

The reason it has to be const is that when you define a string with char *, you pretty much have to make it const so that when you increment the pointer or do anything with it, you will lose its location in memory. That's why it's const--to give you an error if you try to modify it. In any case, generally, when you define strings like this, it's not exactly encouraged (from my background) and this kind also has problems with functions. While they COULD say char [], the reality is that C has no pointers, so they choose to leave it at that, declare strings with char *.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.