C - How To Create Delay Function According To Program Need?
Maybe your like
Home » Programming Tips & Tricks » C - Tips & Tricks
C - How to create delay function according to program need?By: IncludeHelp, on 22 JAN 2017
Simple way to create delay function by running a loop certain time, let suppose while (1) is running 333333333 times in a second.
Based on this count we can create our own delay function, Here is the function
void delay(int seconds){ unsigned long int count=333333333,i,j; for(i=0;i<seconds;i++) for(j=0;j<count;j++); }Consider the following program, which is printing text with current time after 1 and then 2 seconds delay:
#include <stdio.h> #include <time.h> //time related////////// time_t rawtime; struct tm * timeinfo; //////////////////////// void delay(int seconds){ unsigned long int count=333333333,i,j; for(i=0;i<seconds;i++) for(j=0;j<count;j++); } void printTime(void){ time ( &rawtime ); timeinfo = localtime ( &rawtime ); printf ( "Current local time and date: %s\n", asctime (timeinfo) ); } int main() { printf("Text1\n"); printTime(); delay(1); //delay for 1 second printf("Text2\n"); printTime(); delay(2); //delay for 2 seconds printTime(); printf("Text3\n"); return 0; }Output
Text1 Current local time and date: Sun Jan 22 20:41:12 2017 Text2 Current local time and date: Sun Jan 22 20:41:13 2017 Current local time and date: Sun Jan 22 20:41:15 2017 Text3Related Tutorials
- Extracting digits of a number using C program without using modules and divide operator
- Different methods to print "Hello world" without using semicolon in C
- Benefits of using '#define' to declare a constant in C/C++
- How to separate 'words' while declaring an identifier in C language?
- Declaring a function within the main() function in C language
- Terminate any string from given index in C language
- Correct way to declare and define a function in C
- Infinite for loop without condition in C
- How to use Macro instead of 'Equal To' operator to reduce 'Equal To' or 'Assignment' operator confusion in C
- One line form of if else and looping statements
- How should we declare/define a pointer variable?
- C comments within a statement
- How to initialize array elements with hexadecimal values in C?
- Why we should use switch instead of if else?
- Print maximum value of an unsigned int using One's Compliment (~) Operator in C
- An amazing trick to print maximum value of an unsigned integer in C
- A funny trick to use C++ in C language program
- How can we use a single byte to store 8 values in C?
- Check EVEN or ODD without using Modulus (%) Operator in C
- A safest way to check value using 'Equal To' (==) operator in C
- Comparing fixed number of characters of two strings in C language
- Replacing a part of string in C
- Why should we use 'f' with float literal in C?
- C - Pre-Increment is faster than post-increment
- C - Fastest way to copy two bytes integer number (short int) into byte buffer
Comments and Discussions!
Load comments ↻
Tag » How To Delay Code In C
-
How To Delay In C: 7 Steps (with Pictures) - WikiHow
-
Time Delay In C - GeeksforGeeks
-
Delay Function In C | Programming Simplified
-
How Do You Use The Time Delay In C Programming? - Quora
-
The Delay() Function | C For Dummies Blog
-
Implement Time Delay In C - Stack Overflow
-
5 Techniques To Delay Code Execution | Beningo Embedded Group
-
C Examples - Time Delay | ArunEworld
-
C Programming - How To Implement And Use Delays - YouTube
-
C Delay Code Example - Code Grepper
-
How To Use Delay Option In C Programing In Code Block - Sololearn
-
Time Delay Loops
-
How To Wait For Seconds In C++? - Java2Blog
-
Delay Routine Function In C Code | All About Circuits