r/cprogramming • u/thedraegonlord • Sep 22 '26
For command working weirdly
Edit: Declaring n as 1 and changing the for loop to (i = n-1; i > 0; --i) as was suggested seems to have worked. Still don't really get the logic behind it, neither why the order inside the for expression was affecting the result nor why changing the n to 1 from 0 and i to n - 1 made a difference. Aren't they the same value at the end? Anyway, thanks.
For some reason, my "for" command only works when I put the increment in the middle and the condition at the end. I have no clue what's going on but the code is working fine when I do this.
I'm using devc++, the rest of the code is me declaring global variables and asking the user for inputs to fill the arrays. Everything is working exactly as intended but breaks if I write the "for" the regular way.
for(i = n; i--; i>0){
printf("Codigo: %d", codigo\[i\]);
printf("\\nProduto: %s", nome\[i\]);
printf("\\nPeso: %.2f", peso\[i\]);
printf("\\nPreco: %.2f", preco\[i\]);
if(regiao\[i\]==1){printf("\nRegiao: Sudeste");
}
else if(regiao\[i\]==2){printf("\nRegiao: Sul");
}
else if(regiao\[i\]==3){printf("\nRegiao: Nordeste");
}
else if(regiao\[i\]==4){printf("\nRegiao: Norte");
}
printf("\\n\\n");
6
u/atarivcs Sep 22 '26 edited Sep 22 '26
i-- evaluates to zero, and thus to false, when i is zero. So that expression can also act as a condition.
Can you give an example of how the code "breaks" when you use for the regular way?
2
u/Axman6 Sep 22 '26 edited Sep 22 '26
So you set i to zero, and then usei--to decrement it to -1buti--returns the value before the decrement, which is zero, which is interpreted as false.Try
for(i = 0, i < n, i++)or if you need to go in reverse orderfor(i = n-1; i >= 0; i--)2
u/bearheart Sep 22 '26
In your code:
for(i = n; i--; i>0) {... without knowing the value of
nit's impossible to know what the problem may be.The
forcontrol works like this:for(initializer;condition;expression)statement or blockthis is functionally equivalent to:
initializer;
while(condition) {statement;or{block}expression;}The initializer is an expression that is used to initialize a variable,
iin your example.The condition controls the loop. The loop continues while the condition is true, and ends when the condition is false.
The expression is most commonly used to change the condition, e.g.,
--iwhen you're testing forito become zero.So, in your example this would be more correct:
That will count down from
5and execute the statements in the loop five times.1
u/thedraegonlord Sep 22 '26
Here's the full thing
int n;
int i;
int codigo[100];
char nome[100][50];
int regiao[100];
float peso[100];
float preco[100];
int confirma = 0;
int carrinho = 1;
int main(){
printf("\nRegiao: Sudeste");
printf("\nRegiao: Sul");
printf("\nRegiao: Nordeste");
printf("\nRegiao: Norte");
}
1
u/fluffycritter Sep 23 '26
I think you need to understand how a for loop works in C.
The basic syntax is:
It is equivalent to the following:
If you're ever confused about why a
forloop isn't working the way you expect it to, first try converting theforloop to thewhileversion, and then manually step through the code.Where your confusion mostly comes from is that an assignment/increment/decrement can also be a valid condition (pretty much anything in C can be aside from a
voidfunction) and the way that it gets treated as one isn't always immediately intuitive. For example,i++andi--modify the value ofibut then evaluate to the previous value ofi.Compounding this, the
stepstatement can also be a condition that has no side-effect, and so for example a comparison between two numbers is a perfectly validstepeven if it does nothing.I highly recommend compiling your C code with all warnings enabled and possibly converted to errors (in both gcc and clang this is
-Wall -Werror) because it can be very helpful to determine where something might be going wrong.0
u/WittyStick Sep 23 '26 edited Sep 23 '26
Be careful using a decrementing for loop if you are using an unsigned index, as the example (
i > 0) will result in an infinite loop, and the compiler may remove it entirely. Ifiis unsigned the condition should bei != 0, or instead ofi >= 0, usei != SIZE_MAX(for eg,size_t), or use a while loop.