In this guide, you will learn what is iscntrl() function is in C++ programming and how to use it with an example.
1. iscntrl() Function Overview
The iscntrl() function is a part of the C++ <cctype> library. This function checks if the passed character is a control character. Control characters are non-printable characters, with ASCII values between 0 to 31 and 127 (inclusive).
Signature:
int iscntrl(int ch);
Parameters:
- ch: The character to be checked. It's an int, but it is basically the ASCII value of the character.
2. Source Code Example
#include <iostream>
#include <cctype>
int main() {
char ch1 = '\n';
char ch2 = 'A';
if(iscntrl(ch1)) {
std::cout << "ch1 is a control character." << std::endl;
} else {
std::cout << "ch1 is not a control character." << std::endl;
}
if(iscntrl(ch2)) {
std::cout << "ch2 is a control character." << std::endl;
} else {
std::cout << "ch2 is not a control character." << std::endl;
}
return 0;
}
Output:
ch1 is a control character. ch2 is not a control character.
3. Explanation
In the example above, we've used the iscntrl() function to check whether the character is a control character or not.
The character \n (newline) is a control character, hence for ch1, it returns true.
However, ch2 which contains the character 'A' is not a control character, so for ch2, it returns false.
Comments
Post a Comment