long int

M

Magix

Hi,


Let say I have following function:
long number(void)
{
long sum = 421;
return (sum*sum);
}

so, I printf the statement ("Number is %d", number( )); the result
is -19367. Why ? I suppose long can hold up to 32 bits. I expect the result
to be 177241.

Thanks.
 
A

Allin Cottrell

Magix said:
Let say I have following function:
long number(void)
{
long sum = 421;
return (sum*sum);
}

so, I printf the statement ("Number is %d", number( )); the result
is -19367. Why ? I suppose long can hold up to 32 bits. I expect the result
to be 177241.

The format string is composed of zero or more directives:
ordinary characters (not %), which are copied unchanged to
the output stream; and conversion specifications, each of
which results in fetching zero or more subsequent argu-
ments. Each conversion specification is introduced by the
character %. The arguments must correspond properly
(after type promotion) with the conversion specifier.
After the %, the following appear in sequence...

o The optional character l (ell) specifying that a
following d, i, o, u, x, or X conversion applies to
a pointer to a long int or unsigned long int argu-
ment, or that a following n conversion corresponds
to a pointer to a long int argument.

Allin Cottrell
 
A

Artie Gold

Magix said:
Hi,


Let say I have following function:
long number(void)
{
long sum = 421;
return (sum*sum);
}

so, I printf the statement ("Number is %d", number( )); the result
is -19367. Why ? I suppose long can hold up to 32 bits. I expect the result
to be 177241.

It is. You just didn't output it correctly.

If you have a long, you need to use the `l' format specifier, not the
`d'; hence you want:

printf("Number is %l", number());

HTH,
--ag
 
M

Martin Ambuhl

Magix said:
Hi,


Let say I have following function:
long number(void)
{
long sum = 421;
return (sum*sum);
}

so, I printf the statement ("Number is %d", number( )); the result
is -19367. Why ? I suppose long can hold up to 32 bits. I expect the result
to be 177241.


#include <stdio.h>

long number(void)
{
long sum = 421;
return (sum * sum);
}

int main(void)
{
/* mha: note the "%ld" rather than "%d" and the '\n' needed for
portable behavior. */
printf("Number is %ld\n", number());
return 0;
}

[Output]
Number is 177241
 
P

Peter Nilsson

Artie Gold said:
It is. You just didn't output it correctly.

If you have a long, you need to use the `l' format specifier, not the
`d';

'l' is a length modifier, not a conversion specifier.
hence you want:

printf("Number is %l", number());

No, Magix wants...

printf("Number is %ld\n", number());
 
A

Artie Gold

Magix said:
If I use %1, nothing is display.

