0

okay, I got a few line of code

#include "stdafx.h"
#include "iostream"
#include "conio.h"
#include "string"
#include "windows.h"

environment is visualstudio 8 and I am learning OOP..

what I know so far is (From my previous background of learning so far)

  • using #include<iostream> will search a file iostream.h in standard include library
  • using #include"myHeader.h" will search a file myHeader.h in the directory where executing file is present, if not found, it will then go to search in standard include library

based on this, I am much confused, my questions are

  1. WHY qoutes are used with each header? I believe qoutes belong to header on local directory where executing code is present..while these files are in standard lib include folder?
  2. WHERE should I use qoutes or and where not?
  3. I can see .h extension with a few files, and Don't see it with others...Why so??

thanks for bearing these foolish questions..but confused..:( and even reading different sources makes me more confused,

Edit

Based on first answer,

Please guide me the correct and OPTIMAL way to include header files in my case (sequence, Qoutes and placing .h )

P.S Dont know why, but these files includes correctly, and does not give in error in compiler (visual studio 8)

Sam
  • 7,252
  • 16
  • 46
  • 65
Zaffar Saffee
  • 6,167
  • 5
  • 39
  • 77
  • 1
    `#include ` searches for a file called `iostream`, not `iostream.h`, unless the compiler is really strange. – chris Apr 29 '14 at 00:13
  • 1
    see question 3, where to put .h with file name? – Zaffar Saffee Apr 29 '14 at 00:16
  • 1
    @NewBee You should consider upgrading to a newer version of visual studio. There is an [express version of the newest release](http://www.microsoft.com/en-us/download/details.aspx?id=40787) you can download of microsoft's home page – danielschemmel Apr 29 '14 at 00:27

3 Answers3

2

Going through your questions one by one:

  1. Because whoever wrote this code made a mistake - when using quoted paths instead of angled brackets the search order is suboptimal for library headers. Each of your examples should probably use angled brackets instead.

  2. Whenever you use a library, you should use angled brackets and when you include your own headers you should use quotation marks. The only difference between the two is where the compiler will look for the files. A similar suggestion is actually part of the C++11 standard (16.2§7).

  3. Because those files do not have an extension. When writing #include <iostream>, the compiler will search for file named exactly iostream! (Note: This could be implemented differently in other implementations since headers do not technically have to be files at all.)
    Most people agree that your own headers should have a .h, .hpp or .hh solution - which one is up to personal preference, but should be used consistently.

Community
  • 1
  • 1
danielschemmel
  • 10,885
  • 1
  • 36
  • 58
  • 3
    Also, read this: http://stackoverflow.com/questions/901216/why-stl-header-files-have-no-extension – Vince Apr 29 '14 at 00:18
  • 1
    re "when you include your own headers you should use quotation marks", the ownership of the files has no possible relevance, and furthermore the advice about "a library" contradicts the advice based on ownership -- a self-contradiction. – Cheers and hth. - Alf Apr 29 '14 at 00:25
  • 2
    re "those files do not have an extension", a standard header is not necessarily a file. – Cheers and hth. - Alf Apr 29 '14 at 00:26
  • 1
    @gha.st the wording "header or source file" is used because a header is not necessarily a file. footnote 174 in §16.6.1.2 about headers explains this a bit more: "A header is not necessarily a source file, nor are the sequences delimited by < and > in header names necessarily valid source file names". Note however that notes are not normative (in the ISO sense of normative). What you say about the OP's use of MSVC to be "more importantly" is IMHO not so. The environment is only relevant for understanding the environment-specific headers, which you have not addressed. – Cheers and hth. - Alf Apr 29 '14 at 00:42
  • 1
    re "This suggestion is actually part of the C++11 standard", no it's not. However, there is a suggestion, new in C++11, about differentiating between C++ implementation headers and non-implementation headers. According to that one should include e.g. system headers or 3rd party library headers by using quotes, which is just as meaningless and counter-productive (risking picking up wrong headers) as making the choice based on ownership. Argh. Happily, notes are not normative, but still... Note however that that (bad) advice contradicts your "made a mistake" assertion. ;-) – Cheers and hth. - Alf Apr 29 '14 at 00:50
2

#include <iostream> includes a standard header. A standard header does not need to be a file. It can be retrieved from a database, or be hardwired in the compiler. But of course the usual implementation is that it is a file, and when it is, then it's most likely a file called just “iostream”, not a file called “iostream.h”.

The difference between quotes and angle brackets is that quotes perform an additional search, before (if that's not successful) doing exactly the same as with angle brackets.

In practice, by common convention, the additional search is in the including file's folder.


Do ignore advice about using one or the other form depending on whether files are your own or someone else's.

Use the tools that are suitable for the job at hand.

Community
  • 1
  • 1
Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
-1

actually as you said, it's fine to use the "" because when the compiler don't find the library in the project folder it will search for it in the standard library. so for these headers <> or "" will do the job just fine, but using <> for standard libraries result in a faster compilation time.

About the .h extension: the new C++ standard uses libraries without .h extension, so when you use <iostream.h> it will load the old standard library, when using <iostream> will load the new standard library. so I advise you to use

#include <iostream>
using namespace std;
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Sami EMAD
  • 11
  • 1
  • 3