I'm confused why isdigit and other functions in cctype header accepts int as a parameter, though they supposed to work with characters. To be able to work with any types of chars, including unicode?
Asked
Active
Viewed 72 times
0
-
2A character **is** an `int`. Beginners assume that it is a `char` because of the similar name. Almost (perhaps all) the C library functions that take or return a character use the `int` type. I suggest you experiment with `printf("%zu\n", sizeof 'c');` – Weather Vane Jul 08 '20 at 15:29
-
1Essentially so it can handle EOF. – Bathsheba Jul 08 '20 at 15:30
-
Probably because `EOF` is defined as `-1`. A `char` may not be `signed`. – Fiddling Bits Jul 08 '20 at 15:30
-
i removed the C tag, because even though it is inherited from C, `std::isdigit` is a C++ function from the C++ header `
`. You could ask similar for `isdigit` in C, but thats a different question (though with probably very similar answer ;) – 463035818_is_not_an_ai Jul 08 '20 at 15:33 -
The tags keep changing... but the title does say `std::` – Weather Vane Jul 08 '20 at 15:33
-
@WeatherVane before posting this question I was google phrases like "C++ char is int", but didn't find explanation about – DoehJohn Jul 08 '20 at 19:38
-
Sorry, my comment was about C before the tags changed. I can't comment on C++ but you might like to try the `printf("%zu\n", sizeof 'c');` mentioned. – Weather Vane Jul 08 '20 at 19:40
-
@WeatherVane I've tried. As expected, it gives 1, same for all "basic"(ANSII?) symbols. But for unicode characters like printf("%zu\n", sizeof 'が'); it's 4 – DoehJohn Jul 08 '20 at 19:42
-
When the tags were C and C++ someone replied to my comment "not in C++" but deleted it when the tag changed to C. It was then changed to C++. It would help if you tag the correct language to start with. C is not C++ and there is no C/C++ language. – Weather Vane Jul 08 '20 at 19:45