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

int main()
{
    char name [50];
    char age [50];

    printf("Please enter your name:\n");
    fgets(name, 20, stdin);

    printf("Please enter your age:\n");
    fgets(age, 20, stdin);

    printf("Hello %s, you are %s years old", name, age);

    return 0;
}

//sorry for such a trvial question I am new to programming, but Why does the last printf create new lines after each placeholder?

1 Answers1

1

You are getting input from stdin, so you are probably ending your entries by hitting Enter: therefore you end your line with the \n character and fgets() stores it into the name and age variables.

Corentin Pane
  • 4,794
  • 1
  • 12
  • 29
  • Not ”probably”. For all practical purposes, it's a certainty that the inputs are terminated with a newline which is causing the line breaks in the output. – Jonathan Leffler Sep 26 '20 at 21:19