really confused with variable access

T

TaxicabNumber

main()
{
int num;
while(..)
{
num=atoi(str+x)
if(num!=0)
{
....
....
}
else
{
printf("num = %d",num);
}
}


I am not able to access the value of num in the else block. It prints
zero. I am very new to C and as far as I know, num should be accessed
in the else block also as it is declared in the main block which is
actually is the parent block and I guess at runtime, the parent block
is checked for a variable value if that variable is not decalred in
that block.

regards,
vz.
 
J

Joona I Palaste

(e-mail address removed) scribbled the following:
main()
{
int num;
while(..)
{
num=atoi(str+x)
if(num!=0)
{
...
...
}
else
{
printf("num = %d",num);
}
}

I am not able to access the value of num in the else block. It prints
zero. I am very new to C and as far as I know, num should be accessed
in the else block also as it is declared in the main block which is
actually is the parent block and I guess at runtime, the parent block
is checked for a variable value if that variable is not decalred in
that block.

I think the problem is in either the .. part, the ... part or the other
.... part.
 
J

Joona I Palaste

Joona I Palaste said:
(e-mail address removed) scribbled the following:
I think the problem is in either the .. part, the ... part or the other
... part.

Silly me! I should learn to read code.
Your problem is that your code always displays zero in the else part?
That's really your problem?
Well, your elese part only ever gets executed in the first place if
num is zero. What do you expect it to display?
 
E

Emmanuel Delahaye

main()
{
int num;
while(..)
{
num=atoi(str+x)

How are defined an initialized 'str' and 'x'.
if(num!=0)
{
...
...
}
else
{
printf("num = %d",num);
}
I am not able to access the value of num in the else block.

What do you meant ? Compile error?
It prints
zero.

So what ? It could be an error with atoi(). What is the parameter you
passed to atoi() ? How is it a valid string representing a decimal
value ?

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"C is a sharp tool"
 
A

Andrey Tarasevich

main()
{
int num;
while(..)
{
num=atoi(str+x)
if(num!=0)
{
...
...
}
else
{
printf("num = %d",num);
}
}


I am not able to access the value of num in the else block. It prints
zero.
...

Of course, it prints zero. You wrote the code so that the control is
passed to 'else' branch if and only if 'num' is zero. No surprisingly,
the code in the 'else' branch prints zero, as it should.

Maybe you should explain what you are trying to implement first.
 
M

Mike Wahler

main()
{
int num;
while(..)
{
num=atoi(str+x)
if(num!=0)
{

/* this block is executed if num is not zero */
...
...
}
else
{

/* this block is executed if num is zero */
printf("num = %d",num);
}
}


I am not able to access the value of num in the else block.

Yes, you are.
It prints
zero.

Well, of course, because the value *is* zero. Zero is a value.
I am very new to C

I can tell. :)
and as far as I know, num should be accessed
in the else block

It is.
also as it is declared in the main block which is
actually is the parent block and I guess at runtime, the parent block
is checked for a variable value if that variable is not decalred in
that block.

Right.

Word of advice: It's better to post a complete, compilable program,
rather than the incomplete code with syntax errors as you did.

-Mike
 
N

Neo

main()
{
int num;
while(..)
{
num=atoi(str+x)
if(num!=0)
{
...
...
}
else
{
printf("num = %d",num);
}
}

hmm....... Whtz da meaning of else here? Is it the same what the language
says or is it something ELSE U want.... "else" here means when num is ZERO.
So, always zero... so far, so good!
-Neo
 
T

TaxicabNumber

Exactly. How silly me, I should store the previous non-zero value of
num first. Thanks a lot to all.

regards,
vz.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,157
Messages
2,570,879
Members
47,414
Latest member
djangoframe

Latest Threads

Top