* better
[mascara-docs.git] / lang / C / the.ansi.c.programming.language / c.programming.notes / sx6d.html
blob8a7b7c63fb6742fce61db38c1b0ae746063bd874
1 <!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN">
2 <!-- This collection of hypertext pages is Copyright 1995-7 by Steve Summit. -->
3 <!-- This material may be freely redistributed and used -->
4 <!-- but may not be republished or sold without permission. -->
5 <html>
6 <head>
7 <link rev="owner" href="mailto:scs@eskimo.com">
8 <link rev="made" href="mailto:scs@eskimo.com">
9 <title>6.4 Reading Numbers</title>
10 <link href="sx6c.html" rev=precedes>
11 <link href="sx7.html" rel=precedes>
12 <link href="sx6.html" rev=subdocument>
13 </head>
14 <body>
15 <H2>6.4 Reading Numbers</H2>
17 <p>The <TT>getline</TT> function of the previous section
18 reads one line from
19 the user,
20 as a <em>string</em>.
21 What if we want to read a number?
22 One straightforward way is to read a string as before,
23 and then immediately convert the string to a number.
24 The standard C library contains a number of functions for doing this.
25 The simplest to use are
26 <TT>atoi()</TT>, which converts a string to an integer, and
27 <TT>atof()</TT>, which converts a string to a floating-point number.
28 (Both of these functions are declared in the header <TT>&lt;stdlib.h&gt;</TT>,
29 so you should <TT>#include</TT> that header
30 at the top of any file using these functions.)
31 You could read an integer from the user like this:
32 <pre>
33 #include &lt;stdlib.h&gt;
35 char line[256];
36 int n;
37 printf("Type an integer:\n");
38 getline(line, 256);
39 n = atoi(line);
40 </pre>
41 Now the variable <TT>n</TT> contains the number typed by the user.
42 (This assumes that the user <em>did</em> type a valid number,
43 and that <TT>getline</TT> did not return <TT>EOF</TT>.)
44 </p><p>Reading a floating-point number is similar:
45 <pre>
46 #include &lt;stdlib.h&gt;
48 char line[256];
49 double x;
50 printf("Type a floating-point number:\n");
51 getline(line, 256);
52 x = atof(line);
53 </pre>
54 (<TT>atof</TT> is actually declared as returning type <TT>double</TT>,
55 but you could also use it with a variable of type <TT>float</TT>,
56 because
57 in general,
58 C automatically converts between <TT>float</TT> and
59 <TT>double</TT> as needed.)
60 </p><p>Another way of reading in numbers,
61 which you're likely to see in other books on C,
62 involves the <TT>scanf</TT> function,
63 but it has
64 several problems,
65 so we won't discuss it for now.
66 (Superficially, <TT>scanf</TT> seems simple enough,
67 which is why it's often used,
68 especially in textbooks.
69 The trouble is that to perform input reliably using <TT>scanf</TT>
70 is not nearly as easy as it looks,
71 especially when you're not sure what the user is going to type.)
72 </p><hr>
73 <p>
74 Read sequentially:
75 <a href="sx6c.html" rev=precedes>prev</a>
76 <a href="sx7.html" rel=precedes>next</a>
77 <a href="sx6.html" rev=subdocument>up</a>
78 <a href="top.html">top</a>
79 </p>
80 <p>
81 This page by <a href="http://www.eskimo.com/~scs/">Steve Summit</a>
82 // <a href="copyright.html">Copyright</a> 1995-1997
83 // <a href="mailto:scs@eskimo.com">mail feedback</a>
84 </p>
85 </body>
86 </html>