Program does not meet expected flow reading and printing entry

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, second printf() and 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 will try to explain and if anyone knows I think I 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 to this and I'm only 15 years old. D, use accessible language please.

    
asked by anonymous 05.11.2017 / 22:50

3 answers

3

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

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

Actually this function even returns an indicated value whether or not reading is successful. Usually in exercises like this we leave it there, but the correct thing is to verify if the operation was successful. See the documentation for 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);
}

See running ideone . And no Coding Ground . Also put it in GitHub for future reference .

You're already using printf() as you should. You can pass an expression or a variable without problems. What is in % is the location that each argument goes with the specific formatting. View formatting .

I improved some 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 it does not work.

    
05.11.2017 / 22:55
2

& was missing reading values in scanf . This is necessary because scanf receives the location in memory where it has to put the read values, which is what the & operator gives.

Then do 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;
}

You should not also have \n in scanf otherwise reading the numbers will not be normal.

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

    
05.11.2017 / 22:56
1

You have to pass the address of the variable when using scanf() and you are expecting a \n in scanf() , in case you do not need it.

Your code would be correct:

#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;
}
    
05.11.2017 / 22:55