scanf not getting entire long value

S

Sib

I am trying to read two long values in using scanf() as follows:

long lower, upper;
scanf("%ld %ld", &lower, &upper);

When I enter: 999900000 1000000000

I get: lower = 999900000 but upper = 1

I am obviously missing something here, any idea what?
 
S

Sib

Not working on mine. I know this isn't the place to speak of compilers, but
I'm using MS-VC6 SP6 (on my notebook). I've got a linux box I will try with
gcc, with hopefully better results. Thanks for checking.

Sib
 
J

Jack Klein

Both numbers are too large to represent in a 32-bit signed
long.

Perhaps you miscounted the '0' characters.

========
#include <stdio.h>
#include <limits.h>

int main(void)
{
char *input = "999900000 1000000000";
long lower, upper;

sscanf(input, "%ld %ld", &lower, &upper);
printf("LONG_MAX = %10ld\n"
" lower = %10ld\n"
" upper = %10ld\n\n",
LONG_MAX, lower, upper);
return 0;
}
========

Output:

LONG_MAX = 2147483647
lower = 999900000
upper = 1000000000
 
M

Martin Ambuhl

Sib said:
I am trying to read two long values in using scanf() as follows:

long lower, upper;
scanf("%ld %ld", &lower, &upper);

When I enter: 999900000 1000000000

I get: lower = 999900000 but upper = 1

I am obviously missing something here, any idea what?

A compilable example of your problem.
Try the following. If it succeeds on your implementation, then the
problem lies somewhere other than where you think.

#include <stdio.h>

int main(void)
{
long lower, upper;
char input[] = "999900000 1000000000";
printf("testing with input of \"%s\"\n", input);
sscanf(input, "%ld %ld", &lower, &upper);
printf("values read: %ld %ld\n", lower, upper);
return 0;
}

[output]
testing with input of "999900000 1000000000"
values read: 999900000 1000000000
 
S

Sib

Martin Ambuhl said:
Sib said:
I am trying to read two long values in using scanf() as follows:

long lower, upper;
scanf("%ld %ld", &lower, &upper);

When I enter: 999900000 1000000000

I get: lower = 999900000 but upper = 1

I am obviously missing something here, any idea what?

A compilable example of your problem.
Try the following. If it succeeds on your implementation, then the problem
lies somewhere other than where you think.

#include <stdio.h>

int main(void)
{
long lower, upper;
char input[] = "999900000 1000000000";
printf("testing with input of \"%s\"\n", input);
sscanf(input, "%ld %ld", &lower, &upper);
printf("values read: %ld %ld\n", lower, upper);
return 0;
}

[output]
testing with input of "999900000 1000000000"
values read: 999900000 1000000000

As you suspected, I had a bug in my program, silly beginner mistake...
Needed array to hold at most 100000 int values, I was doing the following to
initialize all values to 1:

int b[100000];

for(int i = 0; i < upper-lower+1; i++)
b = 1;

b[] has 100000 spots, 0 thru 99999, not 0 thru 100000 (duh!). Writing
b[100000] = 1 was actually writing upper = 1. Changed test case to:

i < upper-lower

and things work like a charm.

Thanks for looking, and sorry to take your time with this silly mistake.

Sib
 
K

Kevin D. Quitt

As you suspected, I had a bug in my program, silly beginner mistake...

int b[100000];

for(int i = 0; i < upper-lower+1; i++)
b = 1;


Which is why it's important to post the *entire* smallest code fragment that exhibits the
behaviour, and not just the lines you think are the problem. Next time...
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top