-4

So i have this "program" in python3 which adds 3 strings but the middle part x times. But i decided to choose C as my first language after doing some simple stuff on Python 3. NOW this is how it looks in Python:

def printWord(begin, mid, end, r):
    print(begin, end="")
    for i in range(r+1):
        print(mid, end="")
    print(end)
begin = input("Enter your beginning.\t")
mid =input("Enter your middle.\t")
end = input("Enter your ending.\t")
r = int(input("How many times?\t"))

printWord(begin, mid, end, r)

So this works and every thing is cool!

Now C:

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

int makeWord(char begin[], char mid[], char end[], int r)
{
    int i;
    printf("%s", begin);
    for(i=0;i<=r;i++){
        printf("%s", mid);
    }
    printf("%s", end);
    return 0;
}

int main()
{
    int r;
    char begin[3], mid[2], end[3];
    printf("Enter the beginning!\t");
    scanf("%s", begin);
    printf("Enter the middle!\t");
    scanf("%s", mid);
    printf("Enter the ending!\t");
    scanf("%s", end);
    printf("How many times?\t");
    scanf("%d", r);

    makeWord(begin, mid, end, r);

    return 0;
}

But this one stops

Now my question WHY? Thanks for answering

Honey.Sh
  • 19
  • 3

1 Answers1

1

You're misusing scanf. It takes pointers to the target variables, while you're passing the variables themselves - look at the documentation online.

This causes all of your variables to be uninitialized and contain garbage values - using them is therefore undefined behavior.

Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416