C langugage - "Weird" characters in char[] output

11,510

Solution 1

It looks like you don't have a NUL terminator on your postfix string. You could either change the definition to char postfix[20] = {0};, or you just before the printf, you could add postfix[p] = '\0';

Solution 2

YOU DIDN'T NULL TERMINATE YOUR STRINGS! Kidding. Did you get that message yet from the thirty or so other people that told you? Just to add some information on top of that, the garbage characters are an attempt to interpret whatever happens to be in memory beyond the end of your char[] as chars. It grabs all it can of whatever is in memory until it hits a null terminating character and spits it all out, and the reason it's the same every time is that your char[] and ĚĚĚĚĚĚĚĚĚĚĚ get allocated next to each other every time the program runs. If you already knew all of this, then I apologize for wasting your time with another superfluous answer.

Solution 3

You need to NUL-terminate postfix.

postfix[p] = 0;
printf...

A simpler (but slightly less efficient) method would be to initialize your array to {0} or to memset it to 0.

Solution 4

    char infix[20], postfix[20];

You do not initialize these, nor append '\0' at the end of algorithm.

Solution 5

It doesn't look like your string is null terminated.

Share:
11,510
rjovic
Author by

rjovic

A self proclaimed bash guru and guacamole lover!

Updated on August 06, 2022

Comments

  • rjovic
    rjovic almost 2 years

    I had to build a C program which convert's infix notation into postfix notation using STACK. That went well and it's working in some way. It was long ago when I used last time C language so I'm probably dont use char[] variables very well.

    So problem is that when I give input like this :

    A+B*(C*E-D)
    

    My program returns this :

    ABCE*D-*+ĚĚĚĚĚĚĚĚĚĚĚ
    

    So as you see my program did postfix conversion very well but I have bunch of "garbage" chars at my result (ĚĚĚĚĚĚĚĚĚĚĚ).

    Here is snippet of my code (only part that I think is not correct, maybe something with char[] and way how I assing value to postfix[] variable:

    int main()
    {
        char infix[20], postfix[20];
        int len, tip, i, p=0;
    
        STACK pom;
        MAKE_NULL(&pom);
    
        printf ("Unesi izraz.\n");
        scanf ("%s", infix);
    
        len = strlen(infix);
    
        for(i=0; i<len; i++)
        {
            tip = nadi_tip(infix[i]);
    
            if (tip == Lijeva)
            {
                PUSH (infix[i], &pom);
            }
    
            if (tip == Operand)
            {
                postfix[p] = infix[i];
                p++;
            }
    
            if (tip == Desna)
            {
                while (!EMPTY(pom) && (TOP(pom)!= '('))
                  {
                    postfix[p++] = TOP(pom);
                    POP (&pom);
                  }
                POP (&pom);
            }
    
            if (tip == Operator)
            {
                while (!EMPTY(pom) && TOP(pom)!= '(')
                 {
                    if(prioritet(infix[i]) <= prioritet(TOP(pom)))
                    {
                      postfix[p++] = TOP(pom);
                      POP (&pom);
                    }
                    else break;
                 }
                 PUSH(infix[i], &pom);
            }
        }
     while (EMPTY(pom) != 1)
     {
        postfix[p++] = TOP(pom);
        POP(&pom);
     }
    
     printf("Izlaz: %s", postfix);
     return 0;
    
    }
    

    infix[] is my input and postfix[] is my output. What did I do wrong I why am I having ĚĚĚĚĚĚĚĚĚĚĚ characters. Thank you in advance!