header file "cctype" not required?

Dec 29, 2013 at 11:31pm
Hi all:

I tested a simple program from C++ Primer, 5th edition, Chapter 3. The example asks to convert the first word into uppercase. The code goes as below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <string>
#include <cctype>

using std::cin;		using std::cout;	using std::endl;
using std::string;

int main(void)
{
	// read a string
	string str;
	cout << "Please enter the string: ";
	getline(cin, str);

	// convert the first word into upper case
	for (string::size_type i = 0; i != str.size() && !isspace(str[i]); ++i)
		str[i] = toupper(str[i]);

	// display the result
	cout << "Converted string is: ";
	cout << str;
	
	cin.get();
	return 0;
}


The code is simple enough. But if I commented line 3, I suppose the program will not run since the function isspace is not defined. However, it also runs correctly.

I tested on MS Visual Studio 2010 professional and Code::blocks 13.12. All these two can run the program without trouble.

Can anyone explain? Thanks.
Dec 29, 2013 at 11:35pm
the standard library headers are allowed to include each other (and even parts of each other) arbitrarily: it's actually quite common that some #include's may be omitted, but the resulting code is non-portable.
Dec 30, 2013 at 9:29pm
Thanks for the explanation. I believe it's still a good habit to include all the corresponding headers at the beginning, even not required for some compiler.
Topic archived. No new replies allowed.