Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Register Log in

Doubt in C programming

Status
Not open for further replies.

navenmou

Full Member level 4
Joined
Sep 25, 2010
Messages
228
Helped
49
Reputation
98
Reaction score
46
Trophy points
1,318
Location
Bangalore, India
Activity points
2,588
Hi all

I have some basic doubts in C programming...

1.What is difference between for loop and while loop

2. What is difference between structure and union.

3. What is difference between typedef and macro

4.what is difference between logical AND & Bitwise AND

5.What is volatile variable??
 

horace1

Advanced Member level 5
Joined
Nov 18, 2008
Messages
2,123
Helped
596
Reputation
1,188
Reaction score
573
Trophy points
1,393
Location
Norwich, UK
Activity points
13,071
to start a for() is a shorthand version of a while(), i.e.
Code:
for (expression1; expression2; expression3)
       statement
next statement
is shorthand for
Code:
expression1;
while (expression2)
       {
       statement
       expression3;
       }
next statement
have a look at the tutorial for the rest of the answers
C++ Language Tutorial - C++ Documentation
 

horace1

Advanced Member level 5
Joined
Nov 18, 2008
Messages
2,123
Helped
596
Reputation
1,188
Reaction score
573
Trophy points
1,393
Location
Norwich, UK
Activity points
13,071
That i know already...but both of them functionality same right??
yes, typically if the
Code:
for (expression1; expression2; expression3)
is longer than a line use a while otherwise the for() can become difficult to read (in particular if you use the , operator in the expressions)
 

betwixt

Super Moderator
Staff member
Joined
Jul 4, 2009
Messages
14,447
Helped
4,744
Reputation
9,503
Reaction score
4,515
Trophy points
1,393
Location
Aberdyfi, West Wales, UK
Activity points
123,227
"for" loop is generally used when the start and end parameters are known
"while" loop is generally used when the end parameter is variable and might change according to program flow.

"struct" is a named collection of variables where each has a known size and are stored consecutively.
'union' is a named collection of variables where each has a known size and are stored in the same memory space (they can overlap).

'typedef' is a way of renaming a variable type or structure to make the program easier to read. When the 'typedef' name is encountered, the original variable/structure is used by the compiler.
'macro' is a way of grouping one or more statements, sometimes with parameter passing, so they can be used with a single command. The compiler expands the named macro back to the original statements each time it is used.

'logical AND' returns the result of two statements, it is true if the first statement AND the second statement are both true.
'bitwise AND' performs a bit by bit comparison of the binary bits in two numbers, it returns a '1' when the corresponding bits in both numbers is also '1'.

A 'volatile' variable is one in which the value might not be retained as it leaves the scope of a function.

Brian.
 

horace1

Advanced Member level 5
Joined
Nov 18, 2008
Messages
2,123
Helped
596
Reputation
1,188
Reaction score
573
Trophy points
1,393
Location
Norwich, UK
Activity points
13,071
volatile tells to the compiler that the contents of a variable are subject to unpredictable alterations and references to it must not be optimised, e.g. a variable which may be changed by an interrupt service routine, or an I/O register which will be changed by the action of the corresponding device
 

alexan_e

Administrator
Joined
Mar 16, 2008
Messages
11,895
Helped
2,020
Reputation
4,156
Reaction score
2,031
Trophy points
1,393
Location
Greece
Activity points
64,377
Yes, volatile prevents the compiler from optimizing or allocating the variable to a register.
It makes the compiler read the variable value from the memory location instead of a cache (register) so that it obtains always the correct (latest) value.

Alex
 

N vijay kumar

Junior Member level 1
Joined
Feb 28, 2011
Messages
18
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,283
Location
hyderabad
Activity points
1,395
yes,
we can declare some variables as volatile which are most accessed in the code ....
for example in a for loop we can declare the count variable as volatile........

volatile variables are fast to access by the compiler as they are directly available as other are from cache..........
time will be reduced if we declare variable as volatile......
but we should not declare all as volatile ...........
 

alexan_e

Administrator
Joined
Mar 16, 2008
Messages
11,895
Helped
2,020
Reputation
4,156
Reaction score
2,031
Trophy points
1,393
Location
Greece
Activity points
64,377
volatile variables are fast to access by the compiler as they are directly available as other are from cache..........
time will be reduced if we declare variable as volatile......
but we should not declare all as volatile ...........
No, volatile variables are not optimized and are slower than the other variables.
They are never cached in a machine register and this makes the access to them slower.

What you describe is the register variable which is allocated to CPU registers and has very fast access but the registers are limited so you can only do this for a few variables and makes a difference only for variables that are read very often.

Alex
 
Status
Not open for further replies.
Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top