Home > Software design >  Weird characters appearing at the beginning of my code
Weird characters appearing at the beginning of my code

Time:05-15

I've created a code which displays integers till a given value with numbers (up to the maximum of the two digit range), and shows the tens as text. I have noticed that the output gets formatted, separating each group of ten numbers to lines. What is even more weird for me, that some kind of arrow character is put in the beginning of the output and I do not know why. It looks like character no. 16 in the ASCII-chart. Any help to get rid of the weird formatting and the arrow symbol is highly appreciated!

#include <stdio.h>
#include <stdlib.h>

int main() {
    printf("Provide limit between 0 and 99 (integer): ");
    int limit;
    scanf("%d", &limit);
    if (limit < 0 || limit > 99) exit(1);
    char tens[9][8] = {"ten", "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety"};
    char A[limit];
    int num[limit], tens_index;
    for (int i = 0, j = 1; i < limit; i  , j  ) {
        num[i] = j;
        A[i] = j   '0';
            if (num[i] == 10) {
                A[i] = 10;
                num[i] = 1;
                j = 0;
            }
    }
    for (int i = 0; i < limit; i  ) {
        for (int j = 0; j < 10; j  ) {
            if (i == j * 10) {
                tens_index = j - 1;
                printf("%s ", tens[tens_index]);
            }
        }
        printf("%c ", A[i]);
    }
    return 0;
}

Test output for input = 34.

Test

CodePudding user response:

For the formatting, in the code block

if (num[i] == 10) {
   A[i] = 10;
   num[i] = 1;
   j = 0;
}

You are assigning the value 10 to A[i] this corresponds to the new line character ‘\n’, meaning when the following line of code is executed

printf(“%c”,A[i])

At values of i corresponding to indexes containing 10, you are printing the newline character causing the cursor to advance to the next line.

CodePudding user response:

The variable I will be equal to J * 10 when it is equal to 0, when inside this statement, the test_index will be -1 because 0 - 1 = -1, when you try to access a array with negative index it will become very crazy hambuguer.

  • Related