C program "eating" characters when executed

Asked

Viewed 809 times

0

Next, All Ides I program in C are output with characters less with each iteration. For example, when executing a for, every iteration one or more characters disappear:

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

Note that in both netbeans and Devc++ the same error occurs. I think it is something related to the compiler, since other languages like Java for example, do not present this problem in this environment.

I couldn’t find anything related, so I don’t have any reference links. Segue Codigo:

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv) {
    int alunos[10];
    int i, idade;
    for (i = 0; i < 10; i++) {
        printf("insira a idade do aluno %d" + (i + 1));
        scanf("%d", &idade);
        alunos[i] = idade;
    }
    for (i = 0; i < 10; i++) {
        printf("%d \n", alunos[i]);
    }
    return (EXIT_SUCCESS);
}
  • Forget about the whole IDE thing. Read this: http://answall.com/q/101691/101

3 answers

7

Change the line

printf("insira a idade do aluno %d" + (i + 1));

for

printf("insira a idade do aluno %d" ,i+1);

5

People have already identified the mistake and put the right way - but I think it’s worth explaining what was happening:

When placing the line:

printf("insira a idade do aluno %d" + (i + 1));

The result of the expression between is what was passed to "printf" - well, C does not have a "string type" itself - all strings, which by convention point to a char string - and stlib calls agree that when a value is found to be "0" reached the end of the string.

Therefore, when we declare strings that will be changed during execution, they are of the type "char *" - and fixed strings, although written as several characters between " internally are the same thing: the compiler creates that string at one point in the program’s memory, and the whole code uses a pointer to that point in memory.

When writing "insira ..." + (i + 1), the compiler added the number (i + 1) to the pointer for the static string - then the printf in this case, receives at every step of the for i a byte value forward as the start address of the string. As in the text encoding the program uses, one is equivalent to a character , we have the characters being progressively "eaten", in each interaction.

There are things only C does for you. In other languages, called "higher-level" so that it has a higher abstraction, the strings are real, and trying to add the number to the string would have one of three effects, depending on the language: (1) Syntax error, why the operator + not allowed for strings, (2) Typing error, why is not defined the operation of "+" between strings and integers or (3) an implicit conversion of the number to string and concatenation of the result - but even so, the number would be placed at the end of the string, not replacing the "%d" in the string.

(Interestingly, in the Python language the substitution of %s and %d markings by values is defined between strings and other objects with the operator % - and this would work: print("insira a idade do aluno %d:" % (i + 1)) )

3


Change this line:

printf("insira a idade do aluno %d", i + 1);

You can’t concatenate everything like you were doing. Interestingly you know how to do it right and put a placeholder to fit the value you need. Do not forget to use the comma, since the value to be used is another function parameter printf(), not to "add" to the string. Read the documentation carefully.

#include <stdio.h>
#include <stdlib.h>
int main(void) {
    int alunos[10];
    for (int i = 0; i < 10; i++) {
        printf("insira a idade do aluno %d", i + 1);
        scanf("%d", &alunos[i]);
        printf("\n");
    }
    for (int i = 0; i < 10; i++) printf("%d\n", alunos[i]);
    return (EXIT_SUCCESS);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • 1

    I really realized that this error was very recurrent as soon as I read the answer, I think it is why in Java it is so so I ended up doing the same, the error was solved, thank you.

  • Switching between languages is a problem. I sometimes spend half an hour trying to understand the error in using "->" in java or "." in PHP....

Browser other questions tagged

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