isupper() in C
Overview
isupper() in C is a library function that tells us if the character passed to it is in uppercase or not. If the ASCII value of an uppercase alphabet is passed as an argument, then the isupper() function returns a positive integer. Else the function returns 0. Also, we can pass only one character at a time as an argument for the isupper() function. The isupper() function is defined in the ctype.h header file.
Header for isupper() in C
In the C Language, the required header for the isupper function is
Syntax of isupper() in C
Let us now understand what kind of argument can be passed to the function isupper.
Parameters of isupper() in C
isupper() in C takes only a single parameter of integer (int) data type. If we pass a character as an argument, the compiler will implicitly caste the character to an integer or take its ASCII value as an argument.
Return Values of isupper() in C
Return Type: int
As we saw that the function isupper() returns an integer value, let us now discuss the values it can return as an integer and what they would mean.
Return Value | Remarks |
---|---|
Non zero positive integer (that is an integer x > 0) | Argument is an uppercase alphabet. |
Zero(0) | Argument is not an uppercase alphabet. |
Note that the function may return any Non zero positive integer to indicate the argument is an uppercase alphabet
Exceptions of isupper() in C
This function doesn’t throw an exception, but there are some cases where its behavior is undefined, like:
- For long long integers which are not in the range of integer data type, isupper() in the C function returns the garbage value because its parameter is of integer data type.
- If the user tries to pass any object of user-defined data types like structure, enum, or union, this function will throw an error. We can pass only a single character as a parameter to this function; passing a character array or string will also throw a runtime error.
Example of isupper() in C
Output:
In the above code, we have a variable storing character, namely ch1 storing the character 'G'. As the 'G' is an uppercase letter, the function call isupper(ch1) returns a non-zero value, and we can print the result using an if-else statement.
What is isupper() in C?
Let’s look into ASCII values first before moving forward: ASCII is a set of a total of 128 7-bit characters. Every character is given an ASCII number, like uppercase letters in the range 65 to 90, and lowercase letters in the range 97 to 122.
If we pass an integer value between the range 65 to 90 or an uppercase letter, then the isupper() function will return a non-zero value. In another case, isupper() in C will return zero. If we try to pass any float or double data type values in the range 65 to 90, the function will ignore the digits after the decimal point and return a non-zero value; otherwise, it will return zero.
Let us look at some more examples to understand the working of the function properly.
Examples for isupper() in C
Lowercase Alphabets
In this example, we will pass lowercase alphabets as arguments to the function isupper and will look over its ASCII values:
Output:
In the above code, we pass a lowercase letter 'd' as an argument to the function isupper. The function then returns a value of zero(0) as the ASCII value of the letter 'd' is 100, which is not in the range of 65 to 90 (both inclusive).
Uppercase Alphabets
In this example, we will pass uppercase alphabets as arguments to the function isupper and will look over its ASCII values:
Output:
In the above code, we pass the uppercase letter 'H' as an argument to the function isupper. The function returns a non-zero value as the ASCII value of the letter 'H' is 72, which is in the range of 65 to 90 (both inclusive).
Special Characters / Numbers
In this example, we will pass special characters, two different types of numbers, and a double value.
Output:
In the above code, we pass the symbol % as an argument to the function isupper. The function then returns a zero (0) value as the ASCII value of % is 37, which is in the range of 65 to 90 (both inclusive).
Similarly, when we pass integer value '89' as an argument, we see the function returns a non-zero value indicating that it is in the range of 65 to 90, whereas in the case when we pass the value '97' the function return zero (0) indicating that this is not in the range of 65 to 90 (both inclusive).
Again, when we pass the value '8589934657' as an argument, and the function isupper returns a non-zero value indicating that it is in the range of 65 to 90, which is not true. This happens because the function behaves in undefined behavior for all the values greater than the range of the integer data type.
Note: The function may return different values for different compilers and different machines, but the output will be a garbage value if the passed argument is beyond the range of integer data type.
Also, when we pass a double data type as an argument, the function truncates the decimal part. It uses the numerical(integer part) to determine whether the passed argument is in the range of 65 to 90 (both inclusive).
Conclusion
- isupper() in C is used to check if a given character is in uppercase or not.
- It takes an integer as an input and returns an integer value.
- If we pass the ASCII value of an uppercase character, i.e., values between 65 to 90, as an argument, it returns a positive integer. In all other cases, this function will return 0.
- If we pass a character to it as an argument, then the function implicitly castes the character to an integer or takes its ASCII value as an argument.