The toupper function C programming converts a lowercase letter to a corresponding uppercase letter. In the default “C” locale, a lowercase letter is any of: a b c d e f g h i j k l m n o p q r s t u v w x y z
. which translates respectively to: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
.
One thing you should remember is that before using the toupper, you have to include ctype.h
it because it is declared in ctype.h
. The toupper function takes one argument in the form of an integer and returns the value of type int.
Syntax of toupper function C:
//Syntax of toupper int toupper(int c);
Parameters:
c
=> character to classify
Return value:
The toupper version of c
or unmodified c
if no lowercase version is listed in the current “C” locale.
Note:
In other locales, if a lowercase character has more than one correspondent uppercase character, this function always returns the same character for the same value of c; otherwise, the argument is returned unchanged.
C program to understand the working of toupper function:
Consider the below code where I am passing different characters in the toupper 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 = toupper((unsigned char)c); printf("toupper(%c) = %c\n", c, ret); c = 'z'; ret = toupper((unsigned char)c); printf("toupper(%c) = %c\n", c, ret); c = 'a'; ret = toupper((unsigned char)c); printf("toupper(%c) = %c\n", c, ret); c = '*'; ret = toupper((unsigned char)c); printf("toupper(%c) = %c\n", c, ret); return 0; }
Output:
toupper(A) = A toupper(z) = Z toupper(a) = A toupper(*) = *
C Program To Convert Uppercase String To Lowercase Using the toupper:
Algorithm:
1.
Traverse the given string character by character and passed it into the toupper
function.
2.
The toupper function converts the lowercase letter to a corresponding uppercase letter and left another letter unchanged.
3.
Using the putchar function we will print the return value of the toupper function.
#include <stdio.h> #include <ctype.h> int main () { unsigned int i=0; char str[]="aticLe WorlD.COM"; while (str[i]) { putchar (toupper((unsigned char)str[i])); i++; } return 0; }
Output:
ATICLE WORLD.COM
Note:
If the argument’s value (c) is neither representable as unsigned char not equal to EOF, the behavior of toupper is undefined.
As we know the behavior of toupper 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_toupper(char ch) { return toupper((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.