0

I am coding on uVision 4 in C for a ARM assignment. I can't seem to figure it out but every time I keep on getting the string "536876144".

int main(void) {

    int binary[8] = {0,0,0,0,0,0,0,0};//I want this array as integers (for binary terms), therefore i can toggle each number as 1 or 0, corresponding to the input.
    char stringbinary[9]; //string for recording the value of the converted integer array
    sprintf(stringbinary, "%d", binary);//conversion of the array to string
    printf("%s\r\n",stringbinary);//printing the value
          .............
          .............

          if(input=1){
          binary[0]=1 - binary[0]; // I have the each value of the array to toggle with activation
          }

}

It may just be because I'm tired after hours of coding. I'm pretty sure it's something simple and basic but I just can't seem to find it.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
user3682308
  • 53
  • 1
  • 1
  • 6
  • 1
    `if(input=1)` should be `if(input == 1)`, and `int binary[8] = {0}`; is enough, remaining parts will be implicitly initialized to 0 – phuclv May 28 '14 at 07:25
  • the result is not "random" as you said, it's the address of `binary` – phuclv May 28 '14 at 07:26

1 Answers1

2

Your statement:

sprintf(stringbinary, "%d", binary);//conversion of the array to string

indicates that you misunderstand how to convert an array of integers to a string.

The line above will take the address of binary, convert it to an integer, and print the address as an integer.

If you want to print binary to stdout without any spaces between the numbers, you can use:

for (i = 0; i < 8; ++i )
{
   printf("%d", binary[i]);
}
print("\n");

Make sure you add a line

int i;

at the start of the function before you use that for loop.

R Sahu
  • 204,454
  • 14
  • 159
  • 270