Code goes into infinite loop

-1

Why is this code in loop infinity?

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

int main()
{
int i;
float salario[10],  valorDataBase, salarioReajustado[10];

printf("Informe o percentual  de reajuste da data-base: \n");
scanf("%f",  &valorDataBase);


    for (i=0 ; i<10 ;  i=i++)
    {
        printf("Informe o seu salario atual: \n");
        scanf("%f", &salario[i]);
    }
    //Impressão da lista de  dados do vetor salario reajustado
    for (i=0 ; i<10 ;  i=i++)
    {
        salarioReajustado[i] = salario[i] + salario[i]*valorDataBase/100;
        printf("Seu salario reajustado sera %f \n",salarioReajustado[i]);
    }

getch();
return(0);
}
    
asked by anonymous 22.11.2018 / 18:35

1 answer

5

And I ask, why are you incrementing the i variable and assigning it to itself? This is indefinite behavior. If only increment works, after all is correct. You must have mistakenly copied it from somewhere. Anyway the ideal is to never copy anything, is to understand how it works to use properly. Simplifying:

#include <stdio.h>

int main() {
    float salario[10], valorDataBase;
    printf("Informe o percentual  de reajuste da data-base: \n");
    scanf("%f", &valorDataBase);
    for (int i = 0; i < 10; i++) {
        printf("Informe o seu salario atual:\n");
        scanf("%f", &salario[i]);
    }
    for (int i = 0; i < 10; i++) printf("Seu salario reajustado sera %f\n", salario[i] + salario[i] * valorDataBase / 100);
}

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

Final tip, in real cases monetary values can not be of type float .

    
22.11.2018 / 18:53