Single variable declaration for scanf

Asked

Viewed 333 times

1

I already know how to declare a variable in the traditional way using the scanf(), but I’m thinking of a way that I can use fewer lines and make the code more organized (that’s what I tried with the variable valor).

It turns out that when I run the program the value 1 is always printed on the screen. Why the value 1 is printed on the screen?

There is a more organized way to use the scanf() without me using another line (similar to what I’m trying to do)?

Code in the replit ->

#include<stdio.h>

int main()

{
  int resultado = 1;
  printf("Coloque um valor: ");
  int valor = scanf("%d", &valor);

while (valor > 0){
  resultado = resultado * valor;
  valor =  valor - 1;}


  printf("O exponencial é: %d\n", resultado);


}
  • If the problem is the number of lines: Know that you can write everything in one.

  • 1

    The scanf returns the number of entries entered in the argument, in your case the "%d" reads a value so always returns 1. Has here the reference to confirm.

  • I just realized that what is being calculated is the factorial, not the exponential... it would be appropriate to correct the wording of the question for those who come here in the future to consult this question and their answers?

2 answers

1


There is not much to do. I will demonstrate to the more organized and with fewer lines, in addition to fewer characters.

Note that the scanf() nay variable declaring some.

In fact the scanf() nor should it be used like this. We use in exercises. This function returns a success information and not what was typed. What has been typed is always placed in the variable used as argument of the function. That’s why you need to pass it as reference.

Before using any function read the documentation and understand all aspects of it. Documentation of scanf().

Do not try to decrease the number of lines at any cost, just decrease the ones that are more organized like this.

#include<stdio.h>

int main() {
    printf("Coloque um valor: ");
    int valor;
    scanf("%d", &valor);
    int resultado = 1;
    while (valor > 0) {
        resultado *= valor;
        valor--;
    }
    printf("O exponencial é: %d\n", resultado);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

An alternative with for:

#include<stdio.h>

int main() {
    printf("Coloque um valor: ");
    int valor;
    scanf("%d", &valor);
    int resultado = 1;
    for (; valor > 0; valor--) resultado *= valor;
    printf("O exponencial é: %d\n", resultado);
}

0

I believe the while loop is not being started once you have declared the value variable wrong. Thus, as declared the value of the result variable, right at the beginning of the program, keeping the value 1 that information will be passed to the last printf of the program.

There is no need to assimilate the user input in the scanf function with the "=" symbol because this is the "&" function that precedes the value variable.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.