Thread: header files

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    254

    header files

    Hi

    Please have a look on the following header files.

    Code:
    #include <iostream>
    #include <cmath>
    #include <cstdlib>
    #include <windows.h>
    #include <conio.h>
    #include <stdlib.h>
    A header file is a pointer to a library file which contains all the functions which you want to use. e.g. <cmath> header is to include library file which contains all math related functions.

    <iostream> is used for functions such as cin and cout.

    <windows.h> is used for windows related activities such as system("cls") and changing cosole's color etc.

    The header files with ".h" extension are not standard header files, at least this is what I have been told. Some of these header files come from C.
    I'm confused about the header files which have "c" as prefix such as <cmath>. Why is "c" necessary? Doesn't C++ have its own math related header file <math>?

    Please help me and please correct me wherever I'm grossly wrong. Thanks.

    Regards
    Jackson
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    My friend, there are some header files which are either deprecated, or scheduled to be deprecated in C++. As for using the winAPI, the suggested system for C++ is going to be QT, as is it designed to be standardized (e.g. cross platform). As for specific header files we would need to see a case by case basis, as things change and teachers do not.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by jackson6612
    A header file is a pointer to a library file which contains all the functions which you want to use. e.g. <cmath> header is to include library file which contains all math related functions.
    I would not say "pointer" since the header file does not refer to the "library file" (which is not necessarily in a separate library, and there is no requirement for a header file to have a corresponding source file).

    Quote Originally Posted by jackson6612
    <iostream> is used for functions such as cin and cout.
    They are actually global input/output stream objects, not functions, but yes.

    Quote Originally Posted by jackson6612
    <windows.h> is used for windows related activities such as system("cls") and changing cosole's color etc.
    No, it pertains to the Windows API, not (specifically) to functionality involving a console window. The system function is declared in <cstdlib>.

    Quote Originally Posted by jackson6612
    The header files with ".h" extension are not standard header files, at least this is what I have been told. Some of these header files come from C.
    I'm confused about the header files which have "c" as prefix such as <cmath>. Why is "c" necessary?
    There may be an exception here and there, but standard C headers are also standard C++ headers, but they are deprecated in C++. The versions with a c prefix are not deprecated. The difference is mainly that various names declared in the std namespace in the c prefix versions are declared in the global namespace instead in the original version from C.

    Quote Originally Posted by jackson6612
    Doesn't C++ have its own math related header file <math>?
    In effect, it does. It just happens to be based on <math.h> and is named <cmath>.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Thank you, AndrewHunter, laserlight.

    I would not say "pointer" since the header file does not refer to the "library file" (which is not necessarily in a separate library, and there is no requirement for a header file to have a corresponding source file).
    Isn't a header file a kind of signal that one is going to certain library functions? e.g. When one uses the header <cmath> it signals that math library file (which contains math related functions) is to be used. So, when code is compiled the compiler can fetch the necessary files.

    No, it pertains to the Windows API, not (specifically) to functionality involving a console window. The system function is declared in <cstdlib>.
    You mean that commands such system("cls"), system("pause"), etc. have nothing to do with the header <windows.h>?

    There may be an exception here and there, but standard C headers are also standard C++ headers, but they are deprecated in C++. The versions with a c prefix are not deprecated. The difference is mainly that various names declared in the std namespace in the c prefix versions are declared in the global namespace instead in the original version from C.
    I don't understand the meaning of "deprecated" in this context. Would you please elaborate a little?

    Is there a time related header <time.h>? Assuming there is one, what does it do?

    Thank you for all the help.

    Regards
    Jackson
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by jackson6612 View Post
    Isn't a header file a kind of signal that one is going to certain library functions? e.g. When one uses the header <cmath> it signals that math library file (which contains math related functions) is to be used. So, when code is compiled the compiler can fetch the necessary files.
    All a header file provides is information so the compiler knows how to call functions and use types declared in those headers.

    It is not a signal to the compiler. All #include <cmath> does is adjust the source code by placing the declarations and some definitions from <cmath> in place of the #include directive. It is pure text substitution. The preprocessor does that substitution before the compiler (a later phase of the build process) even scans the source code.

    Including a header like <cmath> does not link in libraries. A source file that #include's <cmath> will compile successfully if it uses (say) std::pow(), and an object file will be produced. That object file is often used as input to another program called the "linker". The simple act of #include'ing <cmath> does not ensure that the linker will search through some library in order to find std::pow().

    Quote Originally Posted by jackson6612 View Post
    You mean that commands such system("cls"), system("pause"), etc. have nothing to do with the header <windows.h>?
    system() is a function declared in <stdlib.h> in C, or <cstdlib> in C++. The meaning of particular arguments ("cls", "pause") is implementation defined, and have nothing to do with any header named <windows.h> ..... even if your target system is windows.

    Quote Originally Posted by jackson6612 View Post
    I don't understand the meaning of "deprecated" in this context. Would you please elaborate a little?
    Deprecated is a term used in standards. If a standard says something is deprecated, it is scheduled for removal from a future version of that standard. The C++ standard declares a number of things (including all of the standard C header files) as deprecated.

    Practically, if you care about future usability of your C++ code, you will avoid using deprecated features. If you have old code that uses deprecated features, you will modify that code to avoid using deprecated features.
    Quote Originally Posted by jackson6612 View Post
    Is there a time related header <time.h>? Assuming there is one, what does it do?
    One of the C standard headers is named <time.h>. The C++ standard supports <time.h> (and deprecates it) and also supports a header named <ctime> which gives - roughly - equivalent capability.

    I'll leave working out what that header specifies as an exercise. There is a lot that can be found using google.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by jackson6612 View Post
    Isn't a header file a kind of signal that one is going to certain library functions? e.g. When one uses the header <cmath> it signals that math library file (which contains math related functions) is to be used.
    #include, like #define, is a preprocessor instruction. They don't make it thru to the compiler, instead, the preprocessor removes them and alters the rest of your source. For example, the contents of a #define are substituted wherever the label occurs in the source. WRT to #include, the pre-processor literally inserts the contents of the file into the source in place of the #include line. You can see the effect of this if you place an #include somewhere halfway down a source file; function calls depending on it above that line will throw an error or warning. It's also why "include guards" are often necessary (notice, the pre-processor also pre-processes the contents of the include file).

    So that is all #include does. WRT standard functions, they are already built into your C++ library, which is one single rather large shared object. However, the prototypes for those functions are not included in your code, which is why they need to be inserted. This is why you can get away with using many standard library functions even if you don't include the header (particularly if the function returns an int, which is the default prototype the compiler assumes).

    So, when code is compiled the compiler can fetch the necessary files.
    Again, the compiler never sees "#include". It sees the contents of the file that the preprocessor has substituted.

    The standard library is automatically linked to your executable. However, there is an issue if the prototypes are for functions which are not in the standard library, but in some other shared library. These are the cases where you must explicitly instruct the linker, eg, with an "-l" flag to the compiler call (or selecting some option in an IDE menu). Usually, the C math library is like this; functions in <cmath> are not in the standard library; it has its own shared object which you must explicitly link in.

    You mean that commands such system("cls"), system("pause"), etc. have nothing to do with the header <windows.h>?
    No, "system" is part of the C/C++ standard. It literally calls another executable. Those commands are not compiled into your executable. Just their names. The use of <windows.h> illustrates something else about your C++ library, since it is not part of the standard but you do not need to link to anything else. This is because the library contains all the standard functions, plus some stuff that is not in it (such as platform specific calls). The standard says what must be in a complete and complaint C++ library, but it does not say it can't also have a bunch of other things in it.

    I don't understand the meaning of "deprecated" in this context. Would you please elaborate a little?
    I think the idea is that the C++ compiler should not be exposed to includes (as in, the file contents inserted by the preprocessor) that were not written with C++ in mind, which C library headers were not.

    Is there a time related header <time.h>? Assuming there is one, what does it do?
    In C++, <ctime>. It contains, eg, the function prototype for time() -- but this is an example of a function which will work ok without a header, because it returns an integer type.* Actually, a time_t, but close enough. <ctime> also include the typedef for time_t.

    * I am not recommending that as a practice, just that it illustrates how the library works in relation to its header files.
    Last edited by MK27; 09-20-2011 at 07:50 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    but this is an example of a function which will work ok without a header, because it returns an integer type. Actually, a time_t, but close enough. <ctime> also include the typedef for time_t.
    That may be true for C but not for C++. C++ does not allow "default" return values.

    Jim

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by jimblumberg View Post
    That may be true for C but not for C++. C++ does not allow "default" return values.

    Jim
    Ah. I hadn't noticed because, of course, I always take care to use the appropriate includes ; )
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. about #include files and header files
    By bulletbutter in forum C++ Programming
    Replies: 9
    Last Post: 04-18-2008, 10:24 AM
  2. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  3. Replies: 4
    Last Post: 12-14-2005, 02:21 PM
  4. include library header in header files
    By Raison in forum C++ Programming
    Replies: 6
    Last Post: 09-27-2004, 02:50 AM
  5. header files and code files..
    By CompiledMonkey in forum C++ Programming
    Replies: 4
    Last Post: 02-15-2002, 09:35 AM