Intermediate Programming, Spring 2017*

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

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

Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017*

Chapter 8 - Characters and Strings

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

Computers Programming Course 10. Iulian Năstac

Chapter 8 C Characters and Strings

Introduction to string

Intermediate Programming, Spring 2017*

C: How to Program. Week /May/28

Intermediate Programming, Spring 2017*

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

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

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

8. Characters, Strings and Files

Intermediate Programming, Spring 2017*

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

Intermediate Programming, Spring Misha Kazhdan

Multiple Choice Questions ( 1 mark)

Chapter 8 Character Arrays and Strings

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

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

BSM540 Basics of C Language

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

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

Computers Programming Course 11. Iulian Năstac

Strings and Library Functions

C-LANGUAGE CURRICULAM

Basic C Programming. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

C Language: Review. INFO High Performance Scientific Computing. 26 septembre 2017

PRINCIPLES OF OPERATING SYSTEMS

C mini reference. 5 Binary numbers 12

C Characters and Strings

String constants. /* Demo: string constant */ #include <stdio.h> int main() {

CCE1111 Programming for Engineers [C Programming Language]

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

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

Characters and Strings

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

Slide Set 3. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

Computer Science & Engineering 150A Problem Solving Using Computers

Computer Programming: Skills & Concepts (CP) Strings

Procedural Programming & Fundamentals of Programming

by Pearson Education, Inc. All Rights Reserved.

today cs3157-fall2002-sklar-lect05 1

EL2310 Scientific Programming

CS 241 Data Organization Pointers and Arrays

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

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

Introduction to C Programming Ray Lauff Temple University Computer Science v2.0

C PROGRAMMING. Characters and Strings File Processing Exercise

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

G52CPP C++ Programming Lecture 3. Dr Jason Atkin

Pointers, Arrays, and Strings. CS449 Spring 2016

Slide Set 3. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Arrays and Pointers in C. Alan L. Cox

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

CSC231 C Tutorial Fall 2018 Introduction to C

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

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

Procedural Programming & Fundamentals of Programming

Object oriented programming C++

CSE2301. Functions. Functions and Compiler Directives

ONE DIMENSIONAL ARRAYS

C programming basics T3-1 -

CS 220: Introduction to Parallel Computing. Input/Output. Lecture 7

Lecture 10 Arrays (2) and Strings. UniMAP SEM II - 11/12 DKT121 1

Using Character Arrays. What is a String? Using Character Arrays. Using Strings Life is simpler with strings. #include <stdio.

Announcements. Strings and Pointers. Strings. Initializing Strings. Character I/O. Lab 4. Quiz. July 18, Special character arrays

Reading Assignment. Strings. K.N. King Chapter 13. K.N. King Sections 23.4, Supplementary reading. Harbison & Steele Chapter 12, 13, 14

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*

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

ARRAYS(II Unit Part II)

High Performance Programming Programming in C part 1

Variables and literals

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

C Programming Multiple. Choice

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

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

Introduction to the C Programming Language

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010

Strings. Daily Puzzle

Course organization. Course introduction ( Week 1)

Problem 2 Add the two 2 s complement signed 8-bit values given below, and express your answer in decimal.

printf( Please enter another number: ); scanf( %d, &num2);

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

Bil 104 Intiroduction To Scientific And Engineering Computing. Lecture 7

CSCI 6610: Intermediate Programming / C Chapter 12 Strings

Introduction to Algorithms and Data Structures. Lecture 6 - Stringing Along - Character and String Manipulation


Preview from Notesale.co.uk Page 2 of 79

CS1100 Introduction to Programming

INTRODUCTION TO C++ C FORMATTED INPUT/OUTPUT. Dept. of Electronic Engineering, NCHU. Original slides are from

Unit 2: The string class

I2204 ImperativeProgramming Semester: 1 Academic Year: 2018/2019 Credits: 5 Dr Antoun Yaacoub

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

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

Transcription:

600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general and you should not follow my lead on this.

Outline sizeof operator Static arrays Strings Printing to and reading from the console

Last time Integer types: [unsigned] char: [un]signed character (typically 1 byte) [unsigned] int: [un]signed integer (typically 4 bytes) Floating-point types: float: single-precision floating point number (typically 4 bytes) double: double-precision floating point number (typically 8 bytes) https://en.wikipedia.org/wiki/c_data_types

