Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Third lesson in C
- We've already covered things like include <stdio.h> and printf for writing
- stuff out to the screen now we'll look at going the other way, reading
- input in from a user
- Reading data in.
- */
- #include <stdio.h>
- int main() {
- /*
- Before we can read anything in from the user we have to declare a couple
- of variables so we'll have somewhere to put what we read in.
- You almost always do this at the begining of a function or at the beginning
- of the program (or in .h files just to complicate it but that's a lesson for
- later)
- The first variable will be "line" and will be big enough to hold 50 char (a
- really small string)
- */
- char line[50];
- /*
- To break that down
- char is the type (remember when I explained the "int" in the "int main() {"
- line is the variable name (like "main" is the function name)
- [50] says to make a variable big enough to hold 50 of whatever the type is
- This line variable will be used for reading in from the console, now we need
- a variable to store an integer in.
- */
- int value;
- printf("Enter a value: ");
- /*
- fgets, like printf, is provided as part of the stdio library. This function
- reads characters from a input stream and stores them as a C string into str
- until either 1 less than the specified number of characters have been read
- in this case 50 so 49 characters will be read) or a newline or the
- end-of-file is reached, whichever happens first.
- Newlines come when users push enter, so that's what we'll aim for, a note for
- later, the new line will actually be appended to the string, it's not
- something to worry about today.
- */
- fgets(line, 50, stdin);
- /*
- Broken down:
- fgets(
- line - store the data in the variable we declared before,
- 50 - can store at most this number of characters in the variable
- stdin - read fromt he special input handle "stdin" (standard in)
- )
- Standard in for the sake of this lesson is the console you're working in
- but it can be many other things.
- So now we have data from the user sitting in line, what shall we do with it?
- Lets grab an integer from it with sscanf.
- sscanf is basically the exact oposite of sprintf which is similar to printf
- where printf prints formatted text out
- sprintf stores formatted text in a string
- sscanf reads formatted text back to variables
- */
- sscanf(line, "%d", &value);
- /*
- Broken down this one is a little more complicated than the last one
- sscanf(
- line - scan the data stored in the line variable we filled with fgets
- '%d' - look for a number (like the %d in printf)
- &value - store the resulting number at the address of the value variable
- )
- The complicated bit is the &value. Value is a value stored at an address,
- the & prefix causes c to pass the address of value rather than what is stored
- there (think of it as pointing to the shelf the milk is on rather than giving
- up a bottle of milk)
- So, now you have an integer stored in value that should be something you
- inputted.
- Print it back out!
- */
- printf("Your number was %d!\n", value);
- return 0;
- }
Add Comment
Please, Sign In to add comment