LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-30-2016, 04:14 AM   #1
jimmy2447
LQ Newbie
 
Registered: Mar 2016
Posts: 9

Rep: Reputation: Disabled
Question 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.

Last edited by jimmy2447; 10-31-2016 at 11:39 PM.
 
Old 10-30-2016, 09:05 PM   #2
kaz2100
Senior Member
 
Registered: Apr 2005
Location: Penguin land, with apple, no gates
Distribution: SlackWare > Debian testing woody(32) sarge etch lenny squeeze(+64) wheezy .. bullseye bookworm
Posts: 1,832

Rep: Reputation: 108Reputation: 108
Hya

Two points to clarify.

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

cheers
 
Old 10-31-2016, 02:35 PM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,879
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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?
 
Old 10-31-2016, 03:23 PM   #4
luvr
Member
 
Registered: May 2005
Location: Boom - The Home Town of Tomorrowland, Belgium
Distribution: Slackware, Xubuntu
Posts: 459
Blog Entries: 2

Rep: Reputation: 194Reputation: 194
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);
 
2 members found this post helpful.
Old 10-31-2016, 07:46 PM   #5
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Quote:
Originally Posted by rtmistler View Post
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");
 
1 members found this post helpful.
Old 10-31-2016, 11:50 PM   #6
jimmy2447
LQ Newbie
 
Registered: Mar 2016
Posts: 9

Original Poster
Rep: Reputation: Disabled
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.
 
Old 11-01-2016, 12:02 AM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Most likely, the problem was the lack of fflush(stdout)
 
Old 11-01-2016, 12:15 AM   #8
jimmy2447
LQ Newbie
 
Registered: Mar 2016
Posts: 9

Original Poster
Rep: Reputation: Disabled
I did not used fflush but still code works fine, so why you are saying lack?
 
Old 11-01-2016, 01:05 AM   #9
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
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.

Last edited by NevemTeve; 11-01-2016 at 01:08 AM.
 
Old 11-01-2016, 03:58 AM   #10
luvr
Member
 
Registered: May 2005
Location: Boom - The Home Town of Tomorrowland, Belgium
Distribution: Slackware, Xubuntu
Posts: 459
Blog Entries: 2

Rep: Reputation: 194Reputation: 194
Quote:
Originally Posted by NevemTeve View Post
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.
 
Old 11-01-2016, 05:55 AM   #11
jimmy2447
LQ Newbie
 
Registered: Mar 2016
Posts: 9

Original Poster
Rep: Reputation: Disabled
okeh got it thanks.
 
  


Reply

Tags
clear, new line, not working


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How could a computer technician use the "top" command with "ps" and "kill" to investigate how a system is misbehaving? geckono1 Linux - Newbie 13 07-03-2016 07:51 AM
"clear" command not found Fedora 23 ECloud3 Linux - Newbie 4 05-26-2016 02:09 AM
[SOLVED] How to add the "dialout" group in my system without the command "groupadd"? floppy_stuttgart Linux - Networking 3 09-19-2013 02:55 PM
SSH X forwarding in program using "system" command JayKemper Linux - Networking 2 12-01-2012 02:46 PM
how do i replace the clear "clear screen" with the cls command thefedexguy SUSE / openSUSE 2 12-02-2005 05:02 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:04 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration