C Syntax Arrays and Loops Math Strings Structures Pointers File I/O. Final Review CS Prof. Jonathan Ventura. Prof. Jonathan Ventura Final Review

Similar documents
Files and Streams Opening and Closing a File Reading/Writing Text Reading/Writing Raw Data Random Access Files. C File Processing CS 2060

Chapter 4: Basic C Operators

C Pointers. CS 2060 Week 6. Prof. Jonathan Ventura

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

C Functions. CS 2060 Week 4. Prof. Jonathan Ventura

C Structures Self-referencing Card Shuffling Unions. C Structures CS Prof. Jonathan Ventura. Prof. Jonathan Ventura C Structures

Intermediate Programming, Spring 2017*

Standard C Library Functions

today cs3157-fall2002-sklar-lect05 1

Contents. A Review of C language. Visual C Visual C++ 6.0

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

C programming basics T3-1 -

CS 61c: Great Ideas in Computer Architecture

CSCI 171 Chapter Outlines

Operators and Expression. Dr Muhamad Zaini Yunos JKBR, FKMP

6-1 (Function). (Function) !*+!"#!, Function Description Example. natural logarithm of x (base e) rounds x to smallest integer not less than x

Arrays, Pointers and Memory Management

MARKS: Q1 /20 /15 /15 /15 / 5 /30 TOTAL: /100

ANSI C Programming Simple Programs

Intermediate Programming, Spring 2017*

ENG120. Misc. Topics

C mini reference. 5 Binary numbers 12

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

A function is a named group of statements developed to solve a sub-problem and returns a value to other functions when it is called.

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

Lectures 5-6: Introduction to C

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

COP 3223 Final Review

Arrays and Pointers (part 1)

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

CS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers

Kurt Schmidt. October 30, 2018

Technical Questions. Q 1) What are the key features in C programming language?

Computer Science & Engineering 150A Problem Solving Using Computers

Pointers and Structure. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

COP 3223 Final Review

Procedures, Parameters, Values and Variables. Steven R. Bagley

Structured Programming. Dr. Mohamed Khedr Lecture 4

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 3. Existing Information. Notes. Notes. Notes. Lecture 03 - Functions

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

Lectures 5-6: Introduction to C

Lecture 3. More About C

Ch 11. C File Processing (review)

Pointers and File Handling

Chapter 3. Computer Science & Engineering 155E Computer Science I: Systems Engineering Focus. Existing Information.

Arrays Arrays and pointers Loops and performance Array comparison Strings. John Edgar 2

Operators. Java operators are classified into three categories:

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

C++ Programming: From Problem Analysis to Program Design, Third Edition

Advanced C Programming and Introduction to Data Structures

Review Topics. Final Exam Review Slides

Introduction to Computer Programming Lecture 18 Binary Files

CSci 4061 Introduction to Operating Systems. Input/Output: High-level

Continued from previous lecture

Lecture6 File Processing

Arrays and Pointers (part 1)

Computers Programming Course 6. Iulian Năstac

C File Processing: One-Page Summary

CS 261 Fall Mike Lam, Professor. Structs and I/O

211: Computer Architecture Summer 2016

mith College Computer Science CSC270 Spring 2016 Circuits and Systems Lecture Notes, Week 11 Dominique Thiébaut

Reg. No. : Question Paper Code : 27157

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6

Lecture 8. Dr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson)

OUTLINE. Review Functions Pointers Function calls Array Pointers Pointer Arrays Data Structures and dynamic Memory Function Pointers quicksort example

Chapter 11 File Processing

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Fundamental of Programming (C)

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

Compiling and Running a C Program in Unix

Chapter 12. Files (reference: Deitel s chap 11) chap8

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Topics so far. Review. scanf/fscanf. How data is read 1/20/2011. All code handin sare at /afs/andrew/course/15/123/handin

Lecture07: Strings, Variable Scope, Memory Model 4/8/2013

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

Pointers, Dynamic Data, and Reference Types

Jagannath Institute of Management Sciences Lajpat Nagar. BCA II Sem. C Programming

File Processing. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Some Details of C and C Environment

Programming. Pointers, Multi-dimensional Arrays and Memory Management

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

PROGRAMMAZIONE I A.A. 2017/2018

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

CSI 402 Lecture 2 Working with Files (Text and Binary)

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

