Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics 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.

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??
 

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
 

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)
 

"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.
 
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
 

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
 

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 ...........
 

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.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top