#include char upperCase(char lowerCase) { return lowerCase-32; } int main() { char lowerCase; scanf("%c",&lowerCase); printf("%c",upperCase(lowerCase)); return 0; }
What does the return statement in the above code returns an int or a char?
#include char upperCase(char lowerCase) { return lowerCase-32; } int main() { char lowerCase; scanf("%c",&lowerCase); printf("%c",upperCase(lowerCase)); return 0; }
What does the return statement in the above code returns an int or a char?
Short answer: a char - as specified by the signature of the function.
But lets take a closer look what is happening: In the function upperCase lowerCase is a char (effectively a one byte number), 32 is an integer literal. In order to perform the subtraction lowerCase gets widened to an integer with the same value (this is always possible). The integer result is then cast back to a char in order to return a char. At this point ou could get an underflow because not every int is representable as a char. But as long as lowerCase is in fact a lower case Ascii character, the result will fit into a char and you get the “right” result.
In java i think that code may look like this
import java.util.Scanner; class MrCoder1{ static char upperCase(char lowerCase){ return (char)(lowerCase-32); /** Converts lowercase to ASCII value lets say entered S - ASCII value is 83 (char)(83-32) = (char)(51) and returning (char)(ASCII) value and corresponding character for 51 is 3 */ } public static void main(String[] args){ Scanner z = new Scanner(System.in); char ch = z.next().charAt(0); System.out.println(MrCoder1.upperCase(ch)); //return 0; Main is a void cannot use that return as is in cpp z.close(); } }
It returns a character.
It returns a character as evident form return type.