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

Learning C - Scanf or Getch, or Getchar not working correctly after first loop.

Hi Gurus,

I have written the following code with the help of Ivor Horton's
Beginning C :

// Structures, Arrays of Structures.

#include "stdafx.h"
#include "stdio.h"
#define MY_ARRAY 15
#define BASIS 360

struct swap
{
char counterParty[10];
float notional;
float intRate;
int busDays;
//int basis = 360;
float flows;
};

void main()
{

/*Arrays of Structures*/

swap myArraySwap[MY_ARRAY];
int hcount = 0;
int i = 0;
//='\0';

// for (hcount = 0; hcount < 5 ; hcount++ )
do
{
char test = NULL;
printf("\nWould you like to enter Swaps (Y or N)?:");

test = getchar();

if (test != 'Y') //|| test !='y')
break;

printf("\nEnter CounterParty:");
scanf("%s", &myArraySwap[hcount].counterParty );
printf("\nEnter Notional :" );
scanf("%f", &myArraySwap[hcount].notional );
printf("\nEnter Interest Rate:" );
scanf("%f", &myArraySwap[hcount].intRate );
printf("\nEnter Number of days:" );
scanf("%d", &myArraySwap[hcount].busDays );
myArraySwap[hcount].flows = (myArraySwap[hcount].notional *
(myArraySwap[hcount].intRate/100.0f) *
((float)myArraySwap[hcount].busDays/360.0f));

hcount++;
}
while (hcount < 5);

for (i=0 ; i < hcount ; i++)
{
printf("\nCounter Party: %s",myArraySwap[hcount].counterParty);
printf("\nFlows %f", myArraySwap[i].flows);
printf("\nDone");
}
}
Problem:

First time around the var test = '' and I can enter a value i.e. Y

here is the dos output:

Would you like to enter Swaps (Y or N)?:Y

Enter CounterParty:Test

Enter Notional :1000

Enter Interest Rate:5.25

Enter Number of days:35

Would you like to enter Swaps (Y or N)?:

second time around the at the point of test = getchar(); test is set
to '0 ' automatically and I am not able to enter Y or N.

when the app reaches:test = getchar(); it automatically sets the var
from Y 89 to : 0 ''

is this compiler related i have vs.net 2003 ?

