Btw, im fairly new to coding :)
Basically I'm trying to do a program which asks the student how many grades he has (div), and calculates the overall grade (nota=grade, im portuguese). Note that in every try I always input '3' and the value for div and that I'm only showing part of the code
As for the [CORRECT VERSION], everything looks normal to me and runs fine.
**[CORRECT VERSION]**
short unsigned int div, i;
printf("Pretende fazer a media de quantas notas?(Máximo é 10): ");
scanf("%hd", &div);
float nota[div], media, soma = 0;
for(i = 0; i < div; i++) { //duvida linhas 25-26
pergunta:
printf("A %dª nota foi: ", i+1);
scanf("%f", ¬a[i]);
if((nota[i] < 0) || (nota[i] >20)) {
printf("\n(Erro: Por favor insira valores de 0 a 20\n");
goto pergunta;
My doubt is related to the ["NON-SENSE" VERSION], in the for loop.
I understand that me making i++, the loop's first run will assume i as being 0, but inside the for loop, i will be 1. Since the array_size=3, the 3rd grade (nota) would not have a slot to be assigned (because nota[0] would be skipped, which makes it 0 right?, leaving only 2 slots left), but the loop still runs 3 times, asks for the 3 grades and gives me the overall.
**["NON-SENSE VERSION]**
short unsigned int div, i;
printf("Pretende fazer a media de quantas notas?(Máximo é 10): ");
scanf("%hd", &div);
float nota[div], media, soma = 0;
for(i = 0; i++ < div;) { //duvida linhas 25-26
pergunta:
printf("A %dª nota foi: ", i);
scanf("%f", ¬a[i]);
if((nota[i] < 0) || (nota[i] >20)) {
printf("\n(Erro: Por favor insira valores de 0 a 20\n");
goto pergunta;
However, replacing div for 3, even though it is the same value that div assumed on the previous version, I'm getting a segmentation fault (and on some other experiments bus error)
[VERSION WITHOUT div GIVING A NORMAL ERROR]
short unsigned int div, i;
//printf("Pretende fazer a media de quantas notas?(Máximo é 10): ");
//scanf("%hd", &div);
float nota[3], media, soma = 0;
for(i = 0; i++ < 3;) { //duvida linhas 25-26
pergunta:
printf("A %dª nota foi: ", i);
scanf("%f", ¬a[i]);
if((nota[i] < 0) || (nota[i] >20)) {
printf("\n(Erro: Por favor insira valores de 0 a 20\n");
goto pergunta;
Could you explain me what I'm missing and why does even the ["NON-SENSE" VERSION] runs, even though it's pretty much the same as the last one?