Sri Harsha
Sri Harsha

Reputation: 123

How to use delay() function in c using codeblocks 13.12(mingw)?

When I write a program containing delay the compiler shows the error E:\c programms\ma.o:ma.c|| undefined reference to `delay'| ||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

Upvotes: 1

Views: 68652

Answers (3)

Abdulkader
Abdulkader

Reputation: 131

Try this function:

#include <time.h> // clock_t, clock, CLOCKS_PER_SEC

void delay(unsigned int milliseconds){

    clock_t start = clock();

    while((clock() - start) * 1000 / CLOCKS_PER_SEC < milliseconds);
}
  • The parameter milliseconds is a nonnegative number.
  • clock() returns the number of clock ticks elapsed since an epoch related to the particular program execution.
  • CLOCKS_PER_SEC this macro expands to an expression representing the number of clock ticks per second, where clock ticks are units of time of a constant but system-specific length, as those returned by function clock.

Upvotes: 0

Rizwan Raza
Rizwan Raza

Reputation: 19

You can use your own created delay() function for delaying statements for milliseconds as passed in parameter...

Function Body is below.....


#include<time.h>
void delay(unsigned int mseconds)
{
    clock_t goal = mseconds + clock();
    while (goal > clock());
}


Just Paste the above code in your C/C++ source file...

example program is below...

#include<stdio.h>
#include<time.h>
void delay(unsigned int mseconds)
{
    clock_t goal = mseconds + clock();
    while (goal > clock());
}
int main()
{
    int i;
    for(i=0;i<10;i++)
    {
    delay(1000);
    printf("This is delay function\n");
    }
    return 0;
}

Upvotes: 2

Armali
Armali

Reputation: 19385

Try including windows.h and use Sleep(sleep_for_x_milliseconds) instead of delay() – Cool Guy

Upvotes: 2

Related Questions