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

  • Home
  • SEARCH
  • 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 9175613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:56:39+00:00 2026-06-17T16:56:39+00:00

Consider a while loop in ANSI C whose only purpose is to delay execution:

  • 0

Consider a while loop in ANSI C whose only purpose is to delay execution:

unsigned long counter = DELAY_COUNT;
while(counter--);

I’ve seen this used a lot to enforce delays on embedded systems, where eg. there is no sleep function and timers or interrupts are limited.

My reading of the ANSI C standard is that this can be completely removed by a conforming compiler. It has none of the side effects described in 5.1.2.3:

Accessing a volatile object, modifying an object, modifying a file, or calling a function that does any of those operations are all side effects, which are changes in the state of the execution environment.

…and this section also says:

An actual implementation need not evaluate part of an expression if it can deduce that its value is not used and that no needed side effects are produced (including any caused by calling a function or accessing a volatile object).

Does this imply that the loop could be optimised out? Even if counter were volatile?

Notes:

  1. That this is not quite the same as Are compilers allowed to eliminate infinite loops?, because that refers to infinite loops, and questions arise about when a program is allowed to terminate at all. In this case, the program will certainly proceed past this line at some point, optimisation or not.
  2. I know what GCC does (removes the loop for -O1 or higher, unless counter is volatile), but I want to know what the standard dictates.
  • 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-06-17T16:56:40+00:00Added an answer on June 17, 2026 at 4:56 pm

    C standard compliance follows the “as-if” rule, by which the compiler can generate any code that behaves “as if” it was running your actual instructions on the abstract machine. Since not performing any operations has the same observable behaviour “as if” you did perform the loop, it’s entirely permissible to not generate code for it.

    In other words, the time something takes to compute on a real machine is not part of the “observable” behaviour of your program, it is merely a phenomenon of a particular implementation.

    The situation is different for volatile variables, since accessing a volatile counts as an “observable” effect.

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

Sidebar

Related Questions

Consider this code: import socket store = [] scount = 0 while True: scount+=1
get current array position pointer while doing foreach $object->result_array() Hi, consider this, a case.
Consider this body of main : std::srand(std::time(nullptr)); while (std::rand()); Surprisingly, I couldn't find anything,
In the code below, I have a while(true) loop. Consider a situation where there
What is alternative to fetchall for real time loop? Consider this: $query = $db->prepare(SELECT
Consider the following piece of code: void MyRunningThread() { while(counter>0) // counter is a
Consider this JavaScript Code: <script type=text/javascript> function loop(Message){ document.getElementById('output').innerHTML = document.getElementById('output').innerHTML + Message +
consider a nested loop: for i from 1 to n k=i; while(k>0) do c
take this simple loop while(1) { $data = file_get_contents('randomfiles.img'); $resource = imagecreatefromstring($data); //> do
Let's consider this FOR loop in a Windows batch script: D:\MiLu\Dev\C++\temp :: type string.bat

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.