Download presentation
Presentation is loading. Please wait.
1
CS/COE 0449 (term 2174) Jarrett Billingsley
C – IO, Functions, Scope CS/COE 0449 (term 2174) Jarrett Billingsley
2
Class announcements Add/drop ends tomorrow!
Labs will be Fri-Mon-Tue from now on so that the labs are in sync with the lectures. I'll be uploading code examples from now on! Project 1 will be assigned soon? 1/17/2017 CS/COE term 2174
3
Question time 1/17/2017 CS/COE term 2174
4
One thing I forgot... 1/17/2017 CS/COE term 2174
5
Array initializers int list[] = {1, 2, 3, 4, 5}; // like list[5]
If you don't wanna have to put the length of an array that has an initializer... Don't! int list[] = {1, 2, 3, 4, 5}; // like list[5] This works for multidimensional arrays too BUT only on the first dimension (the number of rows). int matrix[][3] = { // like matrix[4][3] {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}, }; 1/17/2017 CS/COE term 2174
6
Getting input 1/17/2017 CS/COE term 2174
7
f'getsaboutit char buffer[100]; fgets(buffer, sizeof(buffer), stdin);
Let's read a line of text on the console. Strings aren't A Thing. You can't "get" a string. What you do instead... char buffer[100]; fgets(buffer, sizeof(buffer), stdin); Woah, what? sizeof(buffer)? And what's stdin? Standard Input. Usually, the keyboard. You might know it as "System.in" from Java. 1/17/2017 CS/COE term 2174
8
Using sizeof() on arrays
If you have access to an array variable (one declared with [brackets]), you can use sizeof() to get the size in multiples of the size of a char (just like any other type). So for char arrays, it's basically exactly the same as the length of the array. But remember... When I say "array" I only mean things declared with [brackets] with a known dimension. char buffer[100]; is ok to use with sizeof. char* dest; is not. In this case sizeof(dest) gets the size of a pointer. 1/17/2017 CS/COE term 2174
9
Your buffer better be big enough...
Let's try using an 8-character buffer and type in more than that. wha... 1/17/2017 CS/COE term 2174
10
Buffered IO Both input and output in C (and many other languages!) are buffered by default. fgets will 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. How can you tell? Well, let's try with a shorter name... Uh oh. 1/17/2017 CS/COE term 2174
11
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? fname[strlen(fname) - 1] = 0; This is what the book says to do, but... Question! 1/17/2017 CS/COE term 2174
12
Functions 1/17/2017 CS/COE term 2174
13
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 the function BELOW our main function. whaa 1/17/2017 CS/COE term 2174
14
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 where you can freely access functions anywhere at any time. 1/17/2017 CS/COE term 2174
15
Function prototypes To get around this, C has something called function prototypes. Basically, the first line of a function, but instead of a body, you put a semicolon. int readLine(char* buffer, int bufSize, FILE* file); This tells the compiler the parameter and return types, but doesn't say how the function is implemented. This is kind of silly, since you now have to repeat this information twice, but it does have kind of a neat side effect: You can use prototypes to hide the implementations of functions! We'll get to that later. 1/17/2017 CS/COE term 2174
16
Variable scope 1/17/2017 CS/COE term 2174
17
It's mostly the same as Java
A global variable is one declared outside any function, and it exists for the entire runtime of the program. Java's static variables are virtually identical. Just like functions, you can have global variable prototypes, and then full declarations later. However... You can also make static variables inside functions. These work just like globals (or Java's statics) but will only be visible within that function. They're... kind of weird. 1/17/2017 CS/COE term 2174
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.