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)) ===|
How to use delay() function in c using codeblocks 13.12(mingw)?
68.2k Views Asked by Sri Harsha At
3
There are 3 best solutions below
0
On
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;
}
1
On
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.
Try including
windows.hand useSleep(sleep_for_x_milliseconds)instead ofdelay()– Cool Guy