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.

Not getting what is expected from this code (gcc compiler)

Status
Not open for further replies.

Arya Kumar

Member level 4
Joined
Aug 25, 2013
Messages
73
Helped
11
Reputation
22
Reaction score
11
Trophy points
8
Activity points
713
Code:
#include <stdio.h>
 
//int sum(int);

int i;
void main()
{
//int d=sum(10);
volatile int a =10;

if(a==10)
{
a=20;
printf("good morning how are you");
}
else if(a==20)
{
printf("hello buddy");
}
else
{;}

}


I am expedting that both condition will be executed. As i have declared it volatile.
but this doesnt happen.
please explain..
FYI, C code Gcc compiler.
 

Just use 'switch' keyword withoud 'break'
Code:
char i = 10;
switch (i)
{
case 10: { printf("good morning how are you"); i = 20;}
case 20: { printf("i'm fine. Trying to teach Arya Kumar embedded C"); i = 30;}
case 30: { printf("and how is his results for this moment?"); i = 40;}
case 40: { printf("pretty bad you know"); i = 0xff; break;} 
}
 

Just use 'switch' keyword withoud 'break'
Code:
char i = 10;
switch (i)
{
case 10: { printf("good morning how are you"); i = 20;}
case 20: { printf("i'm fine. Trying to teach Arya Kumar embedded C"); i = 30;}
case 30: { printf("and how is his results for this moment?"); i = 40;}
case 40: { printf("pretty bad you know"); i = 0xff; break;} 
}

yeah buddy i know that will do..i was just checking for use of volatile. i was not doing like checking all conditions..
problem still not solved
 

The logic of your program flow looks wrong to me.

Just use "if" statements without the "else" conditions.
 

'Volatile' tells the compiler not to trust the value in the variable because it is likely to change at any time. The classic example being that it may be changed in mid-function because an interrupt occurred and modified the value. In general, you only need to use 'volatile' if the variable is used both inside and outside an interrupt routine. The compiler will defensively produce code to avoid the wrong or corrupted version of the variable being used.

Brian.
 

Code:
if(a==10)
{
a=20;
printf("good morning how are you");
}
else if(a==20)
As said, the behaviour has nothing to do with volatile.

In an if(cond) ... else...., it's mandatory by C specification that the condition is only checked once, in your example before a is reassigned. Once the condition has been evaluated by the if statement, the further execution path is determined. The else if will be only evaluated if a is initially != 10.
 

Code:
if(a==10)
{
a=20;
printf("good morning how are you");
}
else if(a==20)
As said, the behaviour has nothing to do with volatile.

In an if(cond) ... else...., it's mandatory by C specification that the condition is only checked once, in your example before a is reassigned. Once the condition has been evaluated by the if statement, the further execution path is determined. The else if will be only evaluated if a is initially != 10.

Using volatile with a variable will reexamine the condition after the " if " has executed. As the value can be changed by external hardware.
here in the example i am doing the same in if condition i am changing the volatile variable. now compiler should again check the else which will fall to be true. but that's not so as practically i see. So it is that an ISR should change then else will be checked. I doubt.
Hope u get what am i trying to tell.
 

I think you misunderstand what 'volatile' does. It is a 'hint' to the compiler that the code it produces should not assume the variable remains constant. Typically, the compiler will then take a copy of it as the function is entered and use the copy instead of the original. It doesn't make the compiler "double check" the variable each time it gets used.

As FvM pointed out, the 'else if' is only executed if the first 'if' is not true, the decision is taken as the first 'if' is executed and does not change afterwards.

Brian.
 

Using volatile with a variable will reexamine the condition after the " if " has executed. As the value can be changed by external hardware.
Did you read this in a book, or is it your personal conclusion? Unfortunately it's wrong.

If some literature uses the term "reexamine", it wants to say that the value is calculated anew each time it's evaluated according to C rules. But the attribute does not change the way an if statement works.

The largest impact of volatile is preventing possible unwanted optimizations of a compiler.

E.g. see this code snippet
Code:
flag = 0;
while (flag == 0); // wait for an external routine, typically an interrupt to set flag=1

Without declaring flag as volatile, a compiler with respective optimization level turned on might either ignore the while statement, or even replace it by an infinite while (1), causing code execution to hang.
 

Thanks Fvm and betwist, Sorry i got confused.... Now my concepts are crystal clear!!

FVM you have explained it very well thanks..!!
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top