1 / 19

C – Input, Functions, Scope, and Lifetime

Learn about input handling, array initialization, buffer IO, function declarations, function prototypes, scope, and lifetime in C programming.

gstallworth
Télécharger la présentation

C – Input, Functions, Scope, and Lifetime

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C – Input, Functions, Scope, and Lifetime CS/COE 0449 (term 2184) Jarrett Billingsley

  2. Class announcements • add/drop ends tomorrow! • but you can drop for another week after • no I don't know if you can get in if you're on the waitlist • please stop asking, it stresses me out CS449 (2184)

  3. oh yeah some more arrays CS449 (2184)

  4. Array initializers • you can initialize an array (fill it with values) like so intlist[5] = {1, 2, 3, 4, 5}; • if you don't wanna put the number of items, that's fine too intlist[] = {1, 2, 3, 4, 5}; • it'll just figure it out in that case CS449 (2184)

  5. ArrayIndexIsOutOfBoundsExceptionErrorOhNoThisIsBad • what if • what if you access past the end of an array intlist[] = {1, 2, 3, 4, 5}; printf("%d\n", list[100]); • ?!?? • this is allowed because of course it is • but this is undefined behavior • lots of this in C • basically, don't do it, or you never know what might happen CS449 (2184)

  6. Getting input, better CS449 (2184)

  7. f'getsaboutit • remember: char buffer[100]; fgets(buffer, sizeof(buffer), stdin); • in more detail: • this will read at most 99 characters from standard in • it will put those characters into buffer • it will zero-terminate the string (hence why only 99) • now buffer contains the input string • but there are some weird issues CS449 (2184)

  8. sizeof and strlen are different things • C strings end with a zero terminator (a character with value 0). • strlen() from string.h gives you the length of the string • it only works on zero-terminated strings • not malformed strings • not any other kind of array • sizeof() (built-in) gives you the size, in bytes, of a variable • if you use it on an array variable, it gives what you'd expect • if you use it on a pointer variable, it does not • you CANNOT use either of these as a general-purpose replacement for Java's .length() • so don't, ok CS449 (2184)

  9. Buffered IO • let's try using a 4-character buffer and type in more than that • fgetswill not necessarily read an entire line, if the buffer is too small! • in that case the rest of the line is still in the input buffer, and the next call to fgets will get that buffered data your input array stdio's buffer for stdin still in the buffer! next fgets will get this. fgets(input, 4, stdin) CS449 (2184)

  10. One more thing about fgets • it gives you the newline, too • you can use strlen to see how long the string is, and then see if the last character is a newline character • how can we make a string end wherever we want? • what says "the string ends here" in a C string? buffer[strlen(buffer) - 1] = 0; • this is what the book says to do, but... • what if, somehow, strlen(buffer) == 0? • then what element would it access? CS449 (2184)

  11. Functions CS449 (2184)

  12. Nothing too new...? • to declare a new function, you write the return type, name, parameters, blah blah blah you've done this before • let's make a function that gets a line of text in a more robust way • but... • now let's try moving that function BELOW our main function • whaa CS449 (2184)

  13. wat • C comes from the dark old days when computers had memory in the one-to-two-digits of kilobytes of memory • it's meant to be compilable in a "single pass" fashion • one archaic consequence of this is: • C does not know about functions if you try to access them before you declare them • this is unlike virtually every other modern language CS449 (2184)

  14. Function prototypes • to get around this, C has something called function prototypes • it's the first line of a function, but instead of code, you put a ; intread_line(char* buffer, intsize, FILE* file); • this tells the compiler the parameter and return types, but doesn't say how the function is implemented • you can use prototypes to hide the implementations of functions! • we'll get to that a good bit later CS449 (2184)

  15. Scope and Lifetime CS449 (2184)

  16. Scope • scopeis "where a name can be seen" • C has three levels of scope: globals, static globals, and locals • globalsare outside functions and can be seen everywhere • like Java's static variables • (we'll come back to static globals later) • locals are inside functions and can only be seen in that function • what's in a local variable by default? do you remember • locals can also shadow other locals, e.g. int x = 52; if(blah()) { int x = 104; // same name! printf("%d\n", x); // prints 104 } CS449 (2184)

  17. Lifetime • every variable takes up space in memory • that memory has to be allocated and deallocated so that we know who it belongs to • lifetimeis a little more subtle than scope... • it's the time between when a piece of memory is allocated and when it's deallocated (no longer needed) • global variables have a lifetime that lasts the entire program • local variables have a lifetime that only lasts for the enclosing function CS449 (2184)

  18. You're watching the Lifetime Channel voidfunc() { int f = 10; } int glob = 0x910B; void main() { int m = 10; func(); } lifetime of glob lifetime of m lifetime of f time CS449 (2184)

  19. If only it were that simple • in this Java code, what happens to the memory for the Scanner object after blah returns? Scanner blah() { returnnew Scanner("blerf.txt"); } • we have an object whose lifetime starts in blah but ends somewhere else • this is terrifying and we'll talk more about it when we talk about the heap and pointers and garbage collection and stuff CS449 (2184)

More Related