Simple C program executed in the terminal with prompt alogside the final output

StackOverflow https://stackoverflow.com/questions/22161966

  •  19-10-2022
  •  | 
  •  

سؤال

I have written a simple "hello, world" program from here http://www.cprogramming.com/tutorial/c/lesson1.html cprogramming: lesson 1, the output is shown then the terminal prompt shows up in the following one. When executing another program:

#include <stdio.h>

main()
{
int age;
printf("How old are you?");
scanf("%d", &age);
if (age <= 20) 
{
printf("You are still young");
}
else if (age >= 20)
printf("You are not that young anymore!");
else if (age >= 30)
printf("Hello young man!\n");
getchar();
return 0;
}

The output is shown in the same line as the terminal prompt in Gnome Terminal 3.6.1 on Ubuntu 13.10. I just don't know if this a code issue or is it related to the terminal only.

هل كانت مفيدة؟

المحلول 2

Add a newline character after each of your printf's

example:

int age;
printf("How old are you?\n");
scanf("%d", &age);

نصائح أخرى

Alignment can be done using escape sequences. In your case u need to use \n for new line

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top