# two different delay functions in program

Status
Not open for further replies.

#### Parth86

##### Member
Hello
I have confused on following two programs. I think both program's are same but there are little difference. In first program, variable j declare inside bracket. while in another program, there is only empty space

C:
void delay (unsigned int j)
{
unsigned int j;
for (j = 0; j < 40000; j++);
}
C:
void delay ()
{
unsigned int j;
for (j = 0; j < 40000; j++);
}
Is there any difference difference between these two program's? why we write in two style?

#### elecLear78

##### Member
In your case both are same. But first one can modified to give delay as per the argument
Code:
void delay(unsigned int j)
{
unsigned int k;
for(k=0;k<j;k++)
{
;
}
From the main you can invoke like
Code:
int main(void)
{
delay(12000);
delay(13000);
retutn 0;
}

#### Ian Rogers

##### User Extraordinaire
Forum Supporter
Is there any difference difference between these two program's? why we write in two style?
No! they are the same, The first example will have a spare variable on the stack ( not a good idea.)

Normally we use the parameter to pass a variable.

C:
void delay(unsigned int j)
{
while(j--);
}
This gives a variable delay system..

#### Parth86

##### Member
Thanks Ian. I have another doubts following is program for LED blinking
C:
#include <REG51.h>
#define LED              P2
#define LED_ON      0xff
#define LED_OFF     0x00
void delay (unsigned int j)
{
unsigned int j;
for (j = 0; j < 40000; j++);
{

}
}

void main()
{

while (1)
{
LED = LED_ON;
delay(500);
LED = LED_OFF;
delay(5000);
LED = LED_OFF;

}
}
I don't understand exact use of while (1) statement in above program
C:
while (1)
My understanding it should be while (j=1). why write only while (1)
C:
while (condition) // while (j=1)
{
statements;
}

#### Ian Rogers

##### User Extraordinaire
Forum Supporter
C:
void delay (unsigned int j)
{
unsigned int j;
for (j = 0; j < 40000; j++);
{

}
}
The first int J will not be used here so delay(500) is a waste of typing... Only the local j will be used.

As for the while()... The compiler evaluates a Boolean answer.. Yes / No so while(1) will always be true..

while(1) {;;;;} is a foreverloop as 1 will always be 1.. Every thing in the curly brackets will be executed forever!

#### Parth86

##### Member
As for the while()... The compiler evaluates a Boolean answer.. Yes / No so while(1) will always be true..

while(1) {;;;;} is a foreverloop as 1 will always be 1.. Every thing in the curly brackets will be executed forever!
I saw this type of while statement in program
C:
 while (1)
{
LED = LED_ON;
delay(500);
LED = LED_OFF;
delay(5000);
LED = LED_OFF;

}
}
we can also use j==1
C:
 while (j==1)
{
LED = LED_ON;
delay(500);
LED = LED_OFF;
delay(5000);
LED = LED_OFF;
}
}
why do we use only while (1) statement and why don't we use while (j==1) instead of while(1) ?

Last edited by a moderator:

#### Ian Rogers

##### User Extraordinaire
Forum Supporter
why do we use only while (1) statement and why don't we use while (j==1) instead of while(1) ?
Because j is a variable.... Its easier to just write while(1) because j can change and 1 cannot!

have a lookz

#### be80be

##### Well-Known Member
And some code too see how it works
Code:
#include <stdio.h>

int main () {

/* local variable definition */
int a = 10;

/* while loop execution */
while( a < 20 ) {
printf("value of a: %d\n", a);
a++;
}

return 0;
}
while is set to 1 when you want it to run forever if you use a test it runs as long as test is true
the j==1 would run till j didn't equal 1 seeing j is the holder for your delay time i guess you don't need a delay cause 1 will happen one time

#### Parth86

##### Member
Because j is a variable.... Its easier to just write while(1) because j can change and 1 cannot!
okay , suppose following conditions
while (1)
if while is set to value (1) then run forever loop other wise don't run loop. does compiler set while to 1 and suppose if while (6) than what happen. does compiler store value of 6 at memory register automatically?

