Er........wat is the command for me to display the date of today, according to the system date onto the screen display?
Also, is it possible for me to create a time control, which isl ike a clock, whereby the time is updated real-time each second?
Printable View
Er........wat is the command for me to display the date of today, according to the system date onto the screen display?
Also, is it possible for me to create a time control, which isl ike a clock, whereby the time is updated real-time each second?
>wat is the command for me to display the date of today
The simplest is:
>which isl ike a clock, whereby the time is updated real-time each second?Code:#include <stdio.h>
#include <time.h>
int main(void)
{
time_t today;
if ((today = time(0)) != (time_t)-1)
printf("%s", ctime(&today));
return 0;
}
Yes, here is the simplest way (which is utterly useless, so I didn't bother making it nice):
To have an interactive clock like this you need to either spawn another process, or make use of multithreading. Otherwise the program will run and all you will be able to do is look at it and do nothing else.Code:#include <stdio.h>
#include <time.h>
int main(void)
{
time_t hereandnow;
struct tm *today;
char buff[BUFSIZ];
while (1)
{
hereandnow = time(0);
today = localtime(&hereandnow);
strftime(buff, BUFSIZ, "%I:%M:%S", today);
printf("%s\r", buff);
fflush(stdout);
}
return 0;
}
I have 3 questions about your code:
1) what is the condition of the while statement? It never stops.Code:while (1)
{
hereandnow = time(0);
today = localtime(&hereandnow);
strftime(buff, BUFSIZ, "%I:%M:%S", today);
printf("%s\r", buff);
fflush(stdout);
}
2) What %I, %M, %S represent?
3) What's the use of fflush(stdout) ?
Thanks in advance
It should be interrupted from the keyboard(Ctrl-c, for example).Quote:
1) what is the condition of the while statement? It never stops.
The standard I/O streams are buffered, which means you can't assume that data will be written immediately after the function call. fflush() flushes the buffer, forcing a write.Quote:
3) What's the use of fflush(stdout) ?
Well, you asked "Also, is it possible for me to create a time control, which isl ike a clock, whereby the time is updated real-time each second?". The only way to update every second is to run. If you want a way out, you have to add code that tells the loop when to break;Quote:
Originally posted by AProg
I have 3 questions about your code:
1) what is the condition of the while statement? It never stops.
Look up the function. The documentation explains all.Quote:
2) What %I, %M, %S represent?
Look up the function. The documentation explains all.Quote:
3) What's the use of fflush(stdout) ?
Try Googling for your answers
>The standard I/O streams are buffered, which means you can't assume that data will be written immediately after the function call. fflush() flushes the buffer, forcing a write.
[italic]int fflush(FILE *stream);
Flushes a stream.
[/italic]
I searched a lot about the above discription but my work established fruitless. You see, English is not my mother tonque, so i have some difficulties in understanding what "flush" means. I turned that word up in my dictionnary, but the tranlslated word didn't help me so much to understand what ffluch() does.
Would you mind explaining me the purpose of fflush in other words pls?
>Would you mind explaining me the purpose of fflush in other words pls?
The input buffer is like filling a pail of water*. Most of the time it's useful to fill the pail to the top before you pour it out, you get more water for your work that way. But, there are times where you don't want a full pail, you just fill it a bit and the pour out what you have, you're calling fflush on the pail. :)
* Input stream, get it? :D
Ok, but why wouln't someone want a full pail, and just fill it a bit and then pour out what he have?
>Ok, but why wouln't someone want a full pail, and just fill it a bit and then pour out what he have?
What if you want to water a little plant and a full pail is too much? Sure, you could fill it up and use a little, but then you would have left over water and need to pour it out elsewhere (what a waste). Sure, you could save it, but then you wouldn't have a pail to use for other things.
It's ok with the tail, but how is that relevant to the fflush.
Could you say: "Flushes a stream" whithout using the world "flushes" and not changing the meaning?
>"Flushes a stream" whithout using the world "flushes" and not changing the meaning?
I haven't changed the meaning once, "flush the stream" means "empty the contents". In my analogy, the pail is the buffer, when you empty the pail, you are flushing the buffer. It really isn't that difficult.
>"empty the contents"
Sorry for asking again, but what contents do you refer to?
>Sorry for asking again, but what contents do you refer to?
The contents of the buffer. The following may not write directly to stdout:
printf("Hello, world!");
Because there's no newline character printed and no call to fflush, the string may be placed in a temporary buffer because calling a low level write function to place the string directly onto stdout can be expensive. When the buffer is full, then it is written as a whole to stdout. You can cause this buffer to be emptied prematurely by placing a newline at the end of the string:
printf("Hello, world!\n");
or calling fflush on stdout:
printf("Hello, world!");
fflush(stdout);
>printf("Hello, world!");
>fflush(stdout);
So, in this example, it would be the same if i placed " printf( "\n" ); ", istead of " fflush( stdout ); " ?
no because you wouldn't get a newline with fflush.
Ok, thanks
P.S. That's why it is so recomended to put '\n' at the end of the printf statement...
Um no. Only if you want a new line on your screen. Otherwise don't.Quote:
Originally posted by AProg
Ok, thanks
P.S. That's why it is so recomended to put '\n' at the end of the printf statement...
Quzah.
What do you mean by "may"? What causes this "may" to occur?Quote:
Originally posted by Prelude
Because there's no newline character printed and no call to fflush, the string may be placed in a temporary buffer because calling a low level write function to place the string directly onto stdout can be expensive.
Is it like a 50/50 thing? You'd think computers would be more exact.
>What do you mean by "may"? What causes this "may" to occur?
>Is it like 50/50 thing? You'd think computers would be more exact.
It depends on the host environment. Take a tiny example.I build this and run it in Visual SlickEdit's Build shell and I get a blank stare (no prompt). On the same machine I run it in a regular Windows command shell and I see the prompt. Same machine, same binary -- different hosts (can) do different things.Code:#include <stdio.h>
int main(void)
{
printf("Press any key to continue...");
getchar();
return 0;
}
"May" means the programmer didn't bother to ensure that a prompt should be seen - it was left up to the host to decide. If the programmer can't afford "may", then it's up to the programmer to be more exact.
Hmm.... I always thought that exiting a program automatically flushed all bufferes. Obviously there are some systems where thist is not true. I consider that very wierd, and non-friendly.