Which Header Files Should I be Using?

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 1453 Views - Last Post: 26 June 2011 - 07:42 AM Rate Topic: -----

#1 Ty Meador  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 113
  • Joined: 23-May 11

Which Header Files Should I be Using?

Posted 25 June 2011 - 09:09 PM

i am getting mixed input on the header files that i should/shouldnt be using for my C++ programs. some people tell me to use <c....> and some tell me that the headers starting w/c are the "old c headers". i want to use the most up to date C++ headers, classes, and functions i can, so what is the truth? here is my template, if you have any more suggestions on how to improve it, they are much appreciated.
/*Header Files
==============*/
#include <iostream> 	//Required for i/o
#include <iomanip>  	//Required for i/o Manipulators
#include <string>       //Required for Strings
#include <cstdlib>      //Required for System Commands
#include <cmath>	   	//Required for Math Library Functions
#include <fstream>      //Required for ifstream && ofstream
#include <cctype>		/*Required for Character
						Analization Library Functions*/
using namespace std;
/*Programmer: 	Ty Meador
Name:
Purpose:
*/
/*Globals
========= */
//double PI = 3.1415926535897

/*Function Prototypes
======================*/




int main()
{

    return 0;
}




Is This A Good Question/Topic? 0
  • +

Replies To: Which Header Files Should I be Using?

#2 ishkabible  Icon User is offline

  • spelling expret
  • member icon





Reputation: 1618
  • View blog
  • Posts: 5,707
  • Joined: 03-August 09

Re: Which Header Files Should I be Using?

Posted 25 June 2011 - 09:25 PM

if it is a header from C then drop the .h and add "c" to the front of it.

#include <cmath>   //good, C++
#include <math.h>  //bad, unless your using C
#include <cctype>  //good
#include <ctype.h> //bad
#include <iostream>//this is a C++ standard library header, so there is no C at the front


Was This Post Helpful? 0
  • +
  • -

#3 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2247
  • View blog
  • Posts: 9,237
  • Joined: 18-February 07

Re: Which Header Files Should I be Using?

Posted 25 June 2011 - 09:31 PM

With C++ you want to use the standard headers. For access to the C libraries these have been prefixed with a 'c' and the .h has been dropped:

C header -> C++ header
#include <stdio.h> -> #include <cstdio>
#include <stdlib.h> -> #include <cstdlib>
#include <time.h> -> #include <ctime>
#include <ctype.h> -> #include <cctype>

You can find a list of the standard headers here: http://www.cplusplus.com/reference/

You program template looks fine to me.
Was This Post Helpful? 0
  • +
  • -

#4 Ty Meador  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 113
  • Joined: 23-May 11

Re: Which Header Files Should I be Using?

Posted 25 June 2011 - 09:54 PM

