The problem that is causing your code not to work is that you have a semicolon after the for
statement like this:
for(i = 1; i < sizeMe + 1; i++);
{
string[i] = arr[sizeMe - i] + '0';
}
That code would be equivalent to this:
for(i = 1; i < sizeMe + 1; i++){}
string[i] = arr[sizeMe - i] + '0';
So what's happening there is that only the last element of string
is written in, the rest is left blank, which generates undefined behavior. To solve the problem, remove the semicolon. I also recommend putting the {
at the end of the line of the for statement instead of on a new line since doing so would prevent you from making such mistakes. This is therefore the correct code, formatted in the way that I recommend formatting:
for(i = 1; i < sizeMe + 1; i++){
string[i] = arr[sizeMe - i + 1] + '0';
}
Note that you also forgot the +1
in arr[sizeMe - i + 1]
.
Although that was the error that made your code not work the way you wanted it to work, you've also made several other mistakes, which could potentially cause your program to crash.
First of all, in the for loop, you're not testing if i
is greater than LENGTH
. The problem with that is that if i
is greater than LENGTH
, string[i]
will be outside of the array, which will cause buffer overflow and make your program crash. To solve this problem, add the following code inside your for loop:
if(i + 1 > LENGTH){
break;
}
The break
statement will exit the for loop immediately. Note that we're testing if i + 1
is greater than LENGTH
to leave space for the null character at the end of the string.
For the same reason, string[sizeMe] = 0;
is also bad, since nothing says that sizeMe
is less than the size of the array. Instead, use string[i] = 0;
, since i
has been incremented until it either reaches LENGTH
or sizeMe
. Therefore, i
will be the minimum of these two values, which is what we want.
You also need to return something at the end of the main
function. The main
function is of type int
, so it should return an int
. In C++, this code wouldn't compile because of that. Your code compiled since you're using C and C compilers are generally more tolerant than C++ compilers, so you only got a warning. Make sure to fix all compiler warnings, since doing so can solve bugs. The warnings are there to help you, not to annoy you. Generally, the main
function should return 0, since this value usually means that everything went well. So you need to add return 0;
at the end of the main
function.
Therefore, this is the code you should use:
#include <stdio.h>
#include <string.h>
#define LENGTH 50
int getLength(int num, int arr[]);
int main(){
int num = 0, i = 0;
int arr[LENGTH] = {0};
char string[LENGTH] = {0};
printf("enter number: ");
scanf("%d", &num);
int sizeMe = getLength(num, arr);
string[2] = arr[2] + '0';
printf("length %d one of values: %c", sizeMe,string[2]);
for(i = 1; i < sizeMe + 1; i++){
string[i] = arr[sizeMe - i + 1] + '0';
if(i + 1 > LENGTH){
break;
}
}
string[0] = '+';
string[i] = 0;
printf("string: %s", string);
return 0;
}
int getLength(int num,int arr[]){
int count = 0;
int i = 0, temp = 0;
while(num != 0){
temp = num%10;
num /= 10;
count++;
i++;
arr[i] = temp;
printf("index %d is %d\n",i,arr[i]);
}
return count;
}
Also, as some programmer dude pointed out in a comment, it's much easier to use sprintf
. Then your code would be much simpler:
#include <stdio.h>
#include <string.h>
#define LENGTH 50
int main(){
int num = 0;
char string[LENGTH] = {0};
printf("enter number: ");
scanf("%d", &num);
sprintf(string, "%c%d", num >= 0 ? '+' : '-', num);
printf("string: %s", string);
return 0;
}