Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST TOOLS

C Tutorial

C HOME C Intro C Get Started C Syntax C Output C Comments C Variables C Data Types C Type Conversion C Constants C Operators C Booleans C If...Else C Switch C While Loop C For Loop C Break/Continue C Arrays C Strings C User Input C Memory Address C Pointers

C Functions

C Functions C Function Parameters C Pass By Reference C Scope C Function Declaration C Functions Challenge C Math Functions C Inline Functions C Recursion C Function Pointers

C Files

C Create Files C Write To Files C Read Files

C Structures

C Structures C Structs Challenge C Nested Structures C Arrays of Structs C Structs & Pointers C Unions C typedef C Struct Padding

C Enums

C Enums

C Memory

C Memory Management C Allocate Memory C Access Memory C Reallocate Memory C Deallocate Memory C Structs and Memory C Memory Example C Memory Mistakes

C Errors

C Errors C Error Challenge C Debugging C NULL C Error Handling C Input Validation

C Problem Solving

C Problem Solving C Build the Logic C Edge Cases & Errors C Test Cases

C More

C Date C Random Numbers C Macros C Organize Code C Storage Classes C Bitwise Operators C Fixed-width Integers C Command-Line Args

C DSA

C DSA Intro C Searching C Sorting C Stacks C Queues C Linked Lists C Trees C Hash Tables C Big O

C Projects

C Projects

C Cert

C Certificate

C Reference

C Reference C Keywords C <stdio.h> C <stdlib.h> C <string.h> C <math.h> C <ctype.h> C <time.h>

C Examples

C Examples C Real-Life Examples C Exercises C Quiz C Code Challenges C Practice Problems C Compiler C Syllabus C Study Plan C Interview Q&A

C Character Functions


Share

Character Functions

When you create a password on a website, it often checks the characters you type: Is there a digit? Is there an uppercase letter?

In C, you can check and change single characters with the character functions in the <ctype.h> header file. To use them, include the header in your program:

#include <ctype.h>

For example, isdigit() checks if a character is a digit (0 to 9):

Example

char c = '7';

if (isdigit(c)) {
  printf("%c is a digit\n", c);
}
Try it Yourself »

The functions that check a character return a non-zero value (true) if the answer is yes, and 0 (false) if not. That is why you can use them directly in an if statement.


Check Characters

Here are the most common functions to check a character:

Function Returns true if the character is Examples
isalpha() a letter 'a', 'Z'
isdigit() a digit '0', '7'
isalnum() a letter or a digit 'a', '7'
isupper() an uppercase letter 'A', 'Z'
islower() a lowercase letter 'a', 'z'
isspace() white space, like a space, a tab or a new line ' ', '\n'

This example counts the letters and digits in a string. It loops through the string (see Strings and Loops) and checks each character:

Example

char text[] = "Room 42";
int letters = 0;
int digits = 0;
int i;

for (i = 0; i < strlen(text); i++) {
  if (isalpha(text[i])) {
    letters++;
  } else if (isdigit(text[i])) {
    digits++;
  }
}

printf("Letters: %d\n", letters);
printf("Digits: %d\n", digits);
Try it Yourself »

The space in "Room 42" is not a letter or a digit, so it is not counted.


Change Characters

Use toupper() to change a letter to uppercase, and tolower() to change it to lowercase:

Example

printf("%c\n", toupper('a'));  // A
printf("%c\n", tolower('B'));  // b
printf("%c\n", toupper('7'));  // 7
Try it Yourself »

Characters that are not letters, like '7', are not changed.

To change a whole string, change one character at a time in a loop:

Example

char text[] = "Hello World";
int i;

for (i = 0; i < strlen(text); i++) {
  text[i] = toupper(text[i]);
}

printf("%s\n", text);
Try it Yourself »

Real-Life Example

Check if a password has at least one uppercase letter and one digit:

Example

char password[] = "Secret42";
int hasUpper = 0;
int hasDigit = 0;
int i;

for (i = 0; i < strlen(password); i++) {
  if (isupper(password[i])) {
    hasUpper = 1;
  }
  if (isdigit(password[i])) {
    hasDigit = 1;
  }
}

if (hasUpper && hasDigit) {
  printf("The password is OK\n");
} else {
  printf("The password needs an uppercase letter and a digit\n");
}
Try it Yourself »

Click "Try it Yourself", change the password to "secret", and see what happens.


Complete Character Reference

For a complete reference of character functions, go to our C <ctype.h> Library Reference.


Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.