i have also tried scanf and get the same :(

thank you.

tesh

Feb 15 '07 #1
26 4479
On 15 Feb, 09:06, "tesh...@googlemail.com" <tesh...@googlemail.com>
wrote:
subject: Learning C - Scanf or Getch, or Getchar not working
correctly after first loop.

note C is a type sensitive language so Scanf etc. should
be written scanf.

Have you read the FAQ, which may be found at:-
http://c-faq.com/stdio/index.html

Try:

12.18a "I'm reading a number with scanf and %d, and then a string with
gets()
but the compiler seems to be skipping the call to gets()"

12.18b "I'm using scanf %c to read a Y/N response, but later input
gets skipped."
12.20 "Why does everyone say not to use scanf? What should I use
instead?"

as these may answer your questions
I have written the following code with the help of Ivor Horton's
Beginning C :
I'd find a better book. Have you tried
"The C Programming Language" by Kernighan & Richie?
// Structures, Arrays of Structures.

#include "stdafx.h"
non-standard header
#include "stdio.h"
standard headers should use this syntax

#include <stdio.h>
#define MY_ARRAY 15
#define BASIS 360

struct swap
{
char counterParty[10];
float notional;
prefer double to float unless you *really* need to save space.
float intRate;
int busDays;
//int basis = 360;
float flows;
};

void main()
int main(void)
{

/*Arrays of Structures*/
not a very useful comment. Ok when you are starting out.
swap myArraySwap[MY_ARRAY];

int hcount = 0;
int i = 0;
//='\0';

// for (hcount = 0; hcount < 5 ; hcount++ )
do
{
char test = NULL;
no, NULL is usually a pointer constant. Use '\0' to represent
a nul charcter.
printf("\nWould you like to enter Swaps (Y or N)?:");
fflush(stdout);
>
test = getchar();

if (test != 'Y') //|| test !='y')
break;

printf("\nEnter CounterParty:");
scanf("%s", &myArraySwap[hcount].counterParty );
printf("\nEnter Notional :" );
scanf("%f", &myArraySwap[hcount].notional );
printf("\nEnter Interest Rate:" );
scanf("%f", &myArraySwap[hcount].intRate );
printf("\nEnter Number of days:" );
scanf("%d", &myArraySwap[hcount].busDays );
myArraySwap[hcount].flows = (myArraySwap[hcount].notional *
(myArraySwap[hcount].intRate/100.0f) *
((float)myArraySwap[hcount].busDays/360.0f));

hcount++;
}
while (hcount < 5);

for (i=0 ; i < hcount ; i++)
{
printf("\nCounter Party: %s",myArraySwap[hcount].counterParty);
printf("\nFlows %f", myArraySwap[i].flows);
printf("\nDone");
}

}

Problem:

First time around the var test = '' and I can enter a value i.e. Y

here is the dos output:

Would you like to enter Swaps (Y or N)?:Y

Enter CounterParty:Test

Enter Notional :1000

Enter Interest Rate:5.25

Enter Number of days:35

Would you like to enter Swaps (Y or N)?:

second time around the at the point of test = getchar(); test is set
to '0 ' automatically and I am not able to enter Y or N.

when the app reaches:test = getchar(); it automatically sets the var
from Y 89 to : 0 ''

is this compiler related i have vs.net 2003 ?

i have also tried scanf and get the same :(

--
Nick Keighley

Feb 15 '07 #2
te*****@googlemail.com said:
Hi Gurus,

I have written the following code with the help of Ivor Horton's
Beginning C :
I suggest you get a better book. From the look of your code, Mr Horton
needs to learn C before he tries writing a book about it.
>
// Structures, Arrays of Structures.

#include "stdafx.h"
No such header is defined by the C language.
#include "stdio.h"
Since you want to pick up the standard header, it's probably better to
use:

#include <stdio.h>
#define MY_ARRAY 15
#define BASIS 360

struct swap
{
char counterParty[10];
float notional;
float intRate;
int busDays;
//int basis = 360;
float flows;
};

void main()
int main(void)
{

/*Arrays of Structures*/

swap myArraySwap[MY_ARRAY];
int hcount = 0;
int i = 0;
//='\0';

// for (hcount = 0; hcount < 5 ; hcount++ )
do
{
char test = NULL;
NULL is intended for use as a pointer value, not a char value. Use 0 for
an initialiser if you don't want test's value to be indeterminate.
Furthermore, you'll be using this for getchar, which - despite its name
- returns int, so make test an int, not a char. (There is a good reason
for this which would be overkill to go into at this stage.)
printf("\nWould you like to enter Swaps (Y or N)?:");

test = getchar();

if (test != 'Y') //|| test !='y')
break;
Well, yes, that's *one* way to get out of a loop, but it isn't the way
I'd have chosen.

Now, when you run your program, you press the 'Y' key, and then you
press the ENTER key, yes? The getchar() call will retrieve the 'Y' from
the standard input stream and assign its value to 'test', but that
still leaves a newline character in the stream (because you pressed
'ENTER'), and that has yet to be read. The next input function call,
then, will retrieve it. But you don't /want/ your scanf call to
retrieve it. You just want to throw it away. You can do so like this:

getchar(); /* read and discard newline character */

This isn't very robust (what if the user typed 'Yes' rather than 'Y'?),
but it'll get you back on track for now.
>
printf("\nEnter CounterParty:");
scanf("%s", &myArraySwap[hcount].counterParty );
Several problems here. Firstly, never ever use %s with scanf. If you
must use scanf to read a string, specify the maximum length of string
that your buffer can support. In this case, the counterParty member of
your struct has ten elements, so it can support a string up to 9
characters in length, so use %9s rather than %s. Secondly, you are
expected to provide a pointer to the first character in your buffer,
not a pointer to the array. So use EITHER

&myArraySwap[hcount].counterParty[0]

with the leading ampersand but specifying the first character in the
array as its operand, OR, more simply and equivalent in meaning:

myArraySwap[hcount].counterParty

(without the leading ampersand).

Also, scanf can fail, so be sure you check the return value.

That'll do to be going on with.

<snip>

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Feb 15 '07 #3
Richard Heathfield <rj*@see.sig.invalidwrites:
te*****@googlemail.com said:
>Hi Gurus,

I have written the following code with the help of Ivor Horton's
Beginning C :

I suggest you get a better book. From the look of your code, Mr Horton
needs to learn C before he tries writing a book about it.
Does your arrogance know no bounds? Did you never see bad code written
by people who read K&R?
Feb 15 '07 #4
Richard <rg****@gmail.comwrote:
Richard Heathfield <rj*@see.sig.invalidwrites:
I suggest you get a better book. From the look of your code, Mr Horton
needs to learn C before he tries writing a book about it.
Does your arrogance know no bounds? Did you never see bad code written
by people who read K&R?
Anyone who would write

void main()

after reading K&R (as OP and, like as not, Mr. Horton did), should
invest in a pair of glasses. Mr. Horton would neither be the first
nor the last would-be textbook author to make that particular error,
and it does not inspire confidence in the other material presented in
Mr. Horton's text.

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Feb 15 '07 #5
On 15 Feb, 13:51, Richard <rgr...@gmail.comwrote:
Richard Heathfield <r...@see.sig.invalidwrites:
tesh...@googlemail.com said:
I have written the following code with the help of Ivor Horton's
Beginning C :
I suggest you get a better book. From the look of your code, Mr Horton
needs to learn C before he tries writing a book about it.

Does your arrogance know no bounds? Did you never see bad code written
by people who read K&R?
assuming the code is a fair reflection of the book, there should
*not* be quite so many "school boy howlers" in a short peice of
code intended for teaching purposes.

Yes, we can all write bad code but code for teaching purposes
should be reviewed by at least two pairs of competent eyes.

(this is not directed at the original poster, who is quite entitled
to make mistakes like this at this stage)
--
Nick Keighley

Feb 15 '07 #6
Christopher Benson-Manica wrote:
Richard <rg****@gmail.comwrote:
Richard Heathfield <rj*@see.sig.invalidwrites:
I suggest you get a better book. From the look of your code, Mr Horton
needs to learn C before he tries writing a book about it.
Does your arrogance know no bounds? Did you never see bad code written
by people who read K&R?

Anyone who would write

void main()

after reading K&R (as OP and, like as not, Mr. Horton did), should
invest in a pair of glasses. Mr. Horton would neither be the first
nor the last would-be textbook author to make that particular error,
and it does not inspire confidence in the other material presented in
Mr. Horton's text.
Here, a so-called "premier" institute for IT, which boasts of
"partnership" with MS, Sun, Oracle etc., uses void main(), K&R style
function declarations, gets(), scanf("%s", ...), char to recieve the
return value of fgetc() and family, and says that all arrays are
automatically terminated with a nul character.

The list is not exhaustive. Can you believe it? With such a quality in
pedagogy from frontline institutes, I'm not surprised at the questions
we get here.

Feb 15 '07 #7
Richard said:
Richard Heathfield <rj*@see.sig.invalidwrites:
>te*****@googlemail.com said:
>>>
I have written the following code with the help of Ivor Horton's
Beginning C :

I suggest you get a better book. From the look of your code, Mr
Horton needs to learn C before he tries writing a book about it.

Does your arrogance know no bounds?
I don't believe so, no. Why do you ask?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Feb 15 '07 #8
santosh <sa*********@gmail.comwrote:
The list is not exhaustive. Can you believe it? With such a quality in
pedagogy from frontline institutes, I'm not surprised at the questions
we get here.
My institution (Georgia Tech) didn't place a lot of weight on strict
conformance, but there were certainly no egregious violations on the
order of void main().

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Feb 15 '07 #9
"te*****@googlemail.com" wrote:
>
I have written the following code with the help of Ivor Horton's
Beginning C :

// Structures, Arrays of Structures.

#include "stdafx.h"
#include "stdio.h"
#define MY_ARRAY 15
#define BASIS 360

struct swap
{
char counterParty[10];
float notional;
float intRate;
int busDays;
//int basis = 360;
float flows;
};

void main()
{

/*Arrays of Structures*/

swap myArraySwap[MY_ARRAY];

int hcount = 0;
int i = 0;
//='\0';

// for (hcount = 0; hcount < 5 ; hcount++ )
do
{
char test = NULL;
printf("\nWould you like to enter Swaps (Y or N)?:");

test = getchar();

if (test != 'Y') //|| test !='y')
break;

printf("\nEnter CounterParty:");
scanf("%s", &myArraySwap[hcount].counterParty );
printf("\nEnter Notional :" );
scanf("%f", &myArraySwap[hcount].notional );
printf("\nEnter Interest Rate:" );
scanf("%f", &myArraySwap[hcount].intRate );
printf("\nEnter Number of days:" );
scanf("%d", &myArraySwap[hcount].busDays );
myArraySwap[hcount].flows = (myArraySwap[hcount].notional *
(myArraySwap[hcount].intRate/100.0f) *
((float)myArraySwap[hcount].busDays/360.0f));

hcount++;
}
while (hcount < 5);

for (i=0 ; i < hcount ; i++)
{
printf("\nCounter Party: %s",myArraySwap[hcount].counterParty);
printf("\nFlows %f", myArraySwap[i].flows);
printf("\nDone");
}

}
Errors detected:

[1] c:\c\junk>cc junk.c
junk.c:1:20: stdafx.h: No such file or directory (ENOENT)
junk.c:12: parse error before '/' token
junk.c:12: warning: no semicolon at end of struct or union
junk.c:14: parse error before '}' token
junk.c:14: warning: ISO C does not allow extra `;' outside of a
functio
junk.c:17: warning: return type of `main' is not `int'
junk.c: In function `main':
junk.c:21: `swap' undeclared (first use in this function)
junk.c:21: (Each undeclared identifier is reported only once
junk.c:21: for each function it appears in.)
junk.c:21: parse error before "myArraySwap"
junk.c:26: parse error before '/' token
junk.c:28: parse error before ')' token
junk.c:34: `test' undeclared (first use in this function)
junk.c:36: parse error before '/' token
junk.c:36: warning: empty body in an if-statement
junk.c:40: `myArraySwap' undeclared (first use in this function)
junk.c:25: warning: unused variable `i'
junk.c:28: warning: statement with no effect
junk.c: At top level:
junk.c:53: parse error before "while"
junk.c:58: parse error before string constant
junk.c:58: warning: type defaults to `int' in declaration of
`printf'
junk.c:58: warning: conflicting types for built-in function
`printf'
junk.c:58: ISO C forbids data definition with no type or storage
class
junk.c:59: parse error before string constant
junk.c:59: warning: type defaults to `int' in declaration of
`printf'
junk.c:59: ISO C forbids data definition with no type or storage
class

After correcting the foolish use of // comments we get:

[1] c:\c\junk>cc junk.c
junk.c:1:20: stdafx.h: No such file or directory (ENOENT)
junk.c:17: warning: return type of `main' is not `int'
junk.c: In function `main':
junk.c:21: `swap' undeclared (first use in this function)
junk.c:21: (Each undeclared identifier is reported only once
junk.c:21: for each function it appears in.)
junk.c:21: parse error before "myArraySwap"
junk.c:40: `myArraySwap' undeclared (first use in this function)

After you correct these errors so that the code is compilable in
ISO standard C, repost and we will try to help you. Read the
documentation for your system to find out how to make it compile
standard C.

I had thought that Horton was fairly sound. Looks like I may need
to revise that opinion.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
Feb 16 '07 #10
Richard Heathfield wrote:
Richard said:
>Richard Heathfield <rj*@see.sig.invalidwrites:
>>te*****@googlemail.com said:

I have written the following code with the help of Ivor Horton's
Beginning C :

I suggest you get a better book. From the look of your code, Mr
Horton needs to learn C before he tries writing a book about it.

Does your arrogance know no bounds?

I don't believe so, no. Why do you ask?
I believe it is bounded slightly below my own level. However in
the limit the levels may be the same. It all depends on epsilon.

Has Richard<nonamedeliberately selected his name with the
intention of making him hard to PLONK?

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
Feb 16 '07 #11
CBFalconer <cb********@yahoo.comwrites:
[...]
Has Richard<nonamedeliberately selected his name with the
intention of making him hard to PLONK?
Can you plonk by e-mail address?

--
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.
Feb 16 '07 #12
"te*****@googlemail.com" <te*****@googlemail.comwrites:
I have written the following code with the help of Ivor Horton's
Beginning C :

// Structures, Arrays of Structures.
[snip]
struct swap
{
char counterParty[10];
float notional;
float intRate;
int busDays;
//int basis = 360;
float flows;
};
[snip]
swap myArraySwap[MY_ARRAY];
[snip]

Here's an error that I don't think anyone else has pointed out, at
least not explicitly. You declare a type called "struct swap", but
you attempt to refer to it as "swap". If your code compiles with this
error, you're probably compiling it as C++, which could mask other
errors as well. First, find out how to tell your compiler to compile
C rather than C++; if you succeed, it will give you an error message
saying that the type "swap" is undeclared. Then change the line
swap myArraySwap[MY_ARRAY];
to
struct swap myArraySwap[MY_ARRAY];

And a style point: The name "MY_ARRAY" implies that it refers to an
array; in fact, it expands to a number. Call it "MY_ARRAY_LENGTH" or
something like that. The compiler doesn't care whether your
identifiers make any sense, but your readers do (that includes you
reading your own code later on).

--
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.
Feb 16 '07 #13
Keith Thompson wrote:
CBFalconer <cb********@yahoo.comwrites:
[...]
>Has Richard<nonamedeliberately selected his name with the
intention of making him hard to PLONK?

Can you plonk by e-mail address?
I get funny effects when trying that, which I have not figured
out. The net effect is to plonk everything in the newsgroup. :-(

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
Feb 16 '07 #14
On 15 Feb 2007 01:50:45 -0800, "Nick Keighley"
<ni******************@hotmail.comwrote:
On 15 Feb, 09:06, "tesh...@googlemail.com" <tesh...@googlemail.com>
wrote:
struct swap
{
char counterParty[10];
float notional;

prefer double to float unless you *really* need to save space.
float intRate;
int busDays;
//int basis = 360;
float flows;
};
But if you do ...
printf("\nEnter Notional :" );
scanf("%f", &myArraySwap[hcount].notional );
printf("\nEnter Interest Rate:" );
scanf("%f", &myArraySwap[hcount].intRate );
.... change those to %lf .
printf("\nEnter Number of days:" );
scanf("%d", &myArraySwap[hcount].busDays );
And in all cases check the return value from *scanf; as well as using
a limit on %s (or %[..]) as already noted elsethread and snipped.
myArraySwap[hcount].flows = (myArraySwap[hcount].notional *
(myArraySwap[hcount].intRate/100.0f) *
((float)myArraySwap[hcount].busDays/360.0f));
And for double arithmetic preferably use double constants (no suffix
f) although small integer values like these are exact in either (any)
floating precision.

Even in float (single) that cast isn't needed; intvalue / floatconst
promotes the int to float before dividing, and intvalue / doubleconst
promotes it to double.

- formerly david.thompson1 || achar(64) || worldnet.att.net
Feb 26 '07 #15
te*****@googlemail.com wrote:

<snip>

I've been studying C for a little while, using online tutorials. I've
read mention in several places against using the scanf function. Is there
an introductory tutorial out there that does NOT introduce this and
instructs the use of another function or array ??

Thanks
Feb 26 '07 #16
pf****@immitationmeat.cox.net wrote:
te*****@googlemail.com wrote:

<snip>

I've been studying C for a little while, using online tutorials. I've
read mention in several places against using the scanf function. Is there
an introductory tutorial out there that does NOT introduce this and
instructs the use of another function or array ??

Thanks
To my knowledge no. The problem is that scanf is easy to introduce to
beginners. It's straightforward to use. The fact that it can cause
undefined behaviour if not used very carefully is something that is,
or should be, taught during the middle of the course. Alternatives are
suggested and the student is expected to use one himself.

In books and tutorials it's very convinient to use scanf for the sake
of conciseness and keeping clutter to a minimum in code samples,
(something that's important for a printed book).

fgets combined with a few other conversion functions like strtol,
strtod etc., is a good alternative. Many people also write their own
line input routines; CBFalconer has a version called ggets on his page.

Feb 26 '07 #17

<pf****@immitationmeat.cox.netwrote in message
news:Hc*************@newsfe21.lga...
te*****@googlemail.com wrote:

<snip>

I've been studying C for a little while, using online tutorials. I've
read mention in several places against using the scanf function. Is there
an introductory tutorial out there that does NOT introduce this and
instructs the use of another function or array ??

Thanks
I tend to use fgets() , followed by strtok() to split the like where I want,
then strtol() etc to convert the data to variables.
Feb 26 '07 #18
pf****@immitationmeat.cox.net said:
te*****@googlemail.com wrote:

<snip>

I've been studying C for a little while, using online tutorials.
I've
read mention in several places against using the scanf function. Is
there an introductory tutorial out there that does NOT introduce this
and instructs the use of another function or array ??
Not yet, as far as I know. That situation may change (if I ever find the
time). There certainly *should* be such a tutorial.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Feb 27 '07 #19

<pf****@immitationmeat.cox.netwrote in message
te*****@googlemail.com wrote:

<snip>

I've been studying C for a little while, using online tutorials. I've
read mention in several places against using the scanf function. Is there
an introductory tutorial out there that does NOT introduce this and
instructs the use of another function or array ??
Input is one of the most basic things that a program has to do.
It is also inherently difficult if the programmer does not control what is
entered.

For instance the input 100000000000000000000000 will overflow an integer and
be approximated in a floating point type. However it is a number, it may
even be a valid number for the problem domain. So you have to be very
careful in what you tell the user.
Feb 27 '07 #20
On Feb 27, 1:48 am, "Malcolm McLean" <regniz...@btinternet.comwrote:
<pfl...@immitationmeat.cox.netwrote in message
tesh...@googlemail.com wrote:
<snip>
I've been studying C for a little while, using online tutorials. I've
read mention in several places against using the scanf function. Is there
an introductory tutorial out there that does NOT introduce this and
instructs the use of another function or array ??

Input is one of the most basic things that a program has to do.
It is also inherently difficult if the programmer does not control what is
entered.

For instance the input 100000000000000000000000 will overflow an integer and
be approximated in a floating point type. However it is a number, it may
even be a valid number for the problem domain. So you have to be very
careful in what you tell the user.
100000000000000000000000 will be represented exactly in any reasonable
floating point type.
There is only one significant digit.

Now, 100000000000000000000001 is another story.

Feb 27 '07 #21
"user923005" <dc*****@connx.comwrites:
100000000000000000000000 will be represented exactly in any reasonable
floating point type.
There is only one significant digit.
There is only one significant digit in base 10. In base 2, there
are 54 significant bits:
2#101010010110100000010110001111110000101001010111 10110100000000000000000000000
That's near the edge of what you can expect to get out of a
64-bit floating-point number.
--
"We put [the best] Assembler programmers in a little glass case in the hallway
near the Exit sign. The sign on the case says, `In case of optimization
problem, break glass.' Meanwhile, the problem solvers are busy doing their
work in languages most appropriate to the job at hand." --Richard Riehle
Feb 27 '07 #22
Ben Pfaff wrote:
"user923005" <dc*****@connx.comwrites:
>100000000000000000000000 will be represented exactly in any reasonable
floating point type.
There is only one significant digit.

There is only one significant digit in base 10. In base 2, there
are 54 significant bits:
2#101010010110100000010110001111110000101001010111 10110100000000000000000000000
That's near the edge of what you can expect to get out of a
64-bit floating-point number.
That would be 53 bits I think for a double. A sign bit, 11 exponent bits
and 53 mantissa bits add to 65. Because bit 52 of the mantissa is always
1, its position is given up to bit 0 of the exponent. Voila, 64 bits.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Feb 27 '07 #23
For anyone interested, I went to my local library and got "The C
Programming Language" by Kernighan and Ritchie. Scanf is not introduced
until chapter 7, so I guess I answered my own question. :D It's a little
old, but with running linux, so far the standard library seems to be the
same.

Peter

<snip>
>
I've been studying C for a little while, using online tutorials. I've
read mention in several places against using the scanf function. Is there
an introductory tutorial out there that does NOT introduce this and
instructs the use of another function or array ??
Mar 3 '07 #24
pf****@immitationmeat.cox.net wrote:
>
<snip>

I've been studying C for a little while, using online tutorials. I've
read mention in several places against using the scanf function. Is there
an introductory tutorial out there that does NOT introduce this and
instructs the use of another function or array ??

For anyone interested, I went to my local library and got "The C
Programming Language" by Kernighan and Ritchie.
Do make sure it's the second edition.

Mar 4 '07 #25
pf****@immitationmeat.cox.net wrote:
>
For anyone interested, I went to my local library and got "The C
Programming Language" by Kernighan and Ritchie. Scanf is not
introduced until chapter 7, so I guess I answered my own question.
:D It's a little old, but with running linux, so far the standard
library seems to be the same.
It is, together with the errata, up-to-date for C90. You will have
nothing to unlearn later.

Please don't top-post. Your answer belongs after (or intermixed
with) the material you quote, after snipping anything irrelevant.
See the following links.

--
Some informative links:
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/ (taming google)
<http://members.fortunecity.com/nnqweb/ (newusers)
Mar 4 '07 #26
santosh said:
pf****@immitationmeat.cox.net wrote:
>>
<snip>
>
I've been studying C for a little while, using online tutorials.
I've
read mention in several places against using the scanf function. Is
there an introductory tutorial out there that does NOT introduce
this and instructs the use of another function or array ??

For anyone interested, I went to my local library and got "The C
Programming Language" by Kernighan and Ritchie.

Do make sure it's the second edition.
If it's the first edition, send it to me. I'll square it with your
library.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 4 '07 #27

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

Similar topics

5
by: Eduardo Olivarez | last post by:
The following code does not work correctly on my machine. Either one of the scanf()'s alone work perfectly. However, when they are combined, the second scanf() call just reads what the first one...
3
by: Vinicius | last post by:
Hello, the following code does not work: " .... void main(void) { char option; printf("Choose an option: ");
6
by: Dawn Minnis | last post by:
Hi (running Win xp and developing using Miracle C. Running applications in windows command prompt) I'm new to the group so be gentle with me. I am currently writing a C program to perform...
185
by: Martin Jørgensen | last post by:
Hi, Consider: ------------ char stringinput ..bla. bla. bla. do {
6
by: obdict | last post by:
Hello, I used scanf() in a while loop, which ensures that user input is valid (must be an integer no greater than 21 or less than 3). If user enters a number out of the range, or enters...
62
by: Argento | last post by:
I was curious at the start about how ungetc() returns the character to the stream, so i did the following coding. Things work as expected except if I change the scanf("%c",&j) to scanf("%d",&j). I...
7
by: vamshi | last post by:
main() { char str; printf("Enter a string: "); scanf("%",str); printf("%",str); } Can some one help me with this code? when i tried to execute this..... it reads the string untill character
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...
2
by: subramanian100in | last post by:
Consider the following program named as x.c #include <stdlib.h> #include <stdio.h> int main(void) { unsigned int u; char str;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.