0

The problem is the same as this:

A program returns different results in two IDE

I have this same issue, but in VSCode, and in C language. How do I set it for 64-bit compilation?

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char const* argv[])
{
    char string[20];
    printf("Informe o nome: ");
    scanf("%[^\n]%*c", &string);

    double salario = 0;
    printf("Informe o salario: ");
    scanf("%lf", &salario);
    double salarioCopy = salario;

    if (salario > 4087.65) {
        salario = salario * 0.725;
    }
    else if (salario >= 3271.39) {
        salario = salario * 0.775;
    }
    else if (salario >= 2453.51) {
        salario = salario * 0.85;
    }
    else if (salario >= 1637.12) {
        salario = salario * 0.925;
    }
    else {
    }
    printf("\n%s ganha R$ %.2f, que apos abatido o IR, ficam R$ %.2f\n", string, salarioCopy, salario);
    system("PAUSE");
    return 0;
}

f I input 10000, the result in VSCode is: 9250, while in a online IDE i get 7250.

Can anybody help?

Gary Strivin'
  • 908
  • 7
  • 20
z0let
  • 1
  • 1
  • If I input 10000, the result in VSCode is: 9250, while in a online IDE i get 7250. – z0let Nov 20 '20 at 12:40
  • what `scanf("%lf", &salario)` returns? Did it read data successfully (returns `1`)? How long name do you type to input? It is possible that it is too long (more then 19) and you have Undefined Behavior. Note that online ide is right. – Marek R Nov 20 '20 at 12:52
  • Did you read the documentation of your C compiler, perhaps [GCC](http://gcc.gnu.org/) ? Did you read the documentation of your debugger, maybe [GDB](https://www.gnu.org/software/gdb/) ? Did you read [*Modern C*](https://modernc.gforge.inria.fr/) and [this C reference](https://en.cppreference.com/w/c) and [n1570](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf) ? Are aware of [undefined behavior](https://www.geeksforgeeks.org/undefined-behavior-c-cpp/) ? – Basile Starynkevitch Nov 20 '20 at 12:59
  • Copy paste input and output for both cases so we have clear picture what have you typed in incorrectly. – Marek R Nov 20 '20 at 13:01
  • Which online IDE? – molbdnilo Nov 20 '20 at 13:05
  • You probably should tag this with C instead of C++. – Gary Strivin' Nov 20 '20 at 13:23
  • 3
    `scanf("%[^\n]%*c", &string);` invokes *undefined behavior* by passing data having wrong type: `char*` is expected but `char(*)[20]` is passed. The `&` here should be removed. – MikeCAT Nov 20 '20 at 13:31
  • Getting two different results from two different compilers means that (excluding the possibility of a compiler bug) [you have demons flying out of your nose](http://www.catb.org/jargon/html/N/nasal-demons.html). P.S. This is not a C++ program. This is a C program. – Sam Varshavchik Nov 20 '20 at 13:34
  • Fix the undefined behaviour first. Then run through the code line by line. My money is on the `if` statements evaluating differently due to strict floating point being on or off. – Bathsheba Nov 20 '20 at 13:36
  • after changing from MINGW to MINGW-64, it worked. Should i review the code anyway? – z0let Nov 20 '20 at 17:23
  • PVS-Studio error: V576 Incorrect format. Consider checking the second actual argument of the 'scanf' function. The pointer to string of char type symbols is expected. - https://godbolt.org/z/7vY3dh – AndreyKarpov Nov 20 '20 at 18:24

0 Answers0