Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I was asked to write a program to blink the given word without using clrscr function in c and I am tring this following code when I add \n in the printf then only it works fine and when I remove \n it doesnt display anything why this is so?
#include <stdio.h>
#include <unistd.h>
int main()
{
int j=10;
while (j--)
{
printf("BLINK");
sleep(1);
system ("clear");
sleep(1);
}
return 0;
}
Buffering.
Output to stdout (which is where printf writes) is by default line buffered. That means the text written to stdout is put into a buffer only, and is not flushed (actually written to the device) unless either the buffer is full, or a newline is written.
Information about this should be in any good book, tutorial or class.
You can solve your problem by adding a flush of stdout.
#include <stdio.h>
#include <unistd.h>
int main()
{
int j=10;
while (j--)
{
printf("BLINK");
fflush(stdout);
sleep(1);
system ("clear");
sleep(1);
}
return 0;
}
But system("clear") will probably make code reviewers cry blood, so maybe consider something like backspace escape sequence.
#include <stdio.h>
#include <unistd.h>
int main()
{
int j=10;
while (j--)
{
printf("BLINK");
fflush(stdout);
sleep(1);
printf("\b\b\b\b\b \b\b\b\b\b");
fflush(stdout);
sleep(1);
}
return 0;
}
Related
This question already has answers here:
What is it with printf() sending output to buffer?
(3 answers)
Closed 1 year ago.
My code:
#include <stdio.h>
#include <unistd.h>
int main(){
printf("I sleep\n");
sleep(3);
printf("\033[H\033[J");
return 0;
}
if I don't write "\n" in the printf-function, "I sleep" wil not be displayed, until sleep(3) is done.
Can someone explain? Thanks!
The standard output stream, when not outputting to a file or device, is line-buffered by default on most UNIX-like systems.
That mean that text sent there typically won't actually be printed until a newline character is encountered.
This question already has answers here:
Why does printf not flush after the call unless a newline is in the format string?
(10 answers)
Closed 1 year ago.
I want to print a line and then get it erased after ten seconds.
So I thought that I should write a timer as a preliminary programme and with erasure I could deal later using whatever I find on the internet (using \r for example).
And so I want to print "I love nachos" and ten seconds later "I hate nachos".
However, even though printf("I love nachos") is written before any conditions, the programme prints both strings after ten seconds.
#include <stdio.h>
#include <time.h>
int main(void) {
printf("I love nachos");
time_t now = time(NULL);
while (1)
{
time_t future = time(NULL);
if ((future-now)==10)
{
break;
}
}
printf("I hate nachos");
return 0;
}
I asked my friend to tackle the problem and he wrote:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
printf("Start\n");
time_t t0 = time(NULL);
while ( (difftime(time(NULL), t0)) <= 10.0 )
{}
printf("End");
return 0;
}
However, in that case the programme only works with \n in printf("Start\n").
If I remove \n I get the same result as per my initial code and I obviously don't want \n there, because I wouldn't be able to erase the line with \r
The printf output to stdout is by default line buffered. That means nothing is actually sent out until either the internal buffer is full or a newline is added or the FILE is closed (as happens implicitly when the program exits)
So either add a newline or use fflush to force the buffer to be transmitted immediately to the output.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
So let's say you have a C program, that calls a function int foo() before returning control to main(), then main() uses puts() to write to the console.
What are some ways to prevent puts from writing to the console?
The only #includes you get are stdio and stdlib. You cannot touch any of the code in main(), only foo().
#include <stdio.h>
#include <stdlib.h>
unsigned int foo()
{
//Your code here
return 0;
}
int main()
{
foo();
puts("If you are reading this you have failed");
return 0;
}
Just redirect stdout to somewhere else (like a normal file or a character device like /dev/null) with freopen(). See the sample there
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
puts("stdout is printed to console");
if (freopen("redir.txt", "w", stdout) == NULL)
{
perror("freopen() failed");
return EXIT_FAILURE;
}
puts("stdout is redirected to a file"); // this is written to redir.txt
fclose(stdout);
}
Adapting this to your particular case should be simple
At //Your code here, put exit(EXIT_SUCCESS);.
This question already has answers here:
Why do programs sometimes "skip over" printfs?
(4 answers)
Closed 7 years ago.
This C program prints Done, then enters an infinite loop.
#include <stdio.h>
int main(int argc, char const *argv[])
{
printf("Done");
while (1) {}
return 0;
}
But when I run it, this code does not print Done. Why is that?
It needs to flush the buffer. Usually console output doesn't flush until it receives a '\n'.
There's a little-used function for this, fflush().
#include <stdio.h>
int main(int argc, char const *argv[])
{
printf("Done");
fflush(stdout);
while (1) {}
return 0;
}
That should print for you.
stdout is buffered. It won't print until its flushed (e.g., but hitting a newline character).
You could just add a '\n' to your printf call:
printf("Done\n");
Or disable its buffing by calling
setbuf(stdout, NULL);
Add fflush(stdout); after your printf statement. stdout is usually line-buffered by default.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
#include <stdio.h>
int main()
{
float a, b, c;
printf("Enter three numbers: ");
scanf("%f %f %f", &a, &b, &c);
if (a>=b)
{
if(a>=c)
printf("Largest number = %.2f",a);
else
printf("Largest number = %.2f",c);
}
else
{
if(b>=c)
printf("Largest number = %.2f",b);
else printf("Largest number = %.2f",c);
}
return 0;
}
when I compile. the code will scan for the 3 numbers but wont do anything afterwards. even if i put {} around every if and else statements, it wont change.
As Shubham suggested, try putting something at the end of the program that prevents the windows command line interface from closing instantly.
A getchar() from stdio.h is more appropriate than getch() from conio, because it's in the standard library.
If you run the program from a command line interface and not by double clicking the icon or hitting the run-button in the IDE your program runs fine without the getchar(), as you would expect.
You can also check if your IDE supports an option to leave the command line interface open after the program has terminated.
Another option is setting a breakpoint on the last line of your main() function.
Try putting a getch() before the return 0 statement. Don't forget to #include <conio.h>.
What is happening is that the program displays the result and closes immediately.
EDIT:
Assuming you are on windows
you have include one more header file like conio.h . you may aware of this one....sorry to remember you this basic header file for output window.......then before your return 0; line just write getch();
Sample like:
#include <stdio.h>
#include <conio.h>
int main()
{
/* your code to check conditions for which number is greater among these numbers */
getch();
return 0;
}