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? 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?
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?
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
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
"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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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....
|
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...
|
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
|
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...
|
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
|
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)");...
|
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...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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
|
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...
| |