Entering an arithmetic expression instead of an integer in C

2

I'm new to C and I made a simple program that gets 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 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 integer value I put an arithmetic expression and to my surprise did not it work? When I enter a sum or subtraction on 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 with an arithmetic expression in the second scanf, the program adds 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 happens when I try to enter 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 multiplication is involved?

    
asked by anonymous 09.12.2018 / 01:26

1 answer

5
  

Instead of typing an integer value I put an arithmetic expression and to my surprise did not it work?

Giving the result you expect is very different from working . In the case it was mere coincidence how quickly he realized when he did a multiplication.

The detail you're missing is that both% w / o% w / o% w / o% w / w% are valid numbers. Then when you enter the entry as 2 the first number will be the +2 and the second number is the -2 which is only the 2+3 with positive sign.

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

See in ideone how putting +3 in the input gets the result 3

Would not it be strange if you were doing a sum in the code, the result being subtraction? And this has nothing to do with interpreting an expression but rather how values are assigned in 3-2 and 3-2 .

In this case the first number was 1 and the second number was num1 .

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

When you put a num2 the first number is valid, but the second is no longer valid since it starts with an invalid caratere and therefore nothing is read for 3 . Since nothing is put into -2 it gets the value it had, whether it is * or a random number in memory, which already depends on the implementation.

In the test I made in ideone for num2 the result leaves only the value of the first number.

As @zentrunix said, in doubt you can and should confirm how many values were read correctly through the return of num2 . In the case of 0 the 3*2 will return scanf because it could not read any number, and then you notice that something did not work correctly.

    
09.12.2018 / 02:21