Program does not meet expected flow by reading and printing input

Asked

Viewed 69 times

3

#include <stdio.h>
#include <stdlib.h>
int main()
{

        int num1, num2, num3;
        printf("Digite o numero A e numero B para saber o resto da divisao resultante da divisao entre eles:\n");
        printf("NumeroA: "); scanf("%i\n", num1);
        printf("\n");
        printf("NumeroB: "); scanf("%i\n", num2);
        printf("O resto da divisao e: %i", num1 % num2);
        return 0;
}

Instead of printing the rest, OS prints the first, the second printf() and the scanf().

What am I doing wrong?

I would also like to know how I can indicate a variable in a printf() (as I know Python I will try to explain and if anyone knows I think you can understand)

n = int(input("Digite um numero: "))
m = int(input("Digite outro numero: "))
print (" Imprimiu o numero {} e {}".format(n, m))

I’m new and in this and I’m only 15 D, use accessible language please.

3 answers

3


The biggest problem is that you are not passing the address of the variable where the value should be placed in the scanf(). This function needs to know where to put the entered value. Passing the value that is in it is useless. It works different from Python that the value is returned in the function.

The operator & takes the variable address. This is what you need to change in the code

In fact this function even returns a given value if the reading is correct or not. In general in exercises like this we let it go, but the correct thing is to check if the operation was successful. See the documentation of scanf().

#include <stdio.h>

int main() {
    int num1, num2;
    printf("Digite o numero A e numero B para saber o resto da divisao resultante da divisao entre eles:\n");
    printf("NumeroA: "); scanf("%d", &num1);
    printf("\n");
    printf("NumeroB: "); scanf("%d", &num2);
    printf("O resto da divisao e: %d", num1 % num2);
}

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

Are you already using the printf() how to use it. You can pass an expression or variable without problems. What is in the % is the location that goes each argument with the specific formatting. See the formatting.

I improved a few more things, pay attention to the code.

I recommend learning things in a structured way and with good material, especially in C. In trial and error does not work.

  • I’m learning from Youtube through the playlist C course: http://www.youtube.com/playlist?list=PLesCEcYj003SwVdufCQM5FIbrOd0GG1M4 recommends something better?

  • I recommend well-known books or courses. These classes look bad. See https://answall.com/tags/c/info.

2

Missed the & in reading the values in scanf. This is necessary because the scanf receives the location in the memory where you have to put the read values, which is what the operator & gives.

So do it like this:

#include <stdio.h>
#include <stdlib.h>
int main()
{

        int num1, num2, num3;
        printf("Digite o numero A e numero B para saber o resto da divisao resultante da divisao entre eles:\n");
        printf("NumeroA: "); scanf("%i", &num1);
        printf("\n");
        printf("NumeroB: "); scanf("%i", &num2);
        printf("O resto da divisao e: %i", num1 % num2);
        return 0;
}

Should not also have the \n in the scanf otherwise the reading of the numbers will not be normal.

The most normal formatting for numbers is %d, but %i works also, being able to recognize values in octal and hexadecimal if prefixed correctly.

1

You have to pass the variable address when using scanf() and you’re expecting a \n in the scanf(), in case you don’t need.

Your code would look the right way:

#include <stdio.h>
#include <stdlib.h>
int main()
{

        int num1, num2, num3;
        printf("Digite o numero A e numero B para saber o resto da divisao resultante da divisao entre eles:\n");
        printf("NumeroA: "); scanf("%i", &num1);
        printf("\n");
        printf("NumeroB: "); scanf("%i", &num2);
        printf("O resto da divisao e: %i", num1 % num2);
        return 0;
}

Browser other questions tagged

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