When submitting a solution in C++, please select either C++14 (GCC 6-32) or C++17 (GCC 7-32) as your compiler. ×

ShubhamAvasthi's blog

By ShubhamAvasthi, history, 4 years ago, In English

I tested the following code with three different compilers and saw some peculiar results. First of all, I am not sure why the program compiles (no errors with isalpha) indicating that cctype/ctype.h is being automatically included allowing the compilation of the program.

As far as I know and according to cplusplus.com, neither cctype namespace nor ctype.h header file is amongst the automatically included header files for iostream. Also, iostream does not inherit from cctype/ctype.h.

#include <iostream>
using namespace std;

int main()
{
	cout<<isalpha('a');
}

On my local machine with MinGW (32 bit), it prints 2.

On Codeforces compiler, it prints 2.

On ideone, it prints 1024.

All the above compilations were done for C++14.

It would be great if someone could share the reason for this behaviour.

  • Vote: I like it
  • +24
  • Vote: I do not like it

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by ShubhamAvasthi (previous revision, new revision, compare).

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by ShubhamAvasthi (previous revision, new revision, compare).

»
4 years ago, # |
Rev. 3   Vote: I like it +5 Vote: I do not like it

Regarding the output value:

I have gone through the same mistake once. The actual reason is here: http://www.cplusplus.com/reference/cctype/isalpha/?kw=isalpha

Unlike you would expect, it doesn't return a boolean value, but returns some integer value that is some non-0 value in case the input char is an alphabet.