Someone can tell me whats is my mistake here? (I am Brazilian)

–1 vote
asked Jul 27 by João Victor (110 points)
/******************************************************************************

Tomada de Decisões (if)

*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <locale.h>

int main()
{
    setlocale(LC_ALL, "");
    /*
    #include <ctype.h> -> Biblioteca que permite manipular caracteres de varias maneiras
    Um caracter pode ser uma letra, um número, um sinal, etc...
    */
    char c;
    
    printf("Digite um caracter, em letra minúscula: ");
    scanf( "%c", &c );
    
    if(c >= 'a')
    {
        printf("Segue a letra que você digitou, em minuscula: > %c <"), toupper( c ) );
    }

    return 0;
}

1 Answer

0 votes
answered Jul 27 by Peter Minarik (4,550 points)

Mismatched Parentheses

You've got mismatched parentheses in your code. Here's the fix:

printf("Segue a letra que você digitou, em minuscula: > %c <", toupper( c ) );

No other big issues.

Scanf() Behaviour

scanf( "%c", &c );

The above line reads not just a character from the user input. The user input doesn't end when a single key was pressed, but when the user pressed ENTER. That is, when scanf() is called, the standard input (stdin) typically can store more than a single character and only the first one will be stored in c, the rest remains there.

So, if after this, you'd have a 

scanf("%s", &s);

call, i.e. reading a string from stdin, then the user would not be prompted for an input for the 2nd time. Scanf would process the remaining input in stdin.

This is not (necessarily) a problem, as long as you're aware of this behaviour.

If you want to process only the first character from the first user input (that is terminated by ENTER) and just ignore anything that follows, so you can ask the user to enter something for the second time, you need to read a whole string and get the character from that.

E.g. like this:

char buffer[256];
scanf("%s", &buffer);
sscanf(buffer, "%c", &c);

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...