Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7665705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:38:35+00:00 2026-05-31T14:38:35+00:00

Why doesn’t this code print New time, more than once? And is it possible

  • 0

Why doesn’t this code print “New time”, more than once?
And is it possible to fix, without calling clock() outside the loop?

#include <stdio.h>
#include <time.h>
int main(char argv[], int argc)
{
    double lastTime = 0;
    while(1)
    {
        printf("New time:\n");
        while(lastTime == (lastTime = (double)clock() / (double)CLOCKS_PER_SEC))
        {
            printf("%f\n", lastTime);
        }
    }
    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T14:38:36+00:00Added an answer on May 31, 2026 at 2:38 pm

    This line won’t work as you might expect (I’ve added the closing brackets cause they were missing above):

    while(lastTime == (lastTime = (double)clock() / (double)CLOCKS_PER_S))
    

    Due to the brackets the right side of the comparison (the assignment) is evaluated first, essentially resulting in this:

    while(lastTime == lastTime)
    

    Which essentially leads to while(0 == 0), which is obviously while(true).

    You’ll need at least a second variable to hold the previous value. Then just add an extra case for when lastTime == 0 (i.e. the first time the loop is run).


    Also, as mentioned in a comment above, you shouldn’t compare the value as it’s not that easy for two double values to be exactly the same (esp. when calculated).

    If you’d like to do some action every x milliseconds (or any other time), you should use a counter to add up the time passed:

    for(clock_t timeSkipped = 0, lastTick = clock() ; ; timeSkipped += clock() - lastTick, lastTick = clock()) {
        while(timeSkipped > 0.5 * CLOCKS_PER_S) { // example: this will run the content of this loop at a fixed time step of once every half second
            timeSkipped -= 0.5 * CLOCKS_PER_S;
            printf("Tick!");
        }
    

    }

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why doesn't this code print test? #include <stdio.h> #include <stdlib.h> void foo ( void
Why doesn't this code work? I'am using FF. <head> <script type=text/javascript> document.getElementById(someID).onclick = function(){
Doesn't configuring WCF in code require more coupling with the model and service libraries?
This doesn't work: create table event ( ... time int default date_part('epoch', timestamp 'now'),
Why doesn't Html.ActionLink work in the below code? This is a page in the
Why doesn't this jQuery code work? $(document).ready(function () { $('currentPage').click(function() { $('myaccount').slideDown('slow', function() {
Why doesn't this work right? function test() { var start = new Date(2012, 3,
Why doesn't this Google Chart API URL render both data sets on this XY
Why doesn't this work (when parameter is set to 1) : SELECT * FROM
Doesn't matter what I do, I simply can't get this to play a sound

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.