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

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2005
Posts: 22
Reputation: ashwinperti is an unknown quantity at this point 
Solved Threads: 0
ashwinperti ashwinperti is offline Offline
Newbie Poster

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

 
0
  #1
Sep 24th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,660
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 724
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

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

 
0
  #2
Sep 24th, 2005
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,407
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1468
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

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

 
0
  #3
Sep 24th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,221
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 137
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso

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

 
0
  #4
Sep 25th, 2005
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?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,660
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 724
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

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

 
0
  #5
Sep 25th, 2005
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC