C Programming Language Review and Dissection III

Similar documents
CSC209H Lecture 4. Dan Zingaro. January 28, 2015

Create a Program in C (Last Class)

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

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

Characters and Strings

Course organization. Course introduction ( Week 1)

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

by Pearson Education, Inc. All Rights Reserved.

Pointers, Arrays, and Strings. CS449 Spring 2016

Computer Programming: Skills & Concepts (CP) Strings

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

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

CS 137 Part 6. ASCII, Characters, Strings and Unicode. November 3rd, 2017

C mini reference. 5 Binary numbers 12

Principles of C and Memory Management

Characters, Character Strings, and string-manipulation functions in C

Arrays, Strings, & Pointers

8. Characters, Strings and Files

Procedural Programming & Fundamentals of Programming

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

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

Programming in C. Session 7. Seema Sirpal Delhi University Computer Centre

Strings(2) CS 201 String. String Constants. Characters. Strings(1) Initializing and Declaring String. Debzani Deb

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

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

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

Computer Science & Engineering 150A Problem Solving Using Computers

Computers Programming Course 11. Iulian Năstac

Chapter 8 Character Arrays and Strings

Fundamentals of Programming & Procedural Programming

Chapter 8 C Characters and Strings

Lecture 05 Pointers ctd..

C-LANGUAGE CURRICULAM

ONE DIMENSIONAL ARRAYS

SYSTEM AND LIBRARY CALLS. UNIX Programming 2015 Fall by Euiseong Seo

Unit 4. Input/Output Functions

Intermediate Programming, Spring 2017*

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

Introduction to string

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

Procedural Programming

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

First of all, it is a variable, just like other variables you studied

CS1100 Introduction to Programming

CS 33. Introduction to C. Part 5. CS33 Intro to Computer Systems V 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Array Initialization

today cs3157-fall2002-sklar-lect05 1

C Characters and Strings

CYSE 411/AIT681 Secure Software Engineering Topic #12. Secure Coding: Formatted Output

2/9/18. CYSE 411/AIT681 Secure Software Engineering. Readings. Secure Coding. This lecture: String management Pointer Subterfuge

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

Getting Started. Project 1

Computers Programming Course 10. Iulian Năstac

C Libraries. Bart Childs Complementary to the text(s)

Converting a Lowercase Letter Character to Uppercase (Or Vice Versa)

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

Standard C Library Functions

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

CS107 Spring 2019, Lecture 4 C Strings

Lecture 03 Bits, Bytes and Data Types

CS 61c: Great Ideas in Computer Architecture

Chapter 8 - Characters and Strings

Arrays and Strings. CS449 Fall 2017

Chapter 21: Introduction to C Programming Language

1 Pointer Concepts. 1.1 Pointer Examples

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

CS107, Lecture 4 C Strings

Library Functions. General Questions

3/7/2018. Sometimes, Knowing Which Thing is Enough. ECE 220: Computer Systems & Programming. Often Want to Group Data Together Conceptually

Formatting functions in C Language

Introduction to C/C++ Lecture 5 - String & its Manipulation

CSC 2400: Computer Systems. Arrays and Strings in C

BSM540 Basics of C Language

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

C: How to Program. Week /May/28

Do not start the test until instructed to do so!

C Programming Multiple. Choice

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

211: Computer Architecture Summer 2016

Midterm Examination # 2 Wednesday, March 18, Duration of examination: 75 minutes STUDENT NAME: STUDENT ID NUMBER:

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

Programming refresher and intro to C programming

CSC 2400: Computer Systems. Arrays and Strings in C

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

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

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

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

More about BOOLEAN issues

Bil 104 Intiroduction To Scientific And Engineering Computing. Lecture 7

DETAILED SYLLABUS INTRODUCTION TO C LANGUAGE

by Pearson Education, Inc. All Rights Reserved.

Variation of Pointers

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

Split up Syllabus (Session )

Strings in C. Professor Hugh C. Lauer CS-2303, System Programming Concepts

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

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

C++ Arrays. Arrays: The Basics. Areas for Discussion. Arrays: The Basics Strings and Arrays of Characters Array Parameters

Memory, Arrays & Pointers

Winter 2017 CS107 Midterm #2 Examination (Practice #2) Problem Points Score Grader TOTAL 30

Transcription:

C Programming Language Review and Dissection III Lecture 5 Embedded Systems 5-1

Today Pointers Strings Formatted Text Output Reading Assignment: Patt & Patel Pointers and Arrays Chapter 16 in 2 nd edition Chapter 17 in 1 st edition and online notes Embedded Systems 5-2

Pointers A pointer variable holds the address of the data, rather than the data itself To make a pointer point to variable a, we can specify the address of a address operator & The data is accessed by dereferencing (following) the pointer indirection operator * works for reads and writes Assigning a new value to a pointer variable changes where the variable points, not the data 1 2 3 4 5 6 void main ( ) { int i, j; int *p1, *p2; i = 4; j = 3; p1 = &i; p2 = &j; *p1 = *p1+*p2; p2 = p1; } i j p1 p2 1&2 4 3 Adx 600 602 604 606 i j p1 p2 3 4 3 600 i j p1 p2 4 4 3 600 602 i j p1 p2 5 7 3 600 602 i j p1 p2 6 7 3 600 600 Embedded Systems 5-3

More about Pointers Incrementing and decrementing pointers to array elements Increment operator ++ makes pointer advance to next element (next larger address) Decrement operator -- makes pointer move to previous element (next smaller address) These use the size of the variable s base type (e.g. int, char, float) to determine what to add p1++ corresponds to p1 = p1 + sizeof(int); sizeof is C macro which returns size of type in bytes int a[18]; int * p; p = &a[5]; *p = 5; /* a[5]=5 */ p++; *p = 7; /* a[6]=7 */ p--; *p = 3; /* a[5]=3 */ Pre and post Putting the ++/-- before the pointer causes inc/dec before pointer is used int *p=100, *p2; p2 = ++p; assigns 102 to integer pointer p2, and p is 102 afterwards Putting the ++/-- after the pointer causes inc/dec after pointer is used char *q=200, *q2; q2 = q--; assigns 200 to character pointer q2, and q is 199 afterwards Embedded Systems 5-4

What else are pointers used for? Data structures which reference each other lists trees etc. Exchanging information between procedures Passing arguments (e.g. a structure) quickly just pass a pointer Returning a structure Accessing elements within arrays (e.g. string) Embedded Systems 5-5

Pointers and the M16C ISA Address space of M16C is 1 megabyte Need 20 bits to address this This space is divided into two areas Near: 64 kilobytes from 00000h to 0FFFFh can be addressed with a 16-bit pointer (top 4 bits of 20-bit address are 0) Pointer is shorter (2 bytes) Pointer operations are faster Note: internal RAM and SFRs are in this space Far: Entire 1 megabyte area from 00000h to FFFFFh can be addressed with a 20-bit pointer Pointer is longer (4 bytes used (1.5 bytes wasted!)) Pointer operations are slower, since ALU operates on 16 bits at a time Details in section 2.3 of M16C C Programming Manual Embedded Systems 5-6

Specifying Areas By default, RAM data is near, ROM data is far Embedded Systems 5-7

Pointers and the M16C ISA (II) Default locations Near area: RAM data data, bss Far area: ROM data rom, program const data Pointer sizes chosen by compiler based on area holding type of data Near pointer (16 bits) used for near data Far pointer (32 bits) used for far data NC30 doesn t recognize near/far keywords? int * near near_data; does not compile int * far far_data; does not compile int near near_data; does not compile int far far_data; does not compile Embedded Systems 5-8

Strings See Section 16.3.4 of Patt & Patel. There is no string type in C. Instead an array of characters is used - char a[44] The string is terminated by a NULL character (value of 0, represented in C by \0). Need an extra array element to store this null Example char str[10] = testing ; t e s t i n g \0 str str[0] str[1] str[2] Embedded Systems 5-9

Formatted String Creation Common family of functions defined in stdio.h printf: print to standard output sprintf: print to a string fprintf: print to a file Syntax: sprintf(char *str, char * frmt, arg1, arg2, arg3.. ); str: destination fmt: format specifying what to print and how to interpret arguments %d: signed decimal integer %f: floating point %x: unsigned hexadecimal integer %c: one character %s: null-terminated string arg1, etc: arguments to be converted according to format string Embedded Systems 5-10

sprintf Examples strings and integers char [30], s2[30]; int a=5, b=10, c=-30; char ch= $ ; sprintf(, Testing ); sprintf(s2, a=%d, b=%d, a, b); sprintf(, b=%x, c=%d, b, c); sprintf(, b=0x%x, b); sprintf(s2, =%s, ); sprintf(, %c %c, ch, s2); Testing s2 a=5, b=10 b=a, c=-30 b=0xa s2 =b=0xa $ s Embedded Systems 5-11

sprintf Examples floating-point Variation on %f format specifier %-w.pf - = left-justify. Optional w = minimum field width (# of symbols) p = precision (digits after decimal point) Examples float f1=3.14, f2=9.991, f3=-19110.331; char [30], s2[30]; sprintf(, %f, f1); sprintf(, %f, f3); sprintf(, %4.1f, f2); 3.140000-19110.3 10.0 Embedded Systems 5-12

sprintf Examples More Integers Variation on %d format specifier for integers (d/i/o/x/u) %-w.pd - = left justify. Optional w = minimum field width (# of symbols) p = precision (digits). Zero pad as needed Examples int a=442, b=1, c=-11; char [30], s2[30]; sprintf(, %5d, a); sprintf(, %-4d, b); sprintf(, %4d, b); sprintf(, %-5.4d, c); 442 1 1-011 Embedded Systems 5-13

String Operations in string.h Copy ct to s including terminating null character. Returns a pointer to s. char* strcpy(char* s, const char* ct); = cheese ; s2 = limburger ; strcpy(, s2); /* = limburger */ Concatenate the characters of ct to s. Terminate s with the null character and return a pointer to it. char* strcat(char* s, const char* ct); = cheese ; s2 = puffs ; strcat(, s2); /* = cheese puffs */ Embedded Systems 5-14

More String Operations Concatenate at most n characters of ct to s. Terminate s with the null character and return a pointer to it. char* strncat(char* s, const char* ct, int n); = cheese ; s2 = puffs ; strncat(, s2, 4); /* cheese puf */ Compares two strings. The comparison stops on reaching a null terminator. Returns a 0 if the two strings are identical, less than zero if s2 is greater than, and greater than zero if is greater than s2. (Alphabetical sorting by ASCII codes) int strcmp(const char*, const char* s2); = cheese ; s2 = chases ; strcmp(,s2); /* returns non-zero number */ strcmp(, cheese ); /* returns zero */ Embedded Systems 5-15

More String Operations Return pointer to first occurrence of c in, or NULL if not found. char* strchr(const char*, int c); = Smeagol and Deagol ; char a *; a = strchr(, g ); /* returns pointer to [4] */ Return pointer to last occurrence of c in, or NULL if not found. char* strrchr(const char*, int c); = Smeagol and Deagol ; char a *; a = strrchr(, a ); /* returns pointer to [14] */ Can use the returned pointer for other purposes *a = \0 ; /* = Smeagol and De */ strcat(, spair ); /* = Smeagol and Despair */ Embedded Systems 5-16