sizeof operator To determine the size of a type, you can use sizeof. int x = 75; printf( Size of char: %d\n, sizeof( char ) ); printf( Size of int: %d\n, sizeof( x ) );./a.out Size of char: 1 Size of int: 4

Static arrays Static arrays are declared/accessed using square brackets: int values[2]; values[0] = 0; values[1] = 130; printf( Array values: %d %d\n, values[0], values[1] );./a.out Array values: 0 130

Static arrays Static arrays are declared/accessed using square brackets: C/C++ does not stop you from accessing values outside the array: #include values[4] <stdio.h> = 0; int values[2]; values[0] = 0; values[1] = 130; printf( Array values: %d %d\n, values[0], values[2] );./a.out Array values: 0 0

Static arrays Static arrays are declared/accessed using square brackets: C/C++ does not stop you from accessing values outside the array #include values[4] <stdio.h> = 0; int values[2]; values[0] = 0; values[1] = 130; printf( Array values: %d %d\n, values[0], values[1024] );./a.out./a.out Array values:./a.out 0 976302907 Array values: 0 123461234 Array values: 0 813401299

Static arrays Static arrays are declared/accessed using square brackets: C/C++ does not stop you from accessing values outside the array #include values[4] <stdio.h> = 0; int x = 100; int values[2]; int y = 100; values[0] = 0 ; values[1] = 1 ; values[2] = 2; printf( "values = %d, %d, y = %d\n", values[0], values[1], y );./a.out values = 0, 1, y = 2

Static arrays Static arrays are declared/accessed using square brackets: C/C++ does not stop you from accessing values outside the array #include values[4] <stdio.h> = 0; int x = 100; int values[2]; int y = 100; values[0] = 0 ; values[1] = 1 ; values[1000000] = 2; printf( "values = %d, %d, y = %d\n", values[0], values[1], y );./a.out Segmentation fault (core dumped)

Static arrays Static arrays are declared/accessed using square brackets: C/C++ does not stop you from accessing values outside the array: You can declare and assign array values at the same time The array size is automatically determined from the assignment int values[] = 0, 130 ; printf( Array values: %d %d\n, values[0], values[1] );./a.out Array values: 0 130

Static arrays You can determine the size of a static array using the sizeof operator int values[] = 0, 130 ; printf( Array size: %d\n, sizeof( values ) );./a.out Array size: 8 Q: Why does the array have size 8 if it only has two entries?

Strings Strings are arrays of null-terminated characters The null termination is required to indicate where the string ends The character \0 has value 0, so either is fine char str[] = h, e, l, l, o, \0 ; printf( str: %s\n, str );./a.out str: hello

Strings Strings are arrays of null-terminated characters The null termination is required to indicate where the string ends The character \0 has value 0, so either is fine The character \n is a new-line The character \t is a tab The character \ is a quote etc. char str[] = h, e, l, l, o, \0 ; printf( str: %s\n, str );./a.out str: hello

Strings Strings are arrays of null-terminated characters The null termination is required to indicate where the string ends Can use double-quotes to assign the string value char str[] = hello ; printf( str: %s\n, str );./a.out str: hello

Strings Strings are arrays of null-terminated characters The null termination is required to indicate where the string ends Can use double-quotes to assign the string value Multiple quoted strings are merged into one long string Makes it possible to split text across multiple lines char str[] = hel lo ; printf( str: %s\n, str );./a.out str: hello

String functions (declared in string.h) strlen: Get the length of a string #include <string.h> int main( void ) char str[] = hello ; printf( string length : %d\n, strlen( str ) );./a.out string length: 5

String functions (declared in string.h) strlen: Get the length of a string #include <string.h> int main( void ) char str[] = hello ; printf( string length / size: %d %d\n, strlen( str ), sizeof( str ) );./a.out string length / size: 5 6 Q: Why are the length and size different?

String functions (declared in string.h) strcpy: Copy the contents of one string into the other The target must be large enough to store the source (and its null-terminator) #include <string.h> char source[] = hello ; char target[6]; strcpy( target, source ); printf( string: %s\n, target );./a.out string: hello

String functions (declared in string.h) strcmp: Compare two strings returns > 0: If the first string comes before the second returns < 0: If the second string comes before the first returns 0: if the strings are equal #include <string.h> char str1[] = hello ; char str2[] = goodbye ; printf( compare( %s, %s ) = %d\n, str1, str2, strcmp( str1, str2 ) );./a.out compare( hello, goodbye ) = 1

