The functionname 'intToStr' and the variable 'string' suggests that this function should build a string from an integer.
The first if-statement (handle the negative integer) is correct and you realy work with characters ( string[0] = '-'; ).
But this line set the value from 0 to 9.
Code:
string[x] = input%10;
All characters lower then 32 are control-characters.
To set the right character (number-symbol) you should add '0'.
Code:
string[x] = input % 10 + '0';
As the other mentioned befor, the contition of the if-statement is not realy good.
But you know the right contition. You use it inside the for-body.
Why you don't use:
Code:
for (x = negative ; input > 0 ; x++)
{
…
or in short:
Code:
for (x = negative ; input ; x++)
{
…
Remember: all values other then zero will evaluate to 'true', only zero will evaluate to 'false'.
With this solution you don't need the if-statement inside the for-body.
The for-loop will end if the input is zero.
You can terminate the string after the loop with:
You can check the result of the function with a last line inside the function.
Code:
printf("Result: '%s'\n", string);
You will notice that the resulted string is in reverse order. This is the next problem for you to solve.