These are the headers im (now) using, are these correct? assume for now that all i want are the headers files here, along with all the C++ standard ones. i looked at the link above, and it didnt help me, i didnt understand the way it was arranged. (there are some more specific questions below.
/*Standard Header Files
=======================*/
#include <iostream> 	//Required for i/o
#include <iomanip>  	//Required for i/o Manipulators WY DO I NOT HAVE TO PUT .H HERE FOR THESE TO COMPILE?
#include <string>       //Required for Strings
#include <cmath>	//Required for Math Library Functions IS THIS C++ OR C?
#include <fstream>      //Required for ifstream && ofstream

/*C Header Files
==================*/
#include <cstdio>        //Required for
#include <cstdlib>       //Required for System Commands ALL OF THESE ARE C HEADERS RIGHT?
#include <ctime>         //Required for time
#include <cctype>        //Required for Character Library Functions


Was This Post Helpful? 0
  • +
  • -

#5 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: Which Header Files Should I be Using?

Posted 25 June 2011 - 09:55 PM

cmath is a C header. The rest are correct.
You don't need .h because the files aren't .h; they have no extension. Find the iostream file on your computer if you like.

This post has been edited by PlasticineGuy: 25 June 2011 - 09:56 PM

Was This Post Helpful? 0
  • +
  • -

#6 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2247
  • View blog
  • Posts: 9,237
  • Joined: 18-February 07

Re: Which Header Files Should I be Using?

Posted 25 June 2011 - 10:03 PM

cmath is the C++ standard header for the C math.h -- i.e. cmath is the correct header.

Really @PlasticineGuy I don't mean to pick on you but really please check a little before you jump to post something. I gave a link to the standard headers.


lol -- Sorry... maybe I should read the full thread before I post!!! Sorry I was wrong you were right!
Was This Post Helpful? 1
  • +
  • -

#7 heyoman1  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 64
  • View blog
  • Posts: 735
  • Joined: 30-November 09

Re: Which Header Files Should I be Using?

Posted 25 June 2011 - 10:12 PM

All of C's standard libraries have been ported over to C++, and you can tell the difference in this in that C's libraries do not have "c" in front of them. Examples:

#define -> (ported to)

math.h -> cmath
string.h -> string
ctype -> cctype
time.h -> ctime
stdio.h -> cstdio
stdlib.h -> cstdlib

All major compilers will allow you to just use (example) #include <cmath>, as it helps differentiate it with it's C counterpart.


EDIT: WHOOPS.

You DON'T use a C++ header in place of C headers. You have #include <ctime> as a C header, but it's really supposed to be #include <time.h>.

This post has been edited by heyoman1: 25 June 2011 - 10:13 PM

Was This Post Helpful? 0
  • +
  • -

#8 Ty Meador  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 113
  • Joined: 23-May 11

Re: Which Header Files Should I be Using?

Posted 25 June 2011 - 10:19 PM

View Postheyoman1, on 25 June 2011 - 10:12 PM, said:

All of C's standard libraries have been ported over to C++, and you can tell the difference in this in that C's libraries do not have "c" in front of them. Examples:

#define -> (ported to)

math.h -> cmath
string.h -> string
ctype -> cctype
time.h -> ctime
stdio.h -> cstdio
stdlib.h -> cstdlib

All major compilers will allow you to just use (example) #include <cmath>, as it helps differentiate it with it's C counterpart.


EDIT: WHOOPS.

You DON'T use a C++ header in place of C headers. You have #include <ctime> as a C header, but it's really supposed to be #include <time.h>.

I couln't make heads or tales of the link earlier. and, anything with a c in front of it is a c header file, right? becuase i want the C++ headers only if i can help it. let me ask it this way, and i will enlarge this so, perhaps people will see it before "answering" my question, what SHOULD my headers look like, if not this
/*Standard Header Files
=======================*/
#include <iostream> 	//Required for i/o
#include <iomanip>  	//Required for i/o Manipulators
#include <string>       //Required for Strings
#include <cstdlib>      //Required for System Commands
#include <cmath>	   	//Required for Math Library Functions
#include <fstream>      //Required for ifstream && ofstream

/*C Header Files
==================*/
#include <cstdio>        //Required for
#include <cstdlib>       //Required for System Commands
#include <ctime>         //Required for time
#include <cctype>        //Required for Character Library Functions

Was This Post Helpful? 0
  • +
  • -

#9 ishkabible  Icon User is offline

  • spelling expret
  • member icon





Reputation: 1618
  • View blog
  • Posts: 5,707
  • Joined: 03-August 09

Re: Which Header Files Should I be Using?

Posted 25 June 2011 - 10:32 PM

you have them right... in C++ the standard library headers shouldn't have a .h after them, simple as that.
Was This Post Helpful? 2
  • +
  • -

#10 Ty Meador  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 113
  • Joined: 23-May 11

Re: Which Header Files Should I be Using?

Posted 25 June 2011 - 10:45 PM

View Postishkabible, on 25 June 2011 - 10:32 PM, said:

you have them right... in C++ the standard library headers shouldn't have a .h after them, simple as that.

ishkabible, you have answered a few of my questions before (sorry im so ignorant, im a senior in high school, in my second year of programming. i love the language, but still have alot to learn), anyway, you always give me the answer i am looking for. simple and strait forward. my *new template is this (thanks to you)

/*Standard Header Files
=======================*/
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
using namespace std;
/*Programmer: 	Ty Meador
Name:
Purpose:
*/
/*Globals
========= */
//double PI = 3.1415926535897

/*Function Prototypes
======================*/




int main()
{

    return 0;
}


Have you anymore suggestions as to how i could improve this template anymore?
Was This Post Helpful? 0
  • +
  • -

#11 ishkabible  Icon User is offline

  • spelling expret
  • member icon





Reputation: 1618
  • View blog
  • Posts: 5,707
  • Joined: 03-August 09

Re: Which Header Files Should I be Using?

Posted 25 June 2011 - 10:47 PM

ya..just one looking at a glance. do you really need all those headers?

edit: also it appaers as though from your comments that you plan to declare a global variable 'PI'. you should make that a constant.

const double PI = 3.1415926535897;

This post has been edited by ishkabible: 25 June 2011 - 10:53 PM

Was This Post Helpful? 0
  • +
  • -

#12 Ty Meador  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 113
  • Joined: 23-May 11

Re: Which Header Files Should I be Using?

Posted 25 June 2011 - 10:53 PM

View Postishkabible, on 25 June 2011 - 10:47 PM, said:

ya..just one looking at a glance. do you really need all those headers?

well, (from what i gather) it doesn't hurt anything having them, it doesn't slow down the computer when running the program, so having them all means (call me lazy) that i don't have to remember them XD
Was This Post Helpful? 0
  • +
  • -

#13 ishkabible  Icon User is offline

  • spelling expret
  • member icon





Reputation: 1618
  • View blog
  • Posts: 5,707
  • Joined: 03-August 09

Re: Which Header Files Should I be Using?

Posted 25 June 2011 - 10:56 PM

i still view it as kinda bad practice personally. you should know what headers are needed for what and just include them as you need them if you ask me. but like you said no harm in terms of your code will come from it. i think it is somewhat detrimental to your learning however.
Was This Post Helpful? 2
  • +
  • -

#14 Ty Meador  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 113
  • Joined: 23-May 11

Re: Which Header Files Should I be Using?

Posted 25 June 2011 - 10:59 PM

View Postishkabible, on 25 June 2011 - 10:56 PM, said:

i still view it as kinda bad practice personally. you should know what headers are needed for what and just include them as you need them if you ask me. but like you said no harm in terms of your code will come from it. i think it is somewhat detrimental to your learning however.

I appreciate your concern for my learning. and your point is dually noted. i will keep them in my template, but use it only as a reference. as of right now, im just trying to get the logic behind the OOP and GUI paradigms. Just how detrimental do you think that choice would be?
Was This Post Helpful? 0
  • +
  • -

#15 ishkabible  Icon User is offline

  • spelling expret
  • member icon





Reputation: 1618
  • View blog
  • Posts: 5,707
  • Joined: 03-August 09

Re: Which Header Files Should I be Using?

Posted 25 June 2011 - 11:06 PM

not very, it's safe to keep doing i guess. i just think it prevents you from learning which headers are for what. it also means the unnecessary code will be compiled, not the best thing in the world. it's not really a big deal, just not my style.

This post has been edited by ishkabible: 25 June 2011 - 11:08 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2