String functions (declared in string.h) strtok: Tokenizes a string strcat: Concatenates two strings and much much more http://www.cplusplus.com/reference/cstring/

String functions (declared in stdlib.h) atoi: converts a string into a an integer atof: converts a string into a (double-precision) floating point value #include <stdlib.h> char str[] = 120 ; int i = atoi( str ); double d = atof( str ); printf( %s -> %d : %f\n, str, i, d );./a.out 120 -> 120 : 120.000000

Printing to the console void putchar( char c ): Outputs a single character to the command prompt (standard out) #include <string.h> char str[] = hello ; for( int i=0 ; str[i]!=0 ; i++ ) putchar( str[i] ); putchar( \n );./a.out hello

Printing to the console int printf( const char format_str[],... ): Prints a (formatted) string to the command prompt (standard out) #include <string.h> char str[] = hello ; printf( %s\n, str );./a.out hello

Printing to the console Formally, printf is a variadic function taking a string, followed by an arbitrary number of arguments: int printf( const char format_str[],... );

Printing to the console Formally, printf is a variadic function taking a string, followed by an arbitrary number of arguments: int printf( const char format_str[],... ); In practice, it writes the characters of the first string to the command prompt if it encounters a special character it writes out the next argument. %d: the next argument is an integer %f: the next argument is floating point number %c: the next argument is a character %s: the next argument is a (null-terminated) string etc.

Printing to the console Formally, printf is a variadic function taking a string, followed by an arbitrary number of arguments: In practice, it int printf( const char * str,... ); writes the characters of the first string to the command prompt if it encounters a special character it writes out the next argument. %d: the next argument is an integer %f: the next argument is floating point number %c: the next argument is a character %s: the next argument is a (null-terminated) string etc. char str[] = C3 ; char c = P ; int i = 0; printf( %s%c%d\n, str, c, i );./a.out C3P0 Make sure that the number of arguments matches the number of format tags The compiler will throw a warning, but will still generate executable code.

Printing to the console You can provide further flags as to how things should be printed: %<j>d: At least <j> spaces should be used to print the number int x = 123; printf( x=%2d : x=%4d\n, x, x );./a.out x=123 : x= 123

Printing to the console You can provide further flags as to how things should be printed: %<j>.<k>f: At least <j> spaces should be used to print the number and <k> decimals of precision should be used float x = 1.484; printf( x=%4.1f\n, x );./a.out x= 1.5 Note: numbers will be rounded if the precision isn t large enough

Printing to the console You can provide further flags as to how things should be printed: and much much more http://www.cplusplus.com/reference/cstdio/printf/

Printing to the console Q: What does this code output? char str[] = good bye ; printf( str=\ %s\ \n, str ); str[ strlen( good ) ] = 0; printf( str=\ %s\ \n, str );./a.out str= good bye str= good

Printing to the console Q: What does this code output? Q: How about this? char str[] = good bye ; printf( %d %d\n, strlen( str), sizeof( str ) ); str[ strlen( good ) ] = 0; printf( %d %d\n, strlen( str), sizeof( str ) );./a.out 8 9 4 9

Reading from the console getchar: Reads the next character from the command prompt (standard in) buffer It waits until [ENTER] is hit before returning anything Calling it a second time, will return the second character read etc. char c; while( 1 ) c = getchar(); putchar( c ); if( c== \n ) break;

int main( void ) const int base = 32; const float factor = 5.0 / 9.0; int fahrenheit = 75; Putting it together float celsius = factor * (fahrenheit - base); printf( %d -> %0.2f\n, farenheight, celsius ); #include <stdlib.h> int main( void ) const int base = 32; const float factor = 5.0 / 9.0; while( 1 ) char str[16]; int idx = 0; printf( Enter a temperature: ); while( ( str[idx]=getchar() )!= \n ) idx++; if(!idx ) break; str[idx] = 0; int farenheit = atoi( str ); float celcius = factor * (farenheit - base); printf( %d -> %0.2f\n, farenheit, celcius ); Q: What is wrong with this code?

In-Class Exercises On Piazza, find Resources section, then click Resources tab Scroll down to section for this course section Find link for Exercise 2-1 and follow it Follow the instructions; raise your hand if you get stuck Make sure you check in with a course staff member sometime during this session