473,326 Members | 2,128 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,326 software developers and data experts.

scanf not getting entire long value

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?
Nov 14 '05 #1
6 7135
Works fine ...

Sib wrote:
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?


Nov 14 '05 #2
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

<ci********@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
Works fine ...

Sib wrote:
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?

Nov 14 '05 #3
On Sun, 15 May 2005 20:19:01 -0400, "P.J. Plauger"
<pj*@dinkumware.com> wrote in comp.lang.c:
"Sib" <si******@gmail.com> wrote in message
news:nr****************@fe06.lga...
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

<ci********@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
Works fine ...

Sib wrote:
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?


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

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #4
Sib wrote:
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
Nov 14 '05 #5
Sib

"Martin Ambuhl" <ma*****@earthlink.net> wrote in message
news:dk*****************@newsread2.news.atl.earthl ink.net...
Sib wrote:
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[i] = 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
Nov 14 '05 #6
On Mon, 16 May 2005 22:30:21 -0400, "Sib" <si******@gmail.com> wrote:
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[i] = 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...
--
#include <standard.disclaimer>
_
Kevin D Quitt USA 91387-4454 96.37% of all statistics are made up
Nov 14 '05 #7

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

Similar topics

12
by: B Thomas | last post by:
Hi, I was reading O'Reilly's "Practical C programming" book and it warns against the use of scanf, suggesting to avoid using it completely . Instead it recomends to use using fgets and sscanf....
7
by: hugo27 | last post by:
obrhy8 June 18, 2004 Most compilers define EOF as -1. I'm just putting my toes in the water with a student's model named Miracle C. The ..h documentation of this compiler does state that when...
51
by: moosdau | last post by:
my code: do { printf("please input the dividend and the divisor.\n"); if(!scanf("%d%d",&dend,&dor)) { temp1=1; fflush(stdin); } else
30
by: James Daughtry | last post by:
char array; scanf("%19s", &array); I know this is wrong because it's a type mismatch, where scanf expects a pointer to char and gets a pointer to an array of 20 char. I know that question 6.12...
14
by: iwinux | last post by:
Hi. Before I use scanf(), I must malloc the memory for it, like this: //Start char * buffer; buffer = malloc(20); scanf("%s", &buffer); //End
8
by: Neil | last post by:
Hello Just to let you know this not homework, I'm learning the language of C on my own time.. I recently tried to create a escape for user saying printf ("Do you want to continue? (y or n)");...
8
by: Army1987 | last post by:
Is this a good way to discard unread data after scanf()? while (getchar() != '\n') ; According to the FAQ scanf always leaves the trailing newline on the input stream, so there's no risk of...
30
by: Tarique | last post by:
I have tried to write my custom scanf function on the lines of minprintf provided in K&R2.In the function myscanf() i access memory directly using the address passed to the function .Can it be...
13
by: FerrisUML | last post by:
Hello everyone! I new to C and am having the following problem. In the below program, the last scanf is being ignored and the program exits. Can anyone see anything that im doing wrong? Thanks...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.