a guest Dec 16th, 2018 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.   Third lesson in C
  4.   We've already covered things like include <stdio.h> and printf for writing
  5.   stuff out to the screen now we'll look at going the other way, reading
  6.   input in from a user
  8.   Reading data in.
  9. */
  11. #include <stdio.h>
  13. int main() {
  14.     /*
  15.       Before we can read anything in from the user we have to declare a couple
  16.       of variables so we'll have somewhere to put what we read in.
  18.       You almost always do this at the begining of a function or at the beginning
  19.       of the program (or in .h files just to complicate it but that's a lesson for
  20.       later)
  22.       The first variable will be "line" and will be big enough to hold 50 char (a
  23.       really small string)
  24.     */
  25.     char line[50];
  26.     /*
  27.       To break that down
  29.       char is the type (remember when I explained the "int" in the "int main() {"
  30.       line is the variable name (like "main" is the function name)
  31.       [50] says to make a variable big enough to hold 50 of whatever the type is
  33.       This line variable will be used for reading in from the console, now we need
  34.       a variable to store an integer in.
  35.     */
  36.     int value;
  38.     printf("Enter a value: ");
  40.     /*
  41.       fgets, like printf, is provided as part of the stdio library. This function
  42.       reads characters from a input stream and stores them as a C string into str
  43.       until either 1 less than the specified number of characters have been read
  44.       in this case 50 so 49 characters will be read) or a newline or the
  45.       end-of-file is reached, whichever happens first.
  47.       Newlines come when users push enter, so that's what we'll aim for, a note for
  48.       later, the new line will actually be appended to the string, it's not
  49.       something to worry about today.
  50.     */
  51.     fgets(line, 50, stdin);
  52.     /*
  53.       Broken down:
  55.       fgets(
  56.         line  - store the data in the variable we declared before,
  57.         50    - can store at most this number of characters in the variable
  58.         stdin - read fromt he special input handle "stdin" (standard in)
  59.       )
  61.       Standard in for the sake of this lesson is the console you're working in
  62.       but it can be many other things.
  64.       So now we have data from the user sitting in line, what shall we do with it?
  66.       Lets grab an integer from it with sscanf.
  67.       sscanf is basically the exact oposite of sprintf which is similar to printf
  69.       where printf prints formatted text out
  70.       sprintf stores formatted text in a string
  71.       sscanf reads formatted text back to variables
  72.     */
  73.     sscanf(line, "%d", &value);
  74.     /*
  75.       Broken down this one is a little more complicated than the last one
  77.       sscanf(
  78.         line   - scan the data stored in the line variable we filled with fgets
  79.         '%d'   - look for a number (like the %d in printf)
  80.         &value - store the resulting number at the address of the value variable
  81.       )
  83.       The complicated bit is the &value. Value is a value stored at an address,
  84.       the & prefix causes c to pass the address of value rather than what is stored
  85.       there (think of it as pointing to the shelf the milk is on rather than giving
  86.       up a bottle of milk)
  88.       So, now you have an integer stored in value that should be something you
  89.       inputted.
  91.       Print it back out!
  92.     */
  93.     printf("Your number was %d!\n", value);
  95.     return 0;
  96. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand