質問

Possible Duplicate:
C program without header

I have been studying C for a long time . but one thing that bothering me is that , today I made a C program and forget to include the stdio.h and conio.h header files I saved the file as kc.c ? when I compiled and run this .c file the output was as I was expecting to be.

but how can a C program could run without using the standard header file?

or I am not aware with the concepts that I am missing hear?

EDIT: the program

int main()
{
  int i=12,j=34;
  int *pa=&i,*pb=&j;
  printf("the value of i and j is %d %d respectively ",*pa,*pb);
  getch();
  return 0;
}

because I have used the printf() function of STDIO.H header here ,but without including it how can It got compiled and run successfully?

役に立ちましたか?

解決

The compiler is allowed to make things work, but is under no obligation to do so.

You are supposed to declare all variable argument list functions before using them; not declaring printf() properly leads to undefined behaviour (and one valid undefined behaviour is to work as expected).

You should be getting warnings about the undeclared functions if you compile in C99 mode (but Turbo C probably doesn't have a C99 mode).


Nit-picking:

[H]ow can a C program could run without using the standard header file?

All programs run without using any headers whatsoever. However, most programs are compiled using the standard headers to declare the standard functions, and the better programs ensure that all functions are declared before they are used (or are defined as static functions before they are used).

C99 requires this, though many compilers will allow errant programs to compile. However, the compilation should produce a diagnostic; the diagnostic may or may not lead to the compilation failing. In practice, it usually doesn't cause the compilation to fail, but it could and with some compilers (GCC, for example) you can force the compiler's hand (e.g. with GCC's -Werror=missing-prototypes -Werror=old-style-definition options).

他のヒント

When the language standard being applied pre-dates ISO C99, C does not require a function to be declared or defined before it is referenced.

However when the compiler encounters such a function call, it simply assumes that the function returns an int and that it takes an indeterminate number and type of parameters. This is called am implicit declaration. If you later declare the function, or call it with a different number of parameters or incompatible parameters, you may get a warning in some compilers that the second call does not match declaration implied by the first, but the ISO C89 standard treats the function as variadic [like printf()] where any number and type of parameters are allowed.

Moreover if the actual return value is not an int, any return value accepted and processed may not make much sense one way or another.

It is bad form to rely on an implicit declaration, and most compilers would issue a warning. If your compiler did not, you need to increase the warning level; those diagnostics are there to help improve your code quality. If you simply ignored the warning (any warning for that matter), then you shouldn't!

In C++ the rules are tighter and failure to declare or define a function before referencing it is an error, since it is necessary to allow function overloading.

A header file is nothing more than a listing of constants, preprocessor macros and function prototypes. The function prototypes tell C what arguments each function takes.

If the compiler sees a function being used without a corresponding prototype or function definition, it generates an implicit declaration of the form int func(). Since C functions are linked solely by name and not by function signature (as is the case with C++), the linker later locates the function definitions in the standard library.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top