Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7788987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:09:10+00:00 2026-06-01T21:09:10+00:00

Ok, so i have the following C code #include <cstdio> #include <cstring> // funkcija

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T21:09:13+00:00Added an answer on June 1, 2026 at 9:09 pm

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: #include <cstdio> template<class Fun, class... Args> void foo(Fun f,
I have the following bit of code, I expect that given cstdio is included
i have the following code: #include <stdio.h> int main(void) { float a[4] __attribute__((aligned(0x1000))) =
I have the following code: #include <string.h> int main(void) { char *buffer = NULL,
I have the following code: #include <iostream> #include boost/shared_ptr.hpp using boost::shared_ptr; class Base {
I have the following code: #include <boost/shared_ptr.hpp> struct Foo { int a; }; static
I have the following code: #include <stdlib.h> #include <stdio.h> #include <pthread.h> #define NUM_THREADS 100
I have the following code: #include <iostream> #include <vector> using namespace std; struct A{};
I have the following code: #include <iostream> #include Student.h <-- fixed the problem #include
I'm using gcc 4.3.2. I have the following code (simplified): #include <cstdlib> template<int SIZE>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.