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))
)