简体   繁体   中英

Passing value entered by user in main() to another function using C

I am trying to write a C main() program with a function. the program asks for a character in main(), then passes it to a function which then prints the character entered and returns that character in uppercase to main(). In main() the new uppercase character is printed.

I am able to read the character and convert it to uppercase, but can't get it to print the entered character from the function "convert". Here is what the output should look like: Sample output . Following is what I have done so far. Any help appreciated.

#include <stdio.h>
#include <ctype.h>

char convert(char a1);

int main (void)
{
    char lower, upper;
    printf("Please enter a lowercase character: ");
    scanf("%c",&lower);

    upper = convert(lower);
    printf("(from main) Uppercase character: %c\n",upper);
    return(0);
}
char convert(char a1)
{
    char result;
    printf("Entered character: %c",lower); /*This statement is to be printed*/
    result = toupper(a1);
    return (result);
}

Here is my output

You need to add below line-1 and line-2 to your function convert :

char convert(char a1)
{
    char result;
    printf("Converting to uppercase ...\n");  // line-1
    printf("(from function) Entered character is: %c\n", a1);  // line-2
    result = toupper(a1);
    return (result);
}

output:

Please enter a lowercase character: t
Converting to uppercase ...
(from function) Entered character is: t
(from main) Uppercase character: T

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM