-1

So i'm writing this code in C but when its time to print out the text why does it print it on the next line ? why ? and how do i print it in one complete line ?

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

/* Function Declerations */

/* Global Variables */
char *str = NULL, *str2 = NULL;
int i, q;

int main(){
    /* Initializing Global Variables */
    i = 6;
    q = 100;

    printf("Enter a string: "); // ask user to put in a string
    /* Initial memory allocation */
    str = (char *)malloc(i * sizeof(char));
    /* Get user input and print results */
    fgets(str, sizeof(str), stdin);
    // fflush(stdin);
    printf("String = %s, Address of String is = %p\n", str, str);
    
    printf("Enter a another string: "); // ask user to put in a string
    /* Reallocating memory */
    str = (char *)realloc(str, ((i + 100) * sizeof(char)));
    str2 = (char *)malloc(q * sizeof(char));
    fgets(str2, sizeof(str2), stdin);
    // fflush(stdin);
    strcat(str, str2);

    printf("String = %s, Address of String is = %p\n", str, str);

    free(str);
    free(str2);
    return 0;
}

/* Function Details */

So when i run this code why is it that the out put will print in the next line like so:

Enter a string: jason
String = jason
, Address of String is = 0000000000B62460
Enter a another string: hello
String = jason
hello
, Address of String is = 0000000000B66FE0

enter image description here

Oh Oh
  • 33
  • 3

0 Answers0