944,192 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4406
  • C++ RSS
Sep 24th, 2005
0

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

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ashwinperti is offline Offline
22 posts
since Aug 2005
Sep 24th, 2005
0

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

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Sep 24th, 2005
0

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

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Sep 25th, 2005
0

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

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?
Reputation Points: 404
Solved Threads: 180
Nearly a Posting Virtuoso
bumsfeld is offline Offline
1,422 posts
since Jul 2005
Sep 25th, 2005
0

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

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: C++ to .BIN Compiler help
Next Thread in C++ Forum Timeline: Binary Code





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC