Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How do you do something like this?

134 views
Skip to first unread message

G B

unread,
Feb 12, 2018, 8:29:17 PM2/12/18
to
Who can provide a two or three liner code in C or C++ to create a clock
like the one shown in this video?

<https://youtu.be/SVxcpL4AJGs>

Please watch it is full screen to know what I am asking about.

Ben Bacarisse

unread,
Feb 12, 2018, 9:02:52 PM2/12/18
to
G B <g...@gb.com> writes:

> Who can provide a two or three liner code in C or C++ to create a

Cross posting between C and C++ can lead to some... disagreements. I
read this in comp.lang.c first and my answer pertains to C.

> clock like the one shown in this video?
>
> <https://youtu.be/SVxcpL4AJGs>

For the time part, lookup the functions time, localtime and strftime in
whatever C reference you like to use.

For the output part, lookup fputs (or printf), fflush and the meaning of
the special character denoted by the escape sequence \r.

Enough hints? (I'm avoiding spoilers because I'm guessing this is some
sort of homework or assignment, and if not, at least some challenge
you've set for yourself.)

--
Ben.

Alf P. Steinbach

unread,
Feb 12, 2018, 9:53:11 PM2/12/18
to
In addition toe Ben's hints, check out `std::this_thread` for means to
avoid using all too much CPU time.

Cheers & hth.,

- Alf

bartc

unread,
Feb 13, 2018, 6:50:53 AM2/13/18
to
Why does it have to be 3 lines?

Anyway this is something I tried in Windows, to do the kind of display
shown in the video. I couldn't be bothered dealing with actual time:

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

int main(void) {
int n=0;

system("cls");

while (1) {
printf("%d\r",n);
++n;
Sleep(1000);
}
}


(This assumes the value being displayed will never get shorter. It will
do eventually but it will be decades hence. A time display however will
be probably always be the same width.)

--
bartc

Egor

unread,
Feb 13, 2018, 7:00:53 AM2/13/18
to
In comp.lang.c G B <g...@gb.com> wrote:
> Who can provide a two or three liner code in C or C++ to create a clock
> like the one shown in this video?

Here's how it's done on windows. If you need a different time format
you'll need to use localtime and strftime.

#include <stdio.h>
#include <time.h>
#include <windows.h>
int main() {
for(;;) {
time_t t = time(NULL);
char *str = ctime(&t);
str[24] = '\0'; /* remove the newline from string */
printf("\r%s", str);
fflush(stdout);
Sleep(500);
}
}

To sleep on unix, use the following code (requires time.h):
struct timespec ts;
ts.tv_sec = 0;
ts.tv_nsec = 500000000L; /* 500 million ns = 500 ms */
nanosleep(&ts, NULL);

Öö Tiib

unread,
Feb 13, 2018, 3:49:32 PM2/13/18
to
Who needs C or C++ for task like that?
Make a clock.bat file, something like that perhaps:

@echo off
setlocal enableextensions enabledelayedexpansion
for /F %%a in ('echo prompt $H ^| cmd') do set "BS=%%a"
:loop
timeout /t 1 > nul
set /p "=%date% %time%!BS!!BS!!BS!!BS!!BS!!BS!!BS!!BS!!BS!!BS!!BS!!BS!!BS!!BS!!BS!!BS!!BS!!BS!!BS!!

BS!!BS!!BS!!BS!!BS!!BS!!BS!!BS!!BS!!BS!!BS!!BS!!BS!!BS!!BS!!BS!" <NUL
goto loop


Then run it.


Richard

unread,
Feb 13, 2018, 4:34:38 PM2/13/18
to
[Please do not mail me a copy of your followup]

bartc <b...@freeuk.com> spake the secret code
<fiAgC.671$3X6...@fx22.am4> thusly:

> system("cls");

If you watch the video, it doesn't clear the screen.
--
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
The Terminals Wiki <http://terminals-wiki.org>
The Computer Graphics Museum <http://computergraphicsmuseum.org>
Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>

bartc

unread,
Feb 13, 2018, 5:01:16 PM2/13/18
to
On 13/02/2018 21:34, Richard wrote:
> [Please do not mail me a copy of your followup]
>
> bartc <b...@freeuk.com> spake the secret code
> <fiAgC.671$3X6...@fx22.am4> thusly:
>
>> system("cls");
>
> If you watch the video, it doesn't clear the screen.

Well, the screen was cleared at one point, if not by the program.
Perhaps the OP wanted the screen cleared completely and didn't know how.

Anyway it's an easy fix; just delete that line.

--
bartc

Juha Nieminen

unread,
Feb 19, 2018, 1:28:36 AM2/19/18
to
In comp.lang.c++ Egor <eg...@ruby.local> wrote:
> Sleep(500);

> nanosleep(&ts, NULL);

I thought C++11 introduced sleep_for(). Why not use the standard function?

Egor

unread,
Feb 22, 2018, 7:54:25 PM2/22/18
to
In comp.lang.c Juha Nieminen <nos...@thanks.invalid> wrote:
> I thought C++11 introduced sleep_for(). Why not use the standard function?
OP posted the question to both comp.lang.c and comp.lang.c++, so I
provided a C-oriented answer.

Didn't know about sleep_for in C++ though, so thanks for your addition.

James R. Kuyper

unread,
Feb 27, 2018, 8:31:12 AM2/27/18
to
On 02/22/2018 07:48 PM, Egor wrote:
> In comp.lang.c Juha Nieminen <nos...@thanks.invalid> wrote:
>> I thought C++11 introduced sleep_for(). Why not use the standard function?
> OP posted the question to both comp.lang.c and comp.lang.c++, so I
> provided a C-oriented answer.

A C-oriented answer using C2011 could use thrd_sleep()

Keith Thompson

unread,
Feb 27, 2018, 12:07:40 PM2/27/18
to
It could, but support for the C11 <threads.h> is still uncommon.

--
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Real Troll

unread,
Feb 27, 2018, 5:08:34 PM2/27/18
to
This one doesn't even compile in any of the compiler I have tried:

GNU G++,
Embarcadero
Visual Studio 2010, 2013, 2015, 2017

> #include <threads.h>
> #include <time.h>
> #include <stdio.h>

> int main(void)
> {
> printf("Time: %s", ctime(&(time_t){time(NULL)}));
> thrd_sleep(&(struct timespec){.tv_sec=1}, NULL); // sleep 1 sec
> printf("Time: %s", ctime(&(time_t){time(NULL)}));
> }


David Brown

unread,
Feb 28, 2018, 4:00:06 AM2/28/18
to
Works fine for me with gcc. Did you remember to use "-std=c11", or a
gcc version new enough to have "-std=gnu11" as the default? Did you use
a new enough gcc? (I think 4.9 is needed.) Did you use a library with
the right support (not a Windows gcc version that uses MS's old and
limited C libraries)?

Keith Thompson

unread,
Feb 28, 2018, 1:04:20 PM2/28/18
to
On Linux systems, gcc typically uses GNU libc by default, and GNU
libc doesn't support <threads.h>. Specifying -std=... doesn't help.
I don't think Visual Studio supports it either. (<threads.h> is
optional, so this doesn't mean these implementations are non-conforming
as long as they predefine __STDC_NO_THREADS__.)

On Debian-based systems, you can install an alternative C library,
MUSL, which does support <threads.h>. You have to use a wrapper
called musl-gcc to compile with it.

David Brown

unread,
Feb 28, 2018, 4:36:08 PM2/28/18
to
Oh, you are correct. The gcc I tested was using MUSL.

Egor

unread,
Mar 5, 2018, 11:37:59 AM3/5/18
to
In comp.lang.c James R. Kuyper <james...@verizon.net> wrote:
> A C-oriented answer using C2011 could use thrd_sleep()
Sadly, C11 support is still trash.
0 new messages