C program "eating" characters when executed

0

Next, All IDEs I program in C are outputting with characters less than each iteration. For example, when executing a for, at each iteration one or more characters are added:

NotethatinbothnetbeansandDevC++,thesameerroroccurs.Ithinkit'ssomethingcompiler-related,sinceotherlanguageslikeJavaforexampledonothavethisprobleminthisenvironment.

Icouldnotfindanythingrelated,soIdonothavereferrallinks.FollowCode:

#include<stdio.h>#include<stdlib.h>intmain(intargc,char**argv){intalunos[10];inti,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);
}
    
asked by anonymous 03.02.2017 / 23:50

3 answers

3

Change this line:

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

You can not concatenate everything as you were doing. Curiously you know how to do the right thing 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 parameter of the function printf() is 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);
}

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

    
04.02.2017 / 00:02
6

Change the line

  

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

for

printf("insira a idade do aluno %d" ,i+1);
    
04.02.2017 / 00:06
4

People have already identified the error and put it right - 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 string of char values - and calls from stlib agree that a value of "0" is reached at the end of the string.

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

When writing "insira ..." + (i + 1) , the compiler has added the number (i + 1) to the pointer to the static string - so in this case, printf receives in each pass of for i a value one byte forward as being address of the beginning of the string. As in the text encoding that the program uses, one equals one character, we have the characters being progressively "eaten" in each interaction.

There are things that only C does for you. In other languages, called "higher level" for which has a greater 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 operator + is not allowed for strings, (2) typing error, why the operation of "+" between strings and integers is not defined or (3) an implicit conversion from number to string and concatenation of result - but even though the number would be placed at the end of the string, not replacing the "% d" in the string.

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

    
04.02.2017 / 01:10