Personal SE. Functions, Arrays, Strings & Command Line Arguments

Similar documents
Personal SE. Functions & Arrays

8. Characters, Strings and Files

ONE DIMENSIONAL ARRAYS

Arrays and Pointers. CSE 2031 Fall November 11, 2013

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

Array Initialization

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

Arrays and Pointers in C. Alan L. Cox

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

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

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows

Intermediate Programming, Spring 2017*

Chapter 8: Character & String. In this chapter, you ll learn about;

SWEN-250 Personal SE. Introduction to C

upper and lower case English letters: A-Z and a-z digits: 0-9 common punctuation symbols special non-printing characters: e.g newline and space.

G52CPP C++ Programming Lecture 3. Dr Jason Atkin

Chapter 5. Section 5.4 The Common String Library Functions. CS 50 Hathairat Rattanasook

Kurt Schmidt. October 30, 2018

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

CSE 230 Intermediate Programming in C and C++ Arrays, Pointers and Strings

Arrays and Pointers (part 1)

Lecture 9 - C Functions

Arrays and Pointers (part 1)

Arrays. An array is a collection of several elements of the same type. An array variable is declared as array name[size]

Introduction to string

ARRAYS(II Unit Part II)

Computers Programming Course 10. Iulian Năstac

Chapter 8 Arrays and Strings. Objectives. Objectives (cont d.) Introduction. Arrays 12/23/2016. In this chapter, you will:

PRINCIPLES OF OPERATING SYSTEMS

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 14


CSC 2400: Computer Systems. Arrays and Strings in C

CS 61c: Great Ideas in Computer Architecture

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

Grade Distribution. Exam 1 Exam 2. Exams 1 & 2. # of Students. Total: 17. Total: 17. Total: 17

Coursework 2: Basic Programming

C Strings. Abdelghani Bellaachia, CSCI 1121 Page: 1

ECE 15B COMPUTER ORGANIZATION

PROGRAMMAZIONE I A.A. 2017/2018

CSC 2400: Computer Systems. Arrays and Strings in C

C strings. (Reek, Ch. 9) 1 CS 3090: Safety Critical Programming in C

Arrays and Pointers (part 2) Be extra careful with pointers!

Bil 104 Intiroduction To Scientific And Engineering Computing. Lecture 7

Computers Programming Course 11. Iulian Năstac

Language comparison. C has pointers. Java has references. C++ has pointers and references

Arrays and Pointers (part 2) Be extra careful with pointers!

Chapter 9 Strings. With this array declaration: char s[10];

(T) x. Casts. A cast converts the value held in variable x to type T

C programming basics T3-1 -

C mini reference. 5 Binary numbers 12

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

Strings. Arrays of characters. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY

gcc hello.c a.out Hello, world gcc -o hello hello.c hello Hello, world

Tutorial 1: Introduction to C Computer Architecture and Systems Programming ( )

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

8. Characters and Arrays

United States Naval Academy Electrical and Computer Engineering Department EC310-6 Week Midterm Spring AY2017

C: Arrays, and strings. Department of Computer Science College of Engineering Boise State University. September 11, /16

Lesson 7. Reading and Writing a.k.a. Input and Output

Pointers. Pointer References

For instance, we can declare an array of five ints like this: int numbers[5];

Array. Prepared By - Rifat Shahriyar

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

Characters in C consist of any printable or nonprintable character in the computer s character set including lowercase letters, uppercase letters,

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

Create a Program in C (Last Class)

BSM540 Basics of C Language

Character Strings Lesson Outline

C for C++ Programmers

Organization of a file

Programming in C. What is C?... What is C?

SYSC 2006 C Winter String Processing in C. D.L. Bailey, Systems and Computer Engineering, Carleton University

Programming in C UVic SEng 265

Chapter 8 Character Arrays and Strings

Programming in C. What is C?... What is C?

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

Final CSE 131B Spring 2004

Fundamentals of Programming

CS1100 Introduction to Programming

Chapter 8 C Characters and Strings

Course organization. Course introduction ( Week 1)

CSC 270 Survey of Programming Languages. C-String Values

Iosif Ignat, Marius Joldoș Laboratory Guide 9. Character strings CHARACTER STRINGS

APT Session 4: C. Software Development Team Laurence Tratt. 1 / 14

Multiple Choice Questions ( 1 mark)

CSCE150A. Introduction. Basics. String Library. Substrings. Line Scanning. Sorting. Command Line Arguments. Misc CSCE150A. Introduction.

