FREEZX
FREEZX

Reputation: 13

Printf somehow changes something?

Ok, so i have the following C code

#include <cstdio>
#include <cstring>

// funkcija za mnozenje na dva 8-bitni broja (vo RC format) so Butov algoritam
// vlez:    a[] - mnozenik, b[] - mnozitel
// izlez:   proizvod[] - proizvodot (mnozenik * mnozitel)


void shiftRight(char niza[])
{
    char out[100];
    strncpy(out, niza, 1);
    strcat(out, niza);
    out[17]='\0';
    strcpy(niza, out);
}


void add(char opa[], char opb[])
{
    char rez[100];
    strcpy(rez, opa);
    char carry='0';
    int i=16;
    while(i>=0)
    {
        int car=carry-'0';
        int currbita=opa[i]-'0';
        int currbitb=opb[i]-'0';
        rez[i]=((car+currbita+currbitb)%2)+'0';
        if(car+currbita+currbitb>=2)
        {
            carry='1';
        }
        else
            carry='0';
        i--;
    }
    strcpy(opa, rez);
}

void vtorKomplement(char in[], char out[])
{
    strcpy(out, in);
    for(int i=0; i<8; i++)
    {
        if(out[i]=='0')
            out[i]='1';
        else
            out[i]='0';
    }
    int i=7;
    char carry='1';
    while(carry!='0')
    {
        int car=carry-'0';
        int currbit=out[i]-'0';
        if(car+currbit>=2)
        {
            carry='1';
        }
        else
            carry='0';
        out[i]=((car+currbit)%2)+'0';
        i--;
    }
}

void mnozenjeButov(char a[], char b[], char proizvod[]) {
    int i;
    char rez[100];
    char A[100];
    char S[100];
    char P[100];
    strcpy(A, a);
    strcat(A, "000000000");
    vtorKomplement(a, S);
    for(i=8; i<17; i++)
    {
        S[i]='0';
    }
    S[17]='\0';
    strcpy(P, "00000000");
    strcat(P, b);
    strcat(P, "0");
    for(int i=0; i<8; i++)
    {
        if(P[15]=='0'&& P[16]=='1')
        {
            add(P, A);
        }
        else if(P[15]=='1' && P[16]=='0')
        {
            printf("Before add P: %s\n", P);
            add(P, S);
        }
        shiftRight(P);
        printf("Shifted P: %s\n", P);
    }
    for(int i=8; i<17; i++)
    {
        proizvod[i-8]=P[i];
    }
    proizvod[8]='\0';
}

int main() {
    int success = 1;

    char a[100];
    char b[100];
    char proizvod[100];
    char w_proizvod[100];

    // TEST 1
    strcpy(a, "00010011");
    strcpy(b, "00000101");
    strcpy(w_proizvod, "01011111");
    mnozenjeButov(a, b, proizvod);
    printf("TEST 1: %s, %s\n", a, b);
    printf("  Tocen odgovor:    %s\n", w_proizvod);
    printf("  Vas odgovor:      %s\n", proizvod);

    if (strcmp(proizvod, w_proizvod) == 0) {
        printf("Vasata programa dava tocen rezultat :-)\n\n");
    } else {
        printf("Vasata programa dava netocen rezultat!\n\n");
        success = 0;
    }

    if (success == 1) {
        printf("Vasata programa gi pomina testovite uspesno!\n");
    } else {
        printf("Nekoi od testovite bea neuspesni.\n");
    }

    return 0;
}

all is well and good, but something weird happens when i remove printf("Before add P: %s\n", P); and/or the printf after that. Then the output somehow changes, and some characters appear that shouldn't be there... I tried debugging, but then i get the normal output. I also tried testing on a different machine, and i also get the weird characters there. I've been banging my head for the last hour, can someone tell me where i'm going wrong? I'm using codeblocks with mingw GCC compiler.

Update: Jens Gustedt's solution worked.

Upvotes: 0

Views: 403

Answers (1)

Jens Gustedt
Jens Gustedt

Reputation: 78903

There is a conceptual error in these two lines:

strncpy(out, niza, 1);
strcat(out, niza);

strncpy here only copies exactly one character. In particular out[0] is equal to niza[0] and out[1] is whatever has been there before. Your strcat then writes niza to the next position where a 0-character is found, which can have catastrophic results. (The man page for strncpy says well so.)

To be able to do strcpy afterwards, you'd probably have to place a '\0' in there. But there is a much easier solution:

out[0] = niza[0];
strcpy(out + 1, niza);

Upvotes: 2

Related Questions