LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C-program is not able to print '\n' after system("clear") command (https://www.linuxquestions.org/questions/programming-9/c-program-is-not-able-to-print-%5Cn-after-system-clear-command-4175592516/)

jimmy2447 10-30-2016 04:14 AM

C-program is not able to print '\n' after system("clear") command
 
I have the following program:
Code:

main()
{
while(1)
 {
  system("clear");
  printf("\n");
  printf("hello");
  sleep(1);
 }
}

Output:
hello

Problem:
It is not printing new line, the programs somehow ignores it and directly prints "hello" on the first line of the terminal.
But when I was not using system command then '\n' works fine.

kaz2100 10-30-2016 09:05 PM

Hya

Two points to clarify.

1. #include ......
2. System buffer

cheers

rtmistler 10-31-2016 02:35 PM

Hi and welcome to LQ.

In the future, and note that you can edit your original post, you should use [code][/code] tags around your code. This will retain the spacing and formatting and make it easier to read.

You probably should explain, if this is the case, that you're just learning and experimenting. It's not a really purposeful program, and I'm the type of person who "grew up" being told to never use the system() call. :) Nothing bad, just more helpful for people to be able to help you.

So, a suggested solution: How about trying:
Code:

printf("\nhello");
as opposed to using two different lines of code?

luvr 10-31-2016 03:23 PM

First, you should #include the required header files, to declare the functions that you use in your program (i.e., printf, system, and sleep):
Code:

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

Next, it is recommended to specify the return type of your main function, as well as its argument list. Since you don't use a return statement, the void return type is most appropriate. Further, since you don't use any arguments, the argument list can be just void as well:
Code:

void main(void)
Finally, the standard output stream is buffered by default, resulting in possibly confusing output if you don't consistently write completed lines (i.e., ending with a newline). To overcome this issue, you may want to disable buffering for the standard output stream, by executing the following code as the first statement of your main function:
Code:

setbuf(stdout, NULL);
Alternatively, if you prefer to keep buffering enabled, then you will have to flush the standard output stream after you output an incomplete line (e.g., after you printf the "hello" string):
Code:

fflush(stdout);

keefaz 10-31-2016 07:46 PM

Quote:

Originally Posted by rtmistler (Post 5625310)
So, a suggested solution: How about trying:
Code:

printf("\nhello");
as opposed to using two different lines of code?

Maybe also an escaped command for clearing the terminal?
Code:

printf("\033\143\nhello\n");

jimmy2447 10-31-2016 11:50 PM

Thank you Guys for replying..
kaz and luvr was right, it was about stdout buffer, i used setbuf(stdout,NULL) and problem is now solved, and i used all that three header files it was not the issue.
thanks keefaz for "clear" alternative, and rtmistler thank you 4 ur help, thanks guys.

NevemTeve 11-01-2016 12:02 AM

Most likely, the problem was the lack of fflush(stdout)

jimmy2447 11-01-2016 12:15 AM

I did not used fflush but still code works fine, so why you are saying lack?

NevemTeve 11-01-2016 01:05 AM

You should add fflush after printf:
Code:

...
int main (void)
{
    while(1)
    {
        system("clear");
        printf("\n");
        printf("hello");
        fflush (stdout);
        sleep(1);
    }
}

That's because FILE-operations are allowed to do whatever buffering they sit fit.

luvr 11-01-2016 03:58 AM

Quote:

Originally Posted by NevemTeve (Post 5625490)
You should add fflush after printf

That would be needed in case buffered output was used, but the OP chose to disable buffering. Hence, no more need for fflush.

jimmy2447 11-01-2016 05:55 AM

okeh got it thanks.


All times are GMT -5. The time now is 02:56 PM.