#### elecLear78

##### Member
The compiler only checks for the condition inside the while(condition). if condition is TRUE (which is non zero) value then it executes the statements inside the while loop. If the condition in the while(condition) is FALSE (value 0) then it comes out of while loop. since 1,6 are non zero values it keeps executing the statements inside the loop. The condition can be an expression or constant, the compiler will evaluate the condition to either TRUE or FALSE and based on that takes decision to either execute or not execute the while statements.

while(condition)
{
/* statements*/
}

#### be80be

##### Well-Known Member
That s not totally true the while loop works. One way if it's true like while 1 then it's forever looping
If it's a test for a value it loops to the value is met
While j < 20 so any value can end it.

Last edited:

#### be80be

##### Well-Known Member
okay , suppose following conditions
while (1)
if while is set to value (1) then run forever loop other wise don't run loop. does compiler set while to 1 and suppose if while (6) than what happen. does compiler store value of 6 at memory register automatically?
If you compled the code I posted you could see what happens the while loop runs till it hits 20

#### Ian Rogers

##### User Extraordinaire
Forum Supporter
if while is set to value (1) then run forever loop other wise don't run loop. does compiler set while to 1 and suppose if while (6) than what happen. does compiler store value of 6 at memory register automatically?
The main reason 1 is used is because 1 = true and 0 = false.. But! As you said it could be 1~255 and it will still work..

If you define true and false ( which the compiler already does ) You would write.
C:
#define true 1
#define false 0

void main(void)
{
while(true)
{
}
{
Which works exactly the same...

#### Parth86

##### Member
if you don't mind.. can I continue another topic in single thread
typedef is a reserved keyword in the C language. The struct keyword is used to define structure. the structure declaration look like following
C:
typedef struct
{
int i;
int j;
int k;
char a;
}my_str;
how to use struct statement in real program.
C:
#include <REG51.h>
typedef struct
{
int i;
int j;
int k;
char a;
}my_str;

void main()
{
while (1)
{
//write code here

}
}

#### Ian Rogers

##### User Extraordinaire
Forum Supporter
Structures are types.. It is still classed as a single variable..
C:
 struct
{
int i;
int j;
int k;
char a;
}a_structure;

// to initialise a structure you use
struct a_structure my_str;

// using typedef we can define the structure as one type
typedef struct
{
int i;
int j;
int k;
char a;
}a_structure;

// to initialise this structure you use
a_structure my_str;

//to use the elements you simple use the '.' operator..

my_str.i = 20;
//or
my_str.a = 'A';

#### Parth86

##### Member
Thanks. Please at look at following program
C:
typedef enum
{

// to initialize a
my_task task;
How to use enum statement in c program.

Last edited by a moderator:

#### Ian Rogers

##### User Extraordinaire
Forum Supporter
enum isn't a structure and hasn't a name

C:
enum{
White,
Blue,
Red,
Black,
};
Now White = 0, Blue = 1, Red = 2 and Black = 3..

enum enumerates identifiers for easy reading.

#### Parth86

##### Member
Now I understand what is enum. How to insert enum and case statement in program. I want to understand use of enum and switch statement in one program
C:
#include<reg51.h>    //header file

sbit   LED1     = P2^0;    //LED 1 conncted to port P2 pin 0
sbit   LED2     = P2^1;    //LED 2 conncted to port P2 pin 1
sbit   LED3     = P2^2;    //LED 3 conncted to port P2 pin 2

// prototype function
void DoSomething0(void);
void DoSomething1(void);
void DoSomething2(void);

typedef enum
{

int main(void)
{
{
DoSomething0();
break;

DoSomething1();
break;

DoSomething2();
break;
}

Last edited by a moderator:

#### Ian Rogers

##### User Extraordinaire
Forum Supporter
Enum isn't a structure!!
C:
typedef enum //<<--- No no!!
{
} my_task_t;  //<<-- No no...
C:
enum my_task_t //<<--- Yes yes!
{
};  //<<-- Yes yes..

Yo can always change the enum like this..
C:
enum my_task_t
{
} ;