isblank() Function
The isblank()
function in C determines if a given character is a blank character. It is primarily used to identify characters used to separate words in a line of text, such as the space (‘ ‘) and tab (‘\t’) characters in the standard “C” locale.
Syntax of isblank()
int isblank(int c);
Parameters
Parameter | Description |
---|---|
c | An integer representing the character to be checked, typically cast from a char or EOF. |
This function is standardized in C99 and, in C++11, a locale-specific template version exists. In the standard “C” locale, isblank()
considers the tab character ('\t'
) and the space character (' '
) as blank characters.
Return Value
The function returns a nonzero value (true) if the character is a blank character, and zero (false) otherwise.
Examples for isblank()
Example 1: Basic Check for a Space Character
This example demonstrates how to check if a single character, specifically a space, is considered blank using isblank()
.
Program
#include <stdio.h>
#include <ctype.h>
int main() {
int ch = ' ';
if (isblank(ch)) {
printf("The character is blank.\n");
} else {
printf("The character is not blank.\n");
}
return 0;
}
Explanation:
- A character variable is initialized with a space character (
' '
). - The
isblank()
function checks if this character qualifies as a blank. - Since a space is a blank character in the standard “C” locale, the program outputs that the character is blank.
Program Output:
The character is blank.
Example 2: Checking a Tab Character as Blank
This example shows how the isblank()
function identifies a tab character as blank in the standard “C” locale.
Program
#include <stdio.h>
#include <ctype.h>
int main() {
int ch = '\t';
if (isblank(ch)) {
printf("The character is a tab and is considered blank.\n");
} else {
printf("The character is not considered blank.\n");
}
return 0;
}
Explanation:
- A character variable is initialized with the tab character (
'\t'
). - The
isblank()
function checks if the tab is classified as a blank character. - In the standard “C” locale, the tab is considered blank, so the program outputs that the character is a tab and is considered blank.
Program Output:
The character is a tab and is considered blank.
Example 3: Scanning a String for Blank Characters
This example checks each character in a string to determine which characters are considered blank using isblank()
.
Program
#include <stdio.h>
#include <ctype.h>
int main() {
char text[] = "Hello\t World!";
int i = 0;
while (text[i] != '\0') {
if (isblank(text[i])) {
printf("'%c' is blank.\n", text[i]);
} else {
printf("'%c' is not blank.\n", text[i]);
}
i++;
}
return 0;
}
Explanation:
- A string containing letters, a tab, and a space is defined.
- The program iterates through each character in the string.
- The
isblank()
function checks if each character is a blank. - A message is printed for each character indicating whether it is blank or not.
Program Output:
'H' is not blank.
'e' is not blank.
'l' is not blank.
'l' is not blank.
'o' is not blank.
' ' is blank.
' ' is blank.
'W' is not blank.
'o' is not blank.
'r' is not blank.
'l' is not blank.
'd' is not blank.
'!' is not blank.