For working professionals
For fresh graduates
More
5. Array in C
13. Boolean in C
18. Operators in C
33. Comments in C
38. Constants in C
41. Data Types in C
49. Double In C
58. For Loop in C
60. Functions in C
70. Identifiers in C
81. Linked list in C
83. Macros in C
86. Nested Loop in C
97. Pseudo-Code In C
100. Recursion in C
103. Square Root in C
104. Stack in C
106. Static function in C
107. Stdio.h in C
108. Storage Classes in C
109. strcat() in C
110. Strcmp in C
111. Strcpy in C
114. String Length in C
115. String Pointer in C
116. strlen() in C
117. Structures in C
119. Switch Case in C
120. C Ternary Operator
121. Tokens in C
125. Type Casting in C
126. Types of Error in C
127. Unary Operator in C
128. Use of C Language
The toupper() function in C is used to convert a lowercase character to its uppercase form. It belongs to the <ctype.h> library. You can use it when working with individual characters that need to be capitalized during input processing or output formatting.
Explore Online Software Engineering Courses from top universities.
This article will explain what the toupper() function in C does, its syntax, and where it fits in real programs. We will also look at examples from basic to advanced levels. Along the way, we will discuss when to use it, common mistakes, and how to avoid them. By the end, you’ll have a complete understanding of how this function works.
If you're new to C, consider going through our Introduction to C tutorial for better clarity before moving ahead.
The toupper() function in C is a character-handling function that converts a lowercase alphabet to its uppercase equivalent. It is defined in the <ctype.h> header file. If the input character is already in uppercase or not a letter, the function simply returns it without any change.
You can use this function when you need to convert user input, standardize data for comparison, or prepare formatted output. It works on a single character at a time and is especially useful when looping through strings.
Must Explore: Static Function in C: Definition, Examples & Real-World Applications
Here’s the syntax of toupper() in C:
int toupper(int ch);
Note: The function accepts only a single parameter - ch (This is the character to be converted. It must be representable as an unsigned char, or equal to EOF.)
Return Value
Also read the strlen() Function in C article!
In this section, we will explore how the toupper() function in C works through practical examples. The examples are divided into three levels - basic, intermediate, and advanced.
This basic example demonstrates how to convert a single lowercase character to uppercase using the toupper() function in C.
#include <stdio.h>
#include <ctype.h>
int main() {
char ch = 'm';
// Use toupper() to convert to uppercase
char upper = toupper(ch);
// Print the results
printf("Original: %c\n", ch);
printf("Uppercase: %c\n", upper);
return 0;
}
Output:
Original: m
Uppercase: M
Explanation:
In this example, we will convert each character of a string to uppercase. This helps when you want to normalize user input before comparison.
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main() {
char str[] = "hello world";
printf("Original string: %s\n", str);
// Convert all characters to uppercase using a loop
for (int i = 0; str[i] != '\0'; i++) {
str[i] = toupper(str[i]); // Convert each character
}
printf("Uppercase string: %s\n", str);
return 0;
}
Output:
Original string: hello world
Uppercase string: HELLO WORLD
Explanation:
This advanced example reads user input, converts lowercase characters to uppercase, and skips other characters (like numbers or symbols).
#include <stdio.h>
#include <ctype.h>
int main() {
char input[100];
printf("Enter a string: ");
fgets(input, sizeof(input), stdin); // Read input from user
printf("Converted string: ");
// Loop through each character
for (int i = 0; input[i] != '\0'; i++) {
if (islower(input[i])) {
putchar(toupper(input[i])); // Convert and print
} else {
putchar(input[i]); // Print other characters as-is
}
}
return 0;
}
Sample Output:
Enter a string: upGrad 123!
Converted string: UPGRAD 123!
Explanation:
Must Explore: Enumeration (or enum) in C article!
Below are key situations where this function becomes useful:
Also Read: Strcpy in C: How to Copy Strings with Examples article!
Example: Normalizing Input Before Comparison
Here’s a simple program that compares two characters after converting them to uppercase.
#include <stdio.h>
#include <ctype.h>
int main() {
char a = 'y';
char b = 'Y';
// Convert both characters to uppercase
if (toupper(a) == toupper(b)) {
printf("Characters match (case-insensitive).\n");
} else {
printf("Characters do not match.\n");
}
return 0;
}
Output:
Characters match (case-insensitive).
Explanation:
Must Explore the strcat() Function in C: Syntax, Examples, and Common Errors article!
While the toupper() function in C is simple and easy to use, beginners often make mistakes that lead to unexpected results. Below are common issues you should avoid when using this function.
1. Passing a string instead of a character:
#include <stdio.h>
#include <ctype.h>
int main() {
// Incorrect: passing a string instead of a character
printf("%c\n", toupper("a")); // ❌ wrong usage
return 0;
}
Correct Way:
printf("%c\n", toupper('a')); // Correct usage
2. Not including the correct header file
// Incorrect usage: missing header
// #include <ctype.h> is missing
int main() {
char ch = 'g';
printf("%c", toupper(ch));
return 0;
}
Always include:
#include <ctype.h> // needed for toupper()
3. Passing characters outside valid ASCII range
#include <stdio.h>
#include <ctype.h>
int main() {
int ch = -5; // Invalid input
printf("%c\n", toupper(ch)); // may cause unexpected output
return 0;
}
4. Assuming it modifies the original variable
#include <stdio.h>
#include <ctype.h>
int main() {
char ch = 'k';
toupper(ch); // result is not stored
printf("%c\n", ch); // Output will still be 'k'
return 0;
}
Store the result properly:
ch = toupper(ch); // modifies the variable with uppercase version
5. Not checking the character type before converting
char ch = '9';
ch = toupper(ch); // '9' stays the same, but operation is unnecessary
Here are some tips:
The toupper() function in C is a simple yet powerful tool used to convert lowercase letters to uppercase. It plays a key role in string formatting, input sanitization, and data validation in C programs. This function is part of the ctype.h library and works on a character-by-character basis.
Throughout this article, we explored the toupper() function in C in great detail. From its syntax and usage to multiple real-life code examples, we covered all essential aspects. We also looked at when and why you should use it, along with common mistakes developers often make.
The toupper() function in C is used to convert a lowercase letter to its uppercase equivalent. It accepts a single character as input and returns its uppercase form if it's a lowercase alphabet; otherwise, it returns the character unchanged.
To use the toupper() function in C, you must include the ctype.h header file. This file provides several character handling functions, including toupper(), tolower(), isalpha(), and more.
No, the toupper() function in C only affects lowercase alphabetic characters. If you pass a digit, punctuation mark, or special symbol, the function returns it without any modification.
The return type of the toupper() function is int. Although it returns a character, it is promoted to an integer to match the function's signature. The returned value can safely be used as a char.
If you pass an already uppercase letter to the toupper() function, it returns the same character. The function checks the input and only converts characters that are in the lowercase ASCII range.
No, the toupper() function in C only processes one character at a time. To convert an entire string, you must loop through each character and apply toupper() individually. This approach ensures all lowercase letters are changed to uppercase.
If you pass a null character ('\0') to the toupper() function in C, it simply returns the same character. Since it's not a lowercase alphabet, the function doesn't attempt any conversion.
Yes, the toupper() function in C is safe to use with string literals as long as you're not modifying the literal directly. Loop through the string, apply toupper() to each character, and store the result in a new array.
The toupper() function in C converts lowercase characters to uppercase, while tolower() does the opposite. Both are part of the ctype.h library and operate on a single character at a time.
The standard toupper() function is not designed for wide characters. For wide character support in C, you should use towupper() from the wctype.h library, which is specifically made for wide character conversions.
The toupper() function in C is widely used in input validation, especially for making user input case-insensitive. By converting input to uppercase, developers can simplify conditional checks and reduce errors in logic.
Take a Free C Programming Quiz
Answer quick questions and assess your C programming knowledge
Author
Start Learning For Free
Explore Our Free Software Tutorials and Elevate your Career.
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918068792934
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.