Fundamentals of Programming & Procedural Programming

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

Operators And Expressions

Function I/O. Function Input and Output. Input through actual parameters. Output through return value. Input/Output through side effects

C Program Structures

PRINCIPLES OF OPERATING SYSTEMS

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

Model Viva Questions for Programming in C lab

Arrays, Strings, & Pointers

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

Function I/O. Last Updated 10/30/18

Operations. Making Things Happen

High Performance Programming Programming in C part 1

Transcription:

CS 2060

Variables Variables are statically typed. Variables must be defined before they are used. You only specify the type name when you define the variable. int a, b, c; float d, e, f; char letter; // Variable assignment: a = 5; // Variable expressions: b = a + 10;

Variables Variables are not initialized to any particular value. Variables should be initialized before use. int a, b, c; // bad: a is used uninitialized: b = a + 10;

Functions Most C code is organized into functions. A function consists of a return type, name argument list and body. float square( float a ) { return a * a; }

Functions You call a function using its name and arguments. float a = 2; float asq; asq = square(a);

Functions A function signature must be defined before it can be called. Or, the function signature and function body can be given. // Function signatures: int foo( float a ); void bar( int b, char c ); // Function definitions: int foo( float a ) { return floor(a*5); }

Functions The return type is void if the function does not return anything. // Returns an integer: int foo( float a ); // No return value: void bar( int b, char c );

Functions Your program starts and ends in the main() function. int foo( float a ) { return floor(a*5); } int main() { // Start of program float a = 10; int b = foo( a ); } // End of program return 0;

Variable scope Local variables... are defined inside a function. only exist for the duration of a function call. can only be accessed from inside the function. int foo( float a ) { return floor(a*5); } int main() { // a and b are local variables float a = 10; int b = foo( a ); } return 0;

Variable scope Global variables... are defined outside any function. exist for the duration of the program. can be accessed from anywhere in the code. // a is a global variable float a = 10; int main() { // b is a local variable int b = floor( a*5 ); } return 0;

Expressions Arithmetic expressions are formed using chains of operators. The result of the expression depends on the operator precedence. 1 Parentheses () 2 Multiplication, division, modulo */% 3 Addition, subtraction +- int b = 3, c = 4, d = 5; a = b + c * d; a = ( b + c ) * d; a = b + ( c * d );

Order of Arithmetic Operations Operator(s) Operation(s) Order of evaluation (precedence) ( ) Parentheses Evaluated first, inner to outer. * Multiplication Evaluated second, left to right. / Division % Modulo + Addition Evaluated third, left to right. - Subtraction = Assignment Evaluated last. Table: Precedence of arithmetic operators in C.

Increment and decrement operators Increment and decrement operators: a++ or ++a will add one to a. a-- or --a will subtract one from a. The difference is in what the expression evaluates to. Postfix: a++ will increment a after returning the (old) value of a. Prefix: ++a will increment a before returning the (new) value of a.

Assignment operators Assignment operators combine arithmetic and assignment: a += 5; a -= 5; a *= 5; a /= 5; The above statements are equivalent to: a = a + 5; a = a - 5; a = a * 5; a = a / 5;

Comparison operators Comparison operators are used for Boolean (true/false) tests: == tests equality!= tests inequality >= tests greater-than-or-equal-to <= tests less-than-or-equal-to

Comparison operators char getgrade( int score ) { if ( score >= 90 ) return A ; else if ( score >= 80 ) return B ; else if ( score >= 70 ) return C ; else if ( score >= 60 ) return D ; else return F ; }

Logical operators Logical operators are used to form Boolean expressions. && (logical AND) (logical OR)! (logical NOT) Their functions are as follows: (a && b) is true only if both a and b are true. (a b) is true if either a or b is true. (!a) is true if a is false.

Comparison operators char getpassfail( int score, int attendance ) { if ( score >= 60 && attendance >= 50 ) return P ; else return F ; }

Array syntax Arrays are continuous blocks of data of the same type. By default, arrays are uninitialized (data could be anything). int numbers[100]; float values[10]; char string[20];

Array syntax A curly brackets initializer sets the elements of the array. int numbers[5] = { 1, 2, 3, 4, 5 };

Array syntax Arrays are indexed using square brackets. The first element has index 0. numbers[0] = 10;

for loops sequence of execution The syntax of a for loop is as follows: for ( /*initialization*/ ; /*condition*/ ; /*increment*/ ) { /* statements */ } 1 Execute initialization. Loop: 1 Test condition stop if false 2 Execute statements 3 Execute increment

Iterating through arrays for loops are ideal for iterating over arrays. int numbers[10]; for ( int i = 0; i < 10; i++ ) numbers[i] = i;

Iterating through arrays Accessing elements 0,2,4,... : int numbers[10] = { 0 }; for ( int i = 0; i < 10; i += 2 ) numbers[i] = i;

while and do...while loops Other kinds of loops: while ( /*condition*/ ) { /* statements */ } do { /* statements */ } while ( /*condition*/ )

Passing arrays to functions Use empty square brackets to indicate an array in function arguments. C doesn t keep track of the size of the array when passed to a function. You need a separate variable for the size of the array. float sumarray( float array[], int size ) { float sum = 0; for ( int i = 0; i < size; i++ ) sum += array[i]; return sum; }

Passing arrays to functions Pass an array to a function using its name only. int main() { float my_array[3] = { -1.f, 0.f, 1.f }; float my_sum; } my_sum = sumarray(my_array,3);

Standard C math library functions in math.h Function sqrt(x) exp(x) log(x) log10(x) abs(x) fabs(x) ceil(x) floor(x) pow(x,y) sin(x),cos(x),tan(x) Description x e x ln(x) log 10 (x) x (int) x (double) x x x y sin(x), cos(x), tan(x) Table: Commonly used math library functions

Random number generation The rand() function (defined in stdlib.h) generates random integers between 0 and RAND MAX. // Get a random integer int i = rand(); // Convert to floating point in range [0,1] float f = (float) i / (float) RAND_MAX;

Random number generation Use modulo and addition to generate random numbers in a specified range. // Get a random integer in range [1 100] int i = rand()%100 + 1;

Characters in C The char type is used to represent characters. char is an 8-bit integer (range 0 to 255). A character is represented by its ASCII code: A = 65, B = 66,... a = 97, b = 98,...

Characters in C Use single quotes around a character to get its ASCII representation. char letter_a = a ; char letter_a = A ; Use %c to read or write a character using scanf or printf.

switch statements The switch statement selects among many options. The value of the control variable determines which option is selected. switch ( getchar() ) { case r : num_red++; break; case b : num_blue++; break; } default: num_other++;

switch statements The control variable can only be a basic integer type like char or int. The cases only test equality. switch ( menu_selection ) { case 1: menu_option_1(); break; case 2: menu_option_2(); break; } default: puts("no such option");

Storing strings in character arrays Strings are character arrays: char string1[] = "first"; In C, strings end with a special string-termination character known as a null character. C strings are called null-terminated strings. The above is equivalent to: char string1[] = { f, i, r, s, t, \0 };

Copying a string Use strcpy to copy a string: char *strcpy( char *to, const char *from ); Remember that the first argument is the destination and the second argument is the source this is typical in the C Standard Library.

Comparing strings strcmp tests string equality: int strcmp( char *s1, const char *s2 ); It returns 0 if the strings are equal.

String length strlen finds the length of a string. size_t strlen( const char *str ); The length does not include the null terminator.

structs A struct contains variables of possibly different types: struct Employee { char first_name[20]; char last_name[20]; unsigned int age; char gender; double hourly_salary; }; A struct is defined outside of any function. Don t forget the semi-colon at the end.

C Structures: struct The type name includes the word struct. Member variables are accessed using the. operator. int main() { struct Employee employee1; strcpy( employee1.first_name, "Jane" ); strcpy( employee1.last_name, "Doe" ); employee1.age = 22; employee1.gender = f ; employee1.hourly_salary = 20; }

Pointers A pointer is a variable whose value is a memory address. The variable does not contain meaningful data itself, but instead points to where data is located in memory. Given the pointer (a memory address), we dereference the pointer to read/write the value stored at that address.

Defining pointers Every data type has an associated pointer type, like int *: int count = 10; int *countptr = &count; The reference operator & gets a pointer to a variable. Variable countptr contains the memory address of count.

Dereferencing pointers The dereference operator * gets the variable pointed to by a pointer: int count = 10; int *countptr = &count; *countptr = 20; count will equal 20 now.

Passing arguments by reference with pointers With pointers we can implement pass-by-reference: void square( float *valueptr ) { *valueptr = (*valueptr) * (*valueptr); } The function modifies the value pointed to by valueptr.

Pointer arithmetic operators Incrementing an int * pointer will move it to the next int in memory. int data[10]; int *dataptr = data; // points to data[0] *dataptr = 0; dataptr++; // points to data[1] *dataptr = 10; dataptr++; // points to data[2] The first two elements of data will be 0 and 10.

Pointer arithmetic example To iterate over an array using a pointer: int array[5] = { 0, 1, 2, 3, 4 }; for ( int *ptr = array; ptr!= array+5; ptr++ ) { printf("%d\n", (*ptr) ); }

Pointers to structs The -> operator accesses struct members through a pointer. void printroom( struct Room *room ) { printf( "%s\n", room->name ); printf(" %s\n", room->description ); }

Dynamic memory allocation malloc allocates memory and returns a pointer to it. free frees the memory when we are done with it. int *data = malloc( 10 * sizeof(int) ); free( data ); data is just like an array of 10 ints.

Dynamic memory allocation example Use malloc to allocate space for a struct: struct Point { int x, int y }; struct Point * makepoint( int x, int y ) { // allocate struct struct Point *pt = malloc( sizeof(struct Point) ); // initialize struct pt->x = x; pt->y = y; } // return pointer return pt;

Function Pointers A function pointer points to a function. void runtask( void (*callbackfn)( int status ) ) { for ( int i = 0; i < 100; i++ ) { //.. do some computation... (*callbackfn)( i ); } } void printstatus( int status ) { printf( "status: %d \% done\n", status ); } //... runtask( printstatus ); The runtask function takes a function pointer as an argument. During a long process, it calls the callback to print out status information.

Files and Streams Files are used for long-term storage of data (on a hard drive rather than in memory). The data in a file is accessible through file access functions such as fprintf and fscanf or fread and fwrite.

FILE structure Open a file with fopen and close it with fclose: FILE *f = fopen( "hello.txt", "r" ); fclose( f ); fopen returns a FILE * pointer. The first argument is the file name. The second argument is the file open mode.

File open modes The most common file open modes: Mode Description Notes "r" Read-only Opens existing file "w" Write-only Creates new file, deleting old file if it exists "a" Append Begins with file pointer at end of file "r+" Read and write Opens existing file "w+" Read and write Creates new file, deleting old file if it exists

Reading from a file Use fscanf for formatted input from a file: // Open hello.txt as read-only FILE *f = fopen("hello.txt","r"); // Read five integers from the file int a[5]; for ( int i = 0; i < 5; i++ ) { fscanf( f, "%d", &a[i] ); } // Close the file fclose(f);

Formatted print fprintf writes formatted output to a file: // Open output.txt FILE *f = fopen("output.txt","w"); // Write out some text fprintf( f, "Hello, world!\n" ); fprintf( f, "2 + 5 = %d\n", 2 + 5 ); // Close the file fclose(f);

Reading integers stored as binary fread reads bytes from a file: // Open the file for reading FILE *f = fopen("integers.dat","r"); // Read 10 integers from the file int values[10]; fread( values, sizeof(int), 10, f ); // Close the file fclose(f); fwrite is similar.

Random Aaccess files A random access file contains a sequence of structs. Starting byte #: 0 100 200 300... Record #: 0 1 2 3... Table: A random access file with 100-byte records

Random access file /** ClientData * Stores data for a bank client */ typedef struct { unsigned int account_num; // account number char last_name[15]; // account last name char first_name[10]; // account first name double balance; // account balance } ClientData;

Updating a random access file int main() { // Open file FILE *f = fopen( "accounts.dat", "r+" ); // Move file pointer to tenth record fseek( f, 9*sizeof(ClientData), SEEK_SET ); // Read requested record ClientData client_data; fread( &client_data, sizeof(clientdata), 1, f );

Reading a random access file // Zero out balance client_data.balance = 0; // Move file pointer back to tenth record fseek( f, 9*sizeof(ClientData), SEEK_SET ); // Update record in file fwrite( &client_data, sizeof(clientdata), 1, f ); } // Close file fclose( f );