CS/COE 0449 (term 2174) Jarrett Billingsley

Slides:



Advertisements
Similar presentations
Lectures 10 & 11.
Advertisements

Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
Week 6 - Wednesday.  What did we talk about last time?  Exam 1!  And before that…  Review!  And before that…  Arrays and strings.
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
1 CSC103: Introduction to Computer and Programming Lecture No 24.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 1 due Friday, 7pm. RAD due next Friday. Presentations week 6. Today: –More details on functions,
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Lecturer: Nguyen Thi Hien Software Engineering Department Home page: hienngong.wordpress.com Chapter 2: Language C++
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Programs – Preprocessing, Compilation and Linking
CS/COE 0449 (term 2174) Jarrett Billingsley
Programs – Calling Conventions
Week 2 - Wednesday CS 121.
CS/COE 0449 (term 2174) Jarrett Billingsley
Computer Organization and Design Pointers, Arrays and Strings in C
User-Written Functions
CS/COE 0447 (term 2181) Jarrett Billingsley
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Spring 2017
Strings CSCI 112: Programming in C.
I/O Streams File I/O 2-D array review
EGR 2261 Unit 10 Two-dimensional Arrays
CS/COE 0449 (term 2174) Jarrett Billingsley
CSE 374 Programming Concepts & Tools
C Interview Questions Prepared By:.
C Programming Tutorial – Part I
Foundations of Programming: Arrays
CS/COE 0447 (term 2181) Jarrett Billingsley
Programs – Dynamic Linking and Loading
Arrays in C.
Instructor: Ioannis A. Vetsikas
CISC/CMPE320 - Prof. McLeod
CSC 253 Lecture 8.
C – Structs, Unions, Enums, Typedefs
Strings, Line-by-line I/O, Functions, Call-by-Reference, Call-by-Value
CS/COE 0449 Jarrett Billingsley
Functions BIS1523 – Lecture 17.
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
7 Arrays.
Number and String Operations
CS 240 – Lecture 5 Scope of Variables, The Stack, Automatic Variables, Global Variables, Constant Type.
CS/COE 0449 Jarrett Billingsley
CS/COE 0449 Jarrett Billingsley
Coding Concepts (Sub- Programs)
Topic 1: Problem Solving
Defining methods and more arrays
Local Variables, Global Variables and Variable Scope
Classes and Objects.
Writing Functions.
CISC/CMPE320 - Prof. McLeod
Workshop for Programming And Systems Management Teachers
Line at a time I/O with fgets() and fputs()
CISC/CMPE320 - Prof. McLeod
Building Java Programs
7 Arrays.
Arrays Arrays A few types Structures of related data items
Submitted By : Veenu Saini Lecturer (IT)
Data Structures & Algorithms
Unit 3: Variables in Java
Miscellaneous Topics I
„Lambda expressions, Optional”
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
CS/COE 0449 Jarrett Billingsley
CS/COE 0449 Jarrett Billingsley
Presentation transcript:

CS/COE 0449 (term 2174) Jarrett Billingsley C – IO, Functions, Scope CS/COE 0449 (term 2174) Jarrett Billingsley

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 0449 term 2174

Question time 1/17/2017 CS/COE 0449 term 2174

One thing I forgot... 1/17/2017 CS/COE 0449 term 2174

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 0449 term 2174

Getting input 1/17/2017 CS/COE 0449 term 2174

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 0449 term 2174

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 0449 term 2174

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 0449 term 2174

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 0449 term 2174

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 0449 term 2174

Functions 1/17/2017 CS/COE 0449 term 2174

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 0449 term 2174

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 0449 term 2174

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 0449 term 2174

Variable scope 1/17/2017 CS/COE 0449 term 2174

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 0449 term 2174