C Function Isspace
## C Library Function - isspace()
The `isspace()` function is a built-in utility in the C standard library used to check whether a passed character is a whitespace character.
This function is declared in the `` header file.
---
## Description
The `isspace()` function determines if the argument `c` falls into the category of standard whitespace characters.
In the default `"C"` locale, the standard whitespace characters are:
| Character | Escape Sequence | ASCII Value (Hex) | Description |
| :--- | :--- | :--- | :--- |
| `' '` | `\x20` | `0x20` | Space (SPC) |
| `'\t'` | `\t` | `0x09` | Horizontal Tab (TAB) |
| `'\n'` | `\n` | `0x0a` | Newline / Line Feed (LF) |
| `'\v'` | `\v` | `0x0b` | Vertical Tab (VT) |
| `'\f'` | `\f` | `0x0c` | Form Feed (FF) |
| `'\r'` | `\r` | `0x0d` | Carriage Return (CR) |
---
## Declaration
Following is the declaration for the `isspace()` function:
```c
int isspace(int c);
```
---
## Parameters
* **`c`** -- This is the character to be checked. It is passed as an `int`, which must be representable as an `unsigned char` or be equal to `EOF` (End of File).
---
## Return Value
* The function returns a **non-zero value** (true) if `c` is a whitespace character.
* It returns **0** (false) if `c` is not a whitespace character.
---
## Code Examples
### Example 1: Basic Usage
The following example demonstrates how to use `isspace()` to check individual characters.
```c
#include
#include
int main()
{
int var1 = 't';
int var2 = '1';
int var3 = ' ';
if (isspace(var1))
{
printf("var1 = |%c| is a whitespace character\n", var1);
}
else
{
printf("var1 = |%c| is NOT a whitespace character\n", var1);
}
if (isspace(var2))
{
printf("var2 = |%c| is a whitespace character\n", var2);
}
else
{
printf("var2 = |%c| is NOT a whitespace character\n", var2);
}
if (isspace(var3))
{
printf("var3 = |%c| is a whitespace character\n", var3);
}
else
{
printf("var3 = |%c| is NOT a whitespace character\n", var3);
}
return 0;
}
```
**Output:**
```text
var1 = |t| is NOT a whitespace character
var2 = |1| is NOT a whitespace character
var3 = | | is a whitespace character
```
---
### Example 2: Counting Whitespace in a String
This practical example demonstrates how to use `isspace()` to count the total number of whitespace characters in a given string.
```c
#include
#include
int main()
{
char text[] = "Hello, World!\nWelcome to YouTip.\tEnjoy coding!";
int i = 0;
int whitespace_count = 0;
while (text != '\0')
{
if (isspace((unsigned char)text))
{
whitespace_count++;
}
i++;
}
printf("The total number of whitespace characters is: %d\n", whitespace_count);
return 0;
}
```
**Output:**
```text
The total number of whitespace characters is: 6
```
---
## Considerations
1. **Type Safety and Undefined Behavior**: The parameter `c` is of type `int`, but the value passed must be representable as an `unsigned char` (0 to 255) or equal to `EOF` (-1). Passing a negative value other than `EOF` results in **undefined behavior**. To prevent this, always cast `char` arguments to `unsigned char` when calling `` functions:
```c
char ch = 'a';
isspace((unsigned char)ch);
```
2. **Locale Dependency**: The behavior of `isspace()` depends on the current locale. In locales other than the default `"C"` locale, additional characters may be considered whitespace.
YouTip