Don't top post.Erm, that's `%l' (percent-sign ell) not a `%1' (percent-sign one).
Damn fonts. ;-(

HTH,
--ag
 
M

Magix

If I use %1, nothing is display.


Artie Gold said:
It is. You just didn't output it correctly.

If you have a long, you need to use the `l' format specifier, not the
`d'; hence you want:

printf("Number is %l", number());

HTH,
--ag
 
M

Magix

I still get the output -19367, after using %1d.

Martin Ambuhl said:
Magix said:
Hi,


Let say I have following function:
long number(void)
{
long sum = 421;
return (sum*sum);
}

so, I printf the statement ("Number is %d", number( )); the result
is -19367. Why ? I suppose long can hold up to 32 bits. I expect the result
to be 177241.


#include <stdio.h>

long number(void)
{
long sum = 421;
return (sum * sum);
}

int main(void)
{
/* mha: note the "%ld" rather than "%d" and the '\n' needed for
portable behavior. */
printf("Number is %ld\n", number());
return 0;
}

[Output]
Number is 177241
 
M

Magix

Sorry, my mistake, I should use "l" (ell) instead one "1" (one)

After I use "l", it outputs:
Number is (non-long

Magix said:
I still get the output -19367, after using %1d.

Martin Ambuhl said:
Magix said:
Hi,


Let say I have following function:
long number(void)
{
long sum = 421;
return (sum*sum);
}

so, I printf the statement ("Number is %d", number( )); the result
is -19367. Why ? I suppose long can hold up to 32 bits. I expect the result
to be 177241.


#include <stdio.h>

long number(void)
{
long sum = 421;
return (sum * sum);
}

int main(void)
{
/* mha: note the "%ld" rather than "%d" and the '\n' needed for
portable behavior. */
printf("Number is %ld\n", number());
return 0;
}

[Output]
Number is 177241
 
K

Keith Thompson

Magix said:
Sorry, my mistake, I should use "l" (ell) instead one "1" (one)

After I use "l", it outputs:
Number is (non-long

Please don't top-post. Any new text should follow, not precede, any
quoted text (the stuff with the "> " prefixes), and there's no need to
quote the entire article to which you're responding. Backwards
written that's stuff read to difficult very it's.

Does it really print "Number is (non-long", or is that a typo? It
should print "Number is 177241". If that's not what you're getting,
please re-post your program (cut-and-paste it, don't try to re-type
it).
 
M

Martin Ambuhl

Artie said:
Erm, that's `%l' (percent-sign ell) not a `%1' (percent-sign one).
Damn fonts. ;-(

It's not just the font. "%l" is wrong. "%ld" is correct. The 'l'
modifies the 'd'.
 
M

Martin Ambuhl

Magix said:
I still get the output -19367, after using %1d.


Don't top-post. That is "%ld" not "%1d". "l" stands for "long," "1"
stands for the number after zero. Don't you have a C book or at least
some sort of on-line help files?



Magix said:
Hi,


Let say I have following function:
long number(void)
{
long sum = 421;
return (sum*sum);
}

so, I printf the statement ("Number is %d", number( )); the result
is -19367. Why ? I suppose long can hold up to 32 bits. I expect the
result
to be 177241.


#include <stdio.h>

long number(void)
{
long sum = 421;
return (sum * sum);
}

int main(void)
{
/* mha: note the "%ld" rather than "%d" and the '\n' needed for
portable behavior. */
printf("Number is %ld\n", number());
return 0;
}

[Output]
Number is 177241
 
R

Ravi Uday

Magix said:
Hi,


Let say I have following function:
long number(void)
{
long sum = 421;
return (sum*sum);
}

so, I printf the statement ("Number is %d", number( )); the result
is -19367. Why ? I suppose long can hold up to 32 bits. I expect the result
to be 177241.

Thanks.

You are invoking a undefined behavior. Auto variables have
scope/life only until the function in which they are defined returns.
After which their value
is junk.
So in your case .. anything can happen !!
print the value (sum*sum) inside the number function. You get what you
expect.

- Ravi
 
J

Jens.Toerring

You are invoking a undefined behavior. Auto variables have
scope/life only until the function in which they are defined returns.

Ahem, he isn't returning a _pointer_ to the local variable but its
_value_ (or its value multiplied by itself, to be precise), so it's
absolutely fine and no undefined behavior is involved there.

Regards, Jens
 
E

Emmanuel Delahaye

Ravi Uday a formulé la demande :
You are invoking a undefined behavior.

Really ? I'm curious...
Auto variables have
scope/life only until the function in which they are defined returns.

So what ?
After which their value
is junk.

Yes, but you do know that a copy of the value is returned. Don't you ?
So in your case .. anything can happen !!
print the value (sum*sum) inside the number function. You get what you
expect.

No. The problem occurs when you return the address of a local. The OP's
code is correct.
 
D

Dan Pop

In said:
You are invoking a undefined behavior. Auto variables have
scope/life only until the function in which they are defined returns.
After which their value is junk.

So what? The return statement is executed while the auto objects are
still alive, not *after*.
So in your case .. anything can happen !!

Bullshit! Or, more precisely, anything can happen because of the bad
printf call, which is the *real* cause of the OP's problem.
print the value (sum*sum) inside the number function. You get what you
expect.

Nope, if he uses the same printf call, he will get the same garbage.

You're a marvelous illustration of the saying "a little knowledge is a
dangerous thing". Don't use *any* rule you don't *really* understand.
The rule in question is about not returning the *address* of automatic
objects, because the objects no longer exist after the function returns.
However, it is always safe to return their values, because the value is
passed back to the caller using a special protocol that doesn't require
the existence of the object when the caller is getting back its value.

But, in this example, even this is irrelevant, as the function doesn't
return sum, but sum * sum. You don't expect this expression to be
evaluated by the caller, do you? So, the function didn't return the
value of *any* automatic object, it returned the value of an arbitrary
expression, value that need not be stored in any memory location.

Mechanically memorised rules are doing you no good. If you cannot
understand something, either ignore it or ask for better explanations.

Dan
 
R

Ravi Uday

Mechanically memorised rules are doing you no good. If you cannot
understand something, either ignore it or ask for better explanations.

Dan

Sorry for my wrong post..!! I had a bad day..so didnt quit time/write
that correctly..
Magix-> Ignore my reply. Well others are correct and I am wrong..

- Ravi
 

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,145
Messages
2,570,825
Members
47,371
Latest member
Brkaa

Latest Threads

Top