For in the C language

Asked

Viewed 105 times

1

I studied the iterative artifice for and thought the same applied to the following example:

for(i = 0; i < 7; i++){Código}

However, when studying the pointer part with vectors, I came across the following example:

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

int main()
{
      int vet[7]={0};
      int i;

      for(i=0;i<7;i++)
        printf("%d ",*(vet+i)); //Mostrando todos os valores do vetor
        printf("\n\n");
        printf("%i \n\n",vet); //Mostrando o endereco de memoria do primeiro indice do vetor

      for(i=0;i<7;i++)
        printf("%i ",(vet+i)); //Mostrando todos os endereco de memoria do vetor
        printf("\n\n");

      *(vet+5)=1024; //Alterando o indice seis para 1024
      for(i=0;i<7;i++)
         printf("%d ",*(vet+i)); //Exibindo todos os indices do vetor
         
      getchar();
      }

Why didn’t the author of this code use the keys and the iteration device worked? In my compiler (Codeblocks), if you put the keys the program does not return the same as the previous one.

  • In reality your indentation is wrong. Only the first printf after the for is that runs in the loop. The other printf runs after the end of the loop. You use the block {} when you want to execute more than one command inside the loop.

1 answer

3


This code is misspelled and gives a wrong idea of what it does. Written in a better way:

#include <stdio.h>

int main() {
    int vet[7] = {0};
    for (int i = 0; i < 7; i++) printf("%d ", vet[i]); //Mostrando todos os valores do vetor
    printf("\n\n");
    printf("%d\n\n", vet); //Mostrando o endereco de memoria do primeiro indice do vetor
    for (int i = 0; i < 7; i++) printf("%d ", (vet + i)); //Mostrando todos os endereco de memoria do vetor
    printf("\n\n");
    vet[5] = 1024; //Alterando o indice seis para 1024
    for (int i = 0; i < 7; i++) printf("%d ", vet[i]); //Exibindo todos os indices do vetor
    getchar();
}

I put in the Github for future reference.

You can write in other ways but now it’s clearer and more readable.

Want to use keys? Ok, use and let closer to the original, no reason. When there are no keys and either by only can contain a command line within it, putting more lines is changing the semantics of the code. Indentation does not define what is a block of commands, only the key creates a block, if it had no key, then it had no block, only one line belongs to the command for:

#include <stdio.h>

int main() {
    int vet[7] = {0};
    for (int i = 0; i < 7; i++) {
        printf("%d ", *(vet + i)); //Mostrando todos os valores do vetor
    }
    printf("\n\n");
    printf("%i \n\n", vet); //Mostrando o endereco de memoria do primeiro indice do vetor
    for (int i = 0; i < 7; i++) {
        printf("%d ", (vet + i)); //Mostrando todos os endereco de memoria do vetor
    }
    printf("\n\n");
    *(vet + 5) = 1024; //Alterando o indice seis para 1024
    for (int i = 0; i < 7; i++) {
        printf("%d ", *(vet + i)); //Exibindo todos os indices do vetor
    }
    getchar();
}

Lesson to be learned: don’t use code as an example that you don’t understand and given by people who don’t care about writing modern, readable code.

  • Wow, thank you so much @Manieiro! Thanks for the tips, will help me a lot in building my journey learning this programming language.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.