473,811 Members | 3,314 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7149
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********@gma il.com> wrote in message
news:11******** **************@ g43g2000cwa.goo glegroups.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.l ga...
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********@gma il.com> wrote in message
news:11******** **************@ g43g2000cwa.goo glegroups.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_MA X = %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.l earn.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*****@earthl ink.net> wrote in message
news:dk******** *********@newsr ead2.news.atl.e arthlink.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.discl aimer>
_
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
9876
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. However no explanation is offered other than that scanf handels end of lines very badly. I have exeperienced such problems when doing some numerical programming but never understood it. Things like some consequitive scanfs would not read in values...
7
7666
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 scanf cannot fill any fields it returns EOF. I have run some tests on scanf and, so far, I've not found an EOF return. For example: If the programer formats scanf for an int or
51
3942
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
3226
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 of the C FAQ says that it's wrong for that very reason. What I don't know is where the standard tells me conclusively that it's wrong. What I also don't know is somewhere that this type mismatch will break in practice.
14
22013
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
2380
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)"); The scanf statement follows the prompt(printf above) which is the last statement in the while loop, before the "}"
8
5174
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 discarding any more than needed. (In the best case, when scanf() correctly works and the user hasn't input spurious data beside those required in the control string, the loop will only iterate once.)
30
3466
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 dangerous ? I am getting the correct output though.Any help is appreciated. /*Include Files*/ /*Assisting Functions*/ int flushln(FILE *f){ /*Code*/}
13
4382
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 in advance. #include <stdio.h> /* HOMEWORK: Assignment 1 Name: Dennis McQuilken
0
9731
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9605
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10405
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7671
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5556
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5697
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4342
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3871
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3020
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.