954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Why To include Header Files in Cpp and Not in C Language

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

ashwinperti
Newbie Poster
22 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 

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

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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?

bumsfeld
Nearly a Posting Virtuoso
1,445 posts since Jul 2005
Reputation Points: 404
Solved Threads: 184
 

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

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You