Sir,

I want to know why is it so that, in the Case of C Language there is no need to include stdio.h Header file. But when the same Program is saved with with .cpp extension it ask for the prototype.

and we have to include the stdio.h header file.

Why is this?

Ashwin Perti

Recommended Answers

All 4 Replies

>I want to know why is it so that, in the Case of C Language there is no
>need to include stdio.h Header file.
It's a (mis)feature in C89 to provide backward compatibility with K&R C. Don't take advantage of it though, you'll end up causing yourself more problems than you can handle. In C99 prototypes are required.

>Why is this?
In K&R C and C89, if a prototype is not supplied, a default function prototype is assumed so that the code will compile. Smart linkers will probably find the right object code and as long as the call is legal, everything may work. Note the terms 'probably' and 'may'. That means that you're doing something incredibly stupid and it's not guaranteed to work.

stdio.h contains function prototypes (declarations) and structure definitions from the standard C stream library. You cann't write either a C or C++ that uses any of those functions without including stdio.h. If the program doesn't use anything from it, then there is no need to include it.

Is it wastefull to throw in an extra header file that may not be needed. Does it add much to the size of the final file, or does the compiler catch it?

>Is it wastefull to throw in an extra header file that may not be needed.
Yes and no...and yes. It really depends on the compiler and the linker, but in general it's better practice to only include what you need.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.