Like Tree1Likes
  • 1 Post By jimblumberg

header files

This is a discussion on header files within the C++ Programming forums, part of the General Programming Boards category; Hi Please have a look on the following header files. Code: #include <iostream> #include <cmath> #include <cstdlib> #include <windows.h> #include ...

  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,866
    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
    21,636
    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>.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,261
    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%.

  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
    2,708
    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
    Elysia likes this.

  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, 10:46 AM
  3. Replies: 4
    Last Post: 12-14-2005, 01: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, 08:35 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21