mith College Computer Science CSC352 Week #7 Spring 2017 Introduction to C Dominique Thiébaut

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 9. Strings. Notes. Notes. Notes. Lecture 07 - Strings

CSE 333 Midterm Exam 5/10/13

Luar Topics. C A Low level Programming Language Make A dependency based build system YUYV Representation of Color ZMP Zero Moment Point control

Fundamental of Programming (C)

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Darshan Institute of Engineering & Technology for Diploma Studies Unit 6

Pointers, Arrays, and Strings. CS449 Spring 2016

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

Procedural Programming & Fundamentals of Programming

Transcription:

Personal SE Functions, Arrays, Strings & Command Line Arguments

Functions in C Syntax like Java methods but w/o public, abstract, etc. As in Java, all arguments (well, most arguments) are passed by value. Example: void try_swap( int x, int y ) { int t = x ; x = y ; y = t ; Doesn't work: x and y are copies of the arguments in the caller. Changing the copy has no effect in the caller.

Functions in C Functions must be declared before use: Declare means specify name, return value, and argument types. Indeed, in C everything must be declared before use!

Functions in C Functions must be declared before use: Declare means specify name, return value, and argument types. Indeed, in C everything must be declared before use! extern int min(int x, int y) ; // Declaration of min static int max(int x, int y) ; // Declaration of max int max_div_min(int x, int y) { return max(x, y) / min(x, y) ; int min(int x, int y) { return (x <= y)? x : y ; // Definition of min static int max(int x, int y) { // Definition of max return (x >= y)? x : y ;

Functions in C extern: defined elsewhere (possibly this file). Functions must be declared before use: Declare means specify name, return value, and argument types. Indeed, in C everything must be declared before use! extern int min(int x, int y) ; // Declaration of min static int max(int x, int y) ; // Declaration of max int max_div_min(int x, int y) { return max(x, y) / min(x, y) ; int min(int x, int y) { return (x <= y)? x : y ; // Definition of min static int max(int x, int y) { // Definition of max return (x >= y)? x : y ;

Functions in C static: defined and known only in this C source file. Functions must be declared before use: Declare means specify name, return value, and argument types. Indeed, in C everything must be declared before use! extern int min(int x, int y) ; // Declaration of min static int max(int x, int y) ; // Declaration of max int max_div_min(int x, int y) { return max(x, y) / min(x, y) ; int min(int x, int y) { return (x <= y)? x : y ; // Definition of min static int max(int x, int y) { // Definition of max return (x >= y)? x : y ;

Arrays in C Generic form: type name[size] ; Examples: #define MAX_SAMPLES (100) int samples[max_samples] ;

Arrays in C Generic form: type name[size] ; Examples: Array of 100 integers. Indices run 0.. 99 NO SUBSCRIPT CHECKS! NOTE THE USE OF SYMBOLIC CONSTANT! #define MAX_SAMPLES (100) int samples[max_samples] ;

Arrays in C Generic form: type name[size] ; Examples: Simple summation of array values. #define MAX_SAMPLES (100) int samples[max_samples] ; int sum = 0 ; int i ; for ( i = 0 ; i < MAXSAMPLES ; ++i ) { sum += samples[ i ] ;

Arrays in C #define DIMENSION (50) ; double m1[dimension][dimension] ;

Arrays in C A matrix or a 2 dimensional array Access by m1[ i ][ j ] #define DIMENSION (50) ; double m1[dimension][dimension] ;

Arrays in C Matrix multiplication to show use of double indices. #define DIMENSION (50) ; double m1[dimension][dimension] ; double m2[dimension][dimension] ; double product[dimension][dimension] ; int i, j, k ; for ( i = 0 ; i < DIMENSION ; ++i ) { for ( j = 0 ; j < DIMENSION ; ++j ) { product[ i ][ j ] = 0.0 ; for ( k = 0 ; k < DIMENSION ; ++k ) { product[i][j] += m1[i][k] * m2[k][j] ;

Arrays in C Arrays are passed to functions by reference.

Arrays in C Arrays are passed to functions by reference. Changes to the array contents in the function will be visible to the caller, e.g., array copy.

Arrays in C Arrays are passed to functions by reference. Changes to the array contents in the function will be visible to the caller, e.g., array copy. void acopy( int to[], int from[], size ) { int i ; for( i = 0 ; i < size ; i++ ) { to[ i ] = from[ i ] ;

Arrays in C Arrays are passed to functions by reference. Changes to the array contents in the function will be visible to the caller, e.g., array copy. void acopy( int to[], int from[], size ) { int i ; for( i = 0 ; i < size ; i++ ) { to[ i ] = from[ i ] ; Need not, but may, give the array size.

Arrays in C - Review Array size fixed at definition time. Good practice (that is, OUR practice) is to use symbolic constants to define array sizes. Array indices are integers. Legal indices run from 0 to arraysize - 1 C will not prevent you from indexing outside the bounds of the array (no subscript checks). Arrays are passed by reference.

Strings in C A string is just an array of chars: char welcome[] = "Hello" ; // C permits this This is an array of 8-bit bytes holding ASCII characters.

Strings in C A string is just an array of chars: char welcome[] = "Hello" ; // C permits this This is an array of 8-bit bytes holding ASCII characters The array in memory looks like this: 'H' 'e' 'l' 'l' 'o' '\0' Whoa! What s that last character????

Strings in C A string is just an array of chars: char welcome[] = "Hello" ; // C permits this This is an array of 8-bit bytes holding ASCII characters The array in memory looks like this: 'H' 'e' 'l' 'l' 'o' '\0' Whoa! What s that last character???? In C, proper strings must be terminated with a NUL (0) character. We always need an extra byte to hold the terminator!

Strings in C Assume we are reading and processing lines of text, where at most the first 80 characters of a line are useful How would we declare an array to hold the line as a string?

Strings in C Assume we are reading and processing lines of text, where at most the first 80 characters of a line are useful How would we declare an array to hold the line as a string? #define MAXLINE (80) char line[ MAXLINE + 1 ] ; // 1 extra character for the NUL

Strings in C Assume we are reading and processing lines of text, where at most the first 80 characters of a line are useful How would we declare an array to hold the line as a string? #define MAXLINE (80) char line[ MAXLINE + 1 ] ; How would we read in such a line? // 1 extra character for the NUL

Strings in C Assume we are reading and processing lines of text, where at most the first 80 characters of a line are useful How would we declare an array to hold the line as a string? #define MAXLINE (80) char line[ MAXLINE + 1 ] ; How would we read in such a line? void readline( char line[], int maxsize ) { int i = 0 ; int ch ; // 1 extra character for the NUL for ( ch = getchar() ; ch!= '\n' && ch!= EOF ; ch = getchar() ) { if ( i < maxsize ) { line[ i++ ] = ch ; line[ i ] = '\0' ; return ;

Strings in C How can we copy one string to another? Modify acopy to strcpy: void strcpy( char sto[], char sfrom[] ) { int i ; for ( i = 0 ; sto[ i ] = sfrom[ i ] ; ++i ) ;

Strings in C How can we copy one string to another? Modify acopy to strcpy: void strcpy( char sto[], char sfrom[] ) { int i ; for ( i = 0 ; sto[ i ] = sfrom[ i ] ; ++i ) ; Copy the ith character. If this was a NUL, exit the loop.

String Library #include <string.h> int strlen( char str[] ) ; Note: strlen("hello") == 5 void strcpy( char sto[], char sfrom[] ) ; void strncpy( char sto[], char sfrom[], unsigned n ); Note: Copies 'n' characters to 'sto' from 'sfrom', padding with '\0' as necessary. Note: If 'sfrom' is too long to fit in 'sto', then 'sto' will NOT be NUL terminated. int strcmp( char str1[], char str2[] ) ; Note: comparison is in dictionary order. Note: returns -1, 0, 1 if 'str1' is less than, equal to, or greater than 'str2', respectively.

Command Line Arguments The full declaration of main is: int main( int ac, char **argv ) ;

Command Line Arguments The full declaration of main is: int main( int ac, char **argv ) ; ac = argument count (the number of command line arguments). ac >= 1, as the program name is the 0th argument.

Command Line Arguments The full declaration of main is: int main( int ac, char **argv ) ; ac = argument count (the number of command line arguments). Includes the program name as the 0th argument. Example: ac == 5 gcc -o myprog main.c util.c 0 1 2 3 4

Command Line Arguments The full declaration of main is: int main( int ac, char **argv ) ; argv = the argument vector - allows access to the arguments it's a pointer, but don't worry - treat it like a 2D array. argv[ i ] is i th argument as a string (array). argv[ i ][ j ] is the j th character of the i th argument.

Example Echo Arguments #include <stdlib.h> #include <stdio.h> #include <string.h> int main( int ac, char **argv ) { int i ; printf( "Program name = %s\n", argv[0] ) ; for( i = 1 ; i < ac ; ++i ) { printf( "argv[%d] = %s ", i, argv[i] ) ; printf( "and its length is %d\n", strlen( argv[i] ) ) ; return 0 ;