Entering an arithmetic expression instead of an integer in C

Asked

Viewed 72 times

2

I’m new in C and I made a very simple program that takes two numbers and prints the sum of them:

// Programa que faz uma soma simples
#include <stdio.h>

// Função principal do programa
int main( void )
{
    int num1, num2, soma;

    printf("Entre com o primeiro número\n"); // Exibe texto na tela
    scanf("%d", &num1); // Lẽ o valor e o atribui a num1

    printf("Entre com o segundo número\n");
    scanf("%d", &num2);

    soma = num1 + num2;

    printf("A soma é %d\n", soma);
} // fim da função main

I compiled it with gcc and it worked normally, but out of curiosity I decided to do some experiments. When the program requested an input, instead of typing an entire value I put an arithmetic expression and to my surprise it didn’t work? When I enter a sum or subtraction in the first entry it does not request a second entry and prints the result of the operation:

$./soma
Entre com o primeiro número
8+10
Entre com o segundo número
A soma é 18

When I enter an arithmetic expression in the second scanf, the program sums the value of the first entry with the first number of the expression, ignoring the rest:

$./soma
Entre com o primeiro número
5
Entre com o segundo número
2+9
A soma é 7

The strange thing happens when I try to enter with an expression involving multiplication or a floating point number, in which case the program simply returns a random value:

$./soma
Entre com o primeiro número
3*2
Entre com o segundo número
A soma é 1714397539

$./soma
Entre com o primeiro número
3*2
Entre com o segundo número
A soma é 98186483

$./soma
Entre com o primeiro número
3.4
Entre com o segundo número
A soma é 229452083

My question is: Why this strange result when there is multiplication involved?

1 answer

5


instead of typing an entire value I put an arithmetic expression and to my surprise it is not that it worked?

Giving the result you expect is quite different from working. In the case was mere coincidence as quickly realized when did a multiplication.

The detail you’re missing is that so much 2 as +2 as -2 are valid numbers. So when you enter as 2+3 the first number shall be 2 and the second number is +3 which is just the 3 positive sign.

Another way to confirm this is to put the expression 3-2.

See in ideone how putting 3-2 on the input you get the result 1

Wouldn’t it be weird if you’re making a sum in the code, the result being subtraction ? And this is not about interpreting an expression but about how values are assigned in num1 and num2.

In this case the first number was the 3 and the second number read was -2.

In this ideone I put the print of the second number so that I can understand better

When you put a * the first number is valid, but the second number is no longer valid because it starts with an invalid character and therefore nothing is read to the num2. As nothing is put on num2 he gets the value he had, whether it’s 0 or a random number in memory, which already depends on the implementation.

In the test I did on ideone for 3*2 the result is only the value of the first number.

Just as @zentrunix said, in doubt you can and should confirm how many values were read correctly by returning scanf. In the case of 3*2 the scanf("%d", &num2) will return you 0 'cause you couldn’t read any numbers, and then you realize something didn’t work properly.

  • 1

    complementing the answer, the correct is to always check the scanf return value to know the number of fields effectively read

  • 1

    @zentrunix Yes correct, and thanks for the add-on :)

  • I’m still a little intrigued about this multiplication thing, I think it has something to do with the pointer and stuff. I hope you find out by studying more about C. In this online IDE I was able to simulate the multiplication result, although here it always gives a result close to 2¹ while in my computer it results in quite different values: http://tpcg.io/vaFfFG

  • @Israel77 It is not about the pointer, but about using values that have not been initialized. It would be equivalent to doing int x; printf("%d", x); where you cannot tell which value will show and will normally be a random value which is the value that is in the memory location where the variable was allocated. The random value is usually high because to be low it needed to have many bits at 0, almost all the first bits and this is statistically improbable, although possible

Browser other questions tagged

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