The ispunct function in C programming checks whether a character is a punctuation character or not. The default C locale classifies the characters !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
as punctuation.
It is declared in ctype.h
and takes one argument in the form of integer and returns the value of type int. If the character passed is a punctuation character, it returns a non-zero integer. If not, it returns 0.
Syntax of ispunct function in C:
//Syntax of ispunct int ispunct(int c);
Parameters:
c
=> character to classify
Return value:
Non-zero value
=> If the argument is a punctuation character.
0
=> If the argument is neither a punctuation character.
Note:
In the “C
” locale, ispunct returns true for every printing character for which neither isspace nor isalnum is true.
Example,
Input : 'a' Output : Zero Input : ';' Output : Non-zero value Input : 1 Output : Zero
C program to understand the working of ispunct function:
Consider the below code where I am passing different characters in the ispunct function. You can see the output of this function with different characters.
#include <stdio.h> #include <ctype.h> int main() { int c = 'A'; int ret = ispunct((unsigned char)c); printf("ispunct(%c) = %d\n", c, ret); c = '1'; ret = ispunct((unsigned char)c); printf("ispunct(%c) = %d\n", c, ret); c = ';'; ret = ispunct((unsigned char)c); printf("ispunct(%c) = %d\n", c, ret); c = '@'; ret = ispunct((unsigned char)c); printf("ispunct(%c) = %d\n", c, ret); return 0; }
Output:
ispunct(A) = 0
ispunct(1) = 0
ispunct(;) = 16
ispunct(@) = 16
C Program To Print all Punctuations:
Let’s C a C program to print default C punctuation characters. The term “punctuation
” means all printable characters that are neither alphanumeric
nor a space
. For example ‘@’, ‘$’, etc.
#include <stdio.h> #include <ctype.h> int main() { unsigned int i; printf("All punctuations in C: \n\n"); // looping through all ASCII characters for (i = 0; i <= 127; ++i) { if(ispunct(i)!= 0) { printf("%c ", i); } } printf("\n\n"); return 0; }
Output:
Note:
If the argument’s value (c) is neither representable as unsigned char not equal to EOF, the behavior of ispunct is undefined.
As we know the behavior of ispunct is undefined if the argument’s value is neither representable as unsigned char nor equal to EOF. So to use these functions safely with plain chars (or signed chars), the argument should first be converted to unsigned char. Because it is good to convert signed char to unsigned char before being assigned or converted to a larger signed type.
int my_ispunct(char ch) { return ispunct((unsigned char)ch); }
Recommended Post:
- tolower function in C.
- How to use the islower function in C?
- Use of iscntrl function in C.
- How to use isalpha function in C programming?
- Use isalnum function in C programming?
- How to use isdigit function in C programming?
- How to use sizeof operator in C.
- _Alignof or alignof Operator in C
- Alignment specifiers in C ( _Alignas).
- Function Specifiers in C.
- Type qualifiers in C.
- Punctuators in C.
- Elements of C language.
- C String Literals with Its Types
- C identifiers and naming rules.