C++ tolower() Function

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

The cctype header file contains the definition of the tolower C++ function. If a character is passed in the function and it is an uppercase letter, then the tolower C++ method turns the uppercase letter into an equivalent lowercase letter.

Syntax of C++ tolower()

Syntax to use the tolower C++ method in a C++ program:

Declared in the cctype header file as:

Parameters of C++ tolower()

The required parameter for the tolower C++ function is a character, which has to be changed into an equivalent lowercase character.

The character parameter is auto-type-casted to the int type (ASCII value of the character) in the tolower C++ method.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Return Value of C++ tolower()

tolower C++ method returns:

  • For Alphabet argument (a-z, A-Z): Returns the ASCII value for the argument's lowercase equivalent character.
  • For Non-alphabets: Returns the argument's ASCII value directly. Non-alphabets can be of type special characters (%, &, @, etc.), numeric values (1, 2, 6, etc.), or anything apart from the alphabet.

Undefined Behavior of tolower() Function

The behaviour of the tolower() C++ function is undefined if the argument's value is neither an unsigned char nor equal to EOF.

Examples of tolower C++ Function

Let's look at the C++ example programs implementation of the tolower C++ function

tolower() With Type Conversion

We are converting two alphabets from uppercase to lowercase using the tolower C++ method, while two of the non-alphabets remain unchanged in the below C++ program.

Output:

Explanation: In the above program, we have declared and initialized four char variables. We are using the tolower C++ method in the above program to convert the uppercase characters into lowercase characters. We have printed the lowercase characters by typecasting the return value of the tolower() function to the char type as the tolower() function returns the ASCII codes for the equivalent lowercase characters.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

tolower() Without Type Conversion

We are converting four alphabets from uppercase to lowercase using the tolower C++ method, but

Output:

Explanation: we have declared and initialized four char variables in the above program. We are using the tolower C++ method to convert the uppercase characters into lowercase characters, but we are not type-casting the value returned by the tolower function, i.e., the equivalent ASCII value. So, the output we get is the equivalent ASCII code for the corresponding char variable values.

tolower() With String

In the C++ program below, we convert a whole string (char array) into a lowercase string using the tolower C++ function.

Output:

Explanation: we have declared and initialized a char array in the above program. We are using the tolower C++ method and for loop in the above program to convert all the uppercase characters of the string into lowercase characters. We have printed the mixed upper and lowercase string and the complete lowercase string in the output.

How does C++ tolower() Work?

According to the cctype header file, the function prototype for tolower() is:

The character parameter ch is transformed into an int value, the lowercase character’s ASCII value.

The tolower() C++ function returns the lowercase character's ASCII code, so it needs to be type-casted to char to print it in the output.

Conclusion

  • We have to include the cctype header file to use the tolower C++ method in a C++ program.
  • The tolower C++ method takes one character parameter and converts the uppercase character to an equivalent lowercase character.
  • The behaviour of the tolower() C++ function is undefined if the argument's value is neither an unsigned char nor equal to EOF.
  • The return value of the tolower() C++ function must be type-casted first to char since it returns the lowercase character's ASCII code.

Read More:

Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more