473,394 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

long int

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.
Nov 14 '05 #1
17 1453
Magix wrote:
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

--
Artie Gold -- Austin, Texas

"What they accuse you of -- is what they have planned."
Nov 14 '05 #2
Magix wrote:
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

Nov 14 '05 #3
Magix wrote:
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
Nov 14 '05 #4
If I use %1, nothing is display.
"Artie Gold" <ar*******@austin.rr.com> wrote in message
news:2m************@uni-berlin.de...
Magix wrote:
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

--
Artie Gold -- Austin, Texas

"What they accuse you of -- is what they have planned."

Nov 14 '05 #5
Magix wrote:
If I use %1, nothing is display.
Don't top post.

"Artie Gold" <ar*******@austin.rr.com> wrote in message
news:2m************@uni-berlin.de...
Magix wrote:
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());

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

HTH,
--ag

--
Artie Gold -- Austin, Texas

"What they accuse you of -- is what they have planned."
Nov 14 '05 #6
"Artie Gold" <ar*******@austin.rr.com> wrote in message
news:2m************@uni-berlin.de...
Magix wrote:
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';


'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());

--
Peter
Nov 14 '05 #7
I still get the output -19367, after using %1d.

"Martin Ambuhl" <ma*****@earthlink.net> wrote in message
news:2m************@uni-berlin.de...
Magix wrote:
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

Nov 14 '05 #8
Sorry, my mistake, I should use "l" (ell) instead one "1" (one)

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

"Magix" <ma***@asia.com> wrote in message
news:40**********@news.tm.net.my...
I still get the output -19367, after using %1d.

"Martin Ambuhl" <ma*****@earthlink.net> wrote in message
news:2m************@uni-berlin.de...
Magix wrote:
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


Nov 14 '05 #9
"Magix" <ma***@asia.com> writes:
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).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #10
Artie Gold wrote:
Magix wrote:
If I use %1, nothing is display.
If you have a long, you need to use the `l' format specifier, not the
`d'; hence you want:

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

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'.
Nov 14 '05 #11
Magix wrote:
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?


"Martin Ambuhl" <ma*****@earthlink.net> wrote in message
news:2m************@uni-berlin.de...
Magix wrote:
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


Nov 14 '05 #12
"Magix" <ma***@asia.com> wrote in message news:<40**********@news.tm.net.my>...
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
Nov 14 '05 #13
Ravi Uday <ra******@gmail.com> wrote:
"Magix" <ma***@asia.com> wrote in message news:<40**********@news.tm.net.my>...
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.


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
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #14
Ravi Uday a formulé la demande :
long number(void)
{
long sum = 421;
return (sum*sum);
} 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.

--
Emmanuel

Nov 14 '05 #15
Martin Ambuhl wrote:
Artie Gold wrote:
Magix wrote:
If I use %1, nothing is display.
If you have a long, you need to use the `l' format specifier, not the
`d'; hence you want:

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

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


Of course. [Cue sound of crawling under desk.]
--ag
--
Artie Gold -- Austin, Texas

"What they accuse you of -- is what they have planned."
Nov 14 '05 #16
In <56**************************@posting.google.com > ra******@gmail.com (Ravi Uday) writes:
"Magix" <ma***@asia.com> wrote in message news:<40**********@news.tm.net.my>...
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 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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #17
<snip>
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
Nov 14 '05 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Tim Clacy | last post by:
How is a 64 bit type defined in strict C++? It seems C has support for 'long long' since C99, but not so for C++? Looking through one compiler vendor's standard library headers has clouded the...
7
by: William Payne | last post by:
Hello, I have a variable of type unsigned long. It has a number of bits set (with set I mean they equal one). I need to determine those bits and their position and create new numbers from them. For...
5
by: Mark Shelor | last post by:
Problem: find a portable way to determine whether a compiler supports the "long long" type of C99. I thought I had this one solved with the following code: #include <limits.h> #ifdef...
29
by: Richard A. Huebner | last post by:
Is the unsigned long long primitive data type supported in ANSI standard C? I've tried using it a couple of times in standard C, but to no avail. I'm using both MS VIsual C++ 6, as well as the...
9
by: luke | last post by:
Hi everybody, please, can someone explain me this behaviour. I have the following piece of code: long long ll; unsigned int i = 2; ll = -1 * i; printf("%lld\n", ll);
21
by: Charles Sullivan | last post by:
I maintain/enhance some inherited FOSS software in C which has compiler options for quite a few different Unix-like operating systems, many of which I've never even heard of. It would be...
12
by: Ahmad Jalil Qarshi | last post by:
Hi, I have an integer value which is very long like 9987967441778573855. Now I want to convert it into equivalent Hex value. The result must be 8A9C63784361021F I have used...
2
by: PengYu.UT | last post by:
Hi, In python, triple quote (""") can be used to quote a paragraph (multiple lines). I'm wondering if there is any equivalent in C++. For the following code, I could write the long string in a...
10
by: ratcharit | last post by:
Currently using cosine function in math.h Currently I get: 1 = cos(1e^-7) Is there another way for cos to return value of high accuracy say: 0.999999 = cos(1e^-7)
15
by: Oliver Graeser | last post by:
I need a >49 bit integer type. tried sizeof(long long), says 8. 8 byte = 64 bit right? but when I try to assign a value with more than 32 bit, it fails. To illustrate: for (i=0; i<64; i++){...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.