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))
)
Forget about the whole IDE thing. Read this: http://answall.com/q/101691/101
– Maniero