Homework Reading Programming Assignments Finish K&R Chapter 1

Slides:



Advertisements
Similar presentations
Chapter 11 Introduction to Programming in C
Advertisements

Advanced Topics Object-Oriented Programming Using C++ Second Edition 13.
Differences between Java and C CS-2303, C-Term Differences between Java and C CS-2303, System Programming Concepts (Slides include materials from.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
Homework Reading –Finish K&R Chapter 1 (if not done yet) –Start K&R Chapter 2 for next time. Programming Assignments –DON’T USE and string library functions,
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
Homework Reading Programming Assignments
Strings in C. Strings are Character Arrays Strings in C are simply arrays of characters. – Example:char s [10]; This is a ten (10) element array that.
C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
CNG 140 C Programming (Lecture set 9) Spring Chapter 9 Character Strings.
EX_01.1/46 Numeric Systems. EX_01.2/46 Overview Numeric systems – general, Binary numbers, Octal numbers, Hexadecimal system, Data units, ASCII code,
Chapter Three The UNIX Editors. 2 Lesson A The vi Editor.
STRING Dong-Chul Kim BioMeCIS UTA 10/7/
C Tutorial Session #2 Type conversions More on looping Common errors Control statements Pointers and Arrays C Pre-processor Makefile Debugging.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
1 Exam / Homework Exam 1 in Class 10 –Open book / open notes HW3 due next class HW4 will be on-line soon. Finishing Chapter 2 of K&R. We will go through.
Data Representation, Number Systems and Base Conversions
Chapter Three The UNIX Editors.
CSE 351 GDB Introduction. Lab 1 Status? How is Lab 1 going? I’ll be available at the end of class to answer questions There are office hours later today.
1 Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 at end of class today –Continue working on HW3 Questions?
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
1 Homework Done the reading? –K&R –Glass Chapters 1 and 2 Applied for cs240? (If not, keep at it!) Gotten a UNIX account? (If not, keep at it!)
Topic 14.1 Extended Hexadecimal  Decimal is base 10 and uses 10 digits (0,1,2,3,4,5,6,7,8,9).  Binary is base 2 and uses 2 digits (0,1).  Computers.
Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Strings Chapter 5.
Characters and Strings
CMSC 104, Version 8/061L14AssignmentOps.ppt Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips Reading Section.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
Chapter 3: Mastering Editors Chapter 3 Mastering Editors (Emacs)
Bitwise Operations C includes operators that permit working with the bit-level representation of a value. You can: - shift the bits of a value to the left.
Binary Representation in Text
Binary Representation in Text
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
Topics Designing a Program Input, Processing, and Output
EGR 2261 Unit 10 Two-dimensional Arrays
CMSC201 Computer Science I for Majors Lecture 22 – Binary (and More)
Week 3 - Friday CS222.
© 2016 Pearson Education, Ltd. All rights reserved.
Chapter 22 – part a Stream refer to any source of input or any destination for output. Many small programs, obtain all their input from one stream usually.
Assembly Language Programming Part 3
Debugging with gdb gdb is the GNU debugger on our CS machines.
Guide To UNIX Using Linux Third Edition
A First Book of ANSI C Fourth Edition
Arrays in C.
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Strings, Line-by-line I/O, Functions, Call-by-Reference, Call-by-Value
Chapter 7 LC-2 Assembly Language.
Debuggers.
Bases and Representations, Memory, Pointers, Arrays, For-Loops
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Chapter 11 Introduction to Programming in C
Introduction to CS Your First C Programs
Common C Programming Errors, GDB Debugging
Differences between Java and C
Your questions from last session
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Homework Continue with K&R Chapter 5 Skipping sections for now
Homework Finishing Chapter 2 of K&R. We will go through Chapter 3 very quickly. Not a lot is new. Questions?
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Chapter 3 - Binary Numbering System
Format String Vulnerability
ECE 120 Midterm 1 HKN Review Session.
Presentation transcript:

Homework Reading Programming Assignments Finish K&R Chapter 1 Start K&R Chapter 2 for next time. Programming Assignments DON’T USE <string.h> and string library functions, e.g. strlen ( ) – Write your own!

Octal and Hex Numbers People normally deal in numbers base 10 Computers normally deal in numbers base 2 The problem: Reading a long string of 1’s and 0’s not easy Conversion between base 2 and base 10 not easy The solution: Convert binary digit strings to Octal or Hex Easily done because 23 = 8 and 24 = 16

Octal and Hex Numbers Look at a long string of binary digits in groups 3 digits for Octal 4 digits for Hex See the following examples: Binary Digits Grouped by threes For Octal Grouped by fours For Hex Don’t convert binary to/from Hex/Octal via decimal! 011010101100 … 011 010 101 100 … 003 002 005 004 … The most significant bit is always zero in the ascii table 0110 1010 1100 … 0x6 0xa 0xc …

ASCII Code For computers to process our letters, digits, punctuation marks, etc, we need a binary code for each such “character”. American Standard Code for Information Interchange (ASCII) provides these codes. See ASCII code chart Standard 8 bit bytes and 16 bit words are not integer multiples of 3 bits but are integer multiples of 4 bits – favoring use of Hex!

ASCII Codes in visitype.c To convert a character’s (integer) value to a printable ASCII string for what it represents: We need a table of 4 byte character strings each ending in a zero byte (created by using “\0”) Arranged in the order of the character values used in the ASCII code to represent them And to create the address of one of these strings to pass to function printf e.g. &asciiname[4*i] Show visittype.c You do not have to say the size of array if you are assigning the values right away. Show the memory table again to show asciiname[] Each one of them has 4 Bytes Ampersand sign means the address and not the value Printf needs the address for printing the arrays You can use my visittype.c code and edit it in your hw

Analysis of visitype.c See separate program text “visitype.c” ASCII code conversion array char asciiname [] = …. ; Initialization values for the array 128 strings of length 4 including the ‘\0’ … asciiname[0] …[1] …[2] …[3] …[4] …[5] …[6] ‘N’ ‘U’ ‘L’ ‘\0’ ‘S’ ‘O’ ‘H’ …

Analysis of visitype.c There are a few characters which can't be placed in the quoted initialization string directly, for example " and \. "∆∆"\0" (where " is hex 22) Problem is that " as a character will be interpreted as the end of string. We need to indicate that it is quoted, that it is to be taken literally by the compiler as an ASCII value, and the way to do that is precede it by a \.  "∆∆\"\0" (where " is hex 22) Now there seem to be five chars in that string, “∆∆\"\0” But there are not, \" is a single character value, just as \0 is

Analysis of visitype.c What other characters MAY need the same special treatment? See K&R page 193. Good page to mark for open book tests! Do all of these require special treatment here? No, which one other than \“ needs it? Answer: (write in here) _________

Analysis of visitype.c Access the strings in the array (i = 0 to 127) asciiname[4*i] Array index 4*i indicates start of each string However, printf needs a pointer to a string &asciiname[4*i] The “&” is the “address of” operator Take on faith for now - more on pointers later Part 4 of hw2 is histogram. Means to count every characters appearance. So you have a file and you will build a histogram for it

More on Debugging 2 ways to debug a program: Use printfs Insert printfs in multiple places in your program and print out intermediate values Use gdb debugger A professional programmer uses a debugger, rather than putting in lots of printf statements to track down a bug. A quick gdb reference guide is posted here: http://www.cs.umb.edu/~ramin/cs240/ gdb-refcard.pdf You don’t need to say the memory location for the variables, you should just use the name of them

Use of the gdb Debugger Start with the correct compiler options: gcc -g vt.c -o vt Type the following to run the program: gdb vt Gives message: Ready to run -- not yet running. creates an executable that has debugging info, e.g. - data type for variables/functions - correspondence between line # and addresses You have the same executable file vt

Use of the gdb Debugger(cont’d) Want to interact with running program, not letting it run free. To set a break point at main(), type: b main break at main() program vt is not running yet To run, type: r <vt.in run, taking stdin from vt.in Will stop when encounters main() in program execution -- often lot of things get done first. Now can single step through program, s or n (skip entering functions), put out values of variables. You don’t need to edit run edit run… Break point means the program will execute until that point. So it will stop at main and ask you what the next command is Now you have some choices: You can continue running You can do single stepping. One statement at a time. It executes the next statement and then stop Explain different of s and n When you get to a func, when you type “s” it will go to the function and by each “s” it will execute one sentence in the function But you have troubleshooted the function and you know it works well, type “n”, it will execute the entire function and return to the next statement

Use of gdb Examples of gdb commands: p i (print value of variable i) p i=2 (set the variable i to 2 and print it) p 3*i (print value of expression 3*i) p/x i (print in hex format value of variable i) set variable i=5 (set the variable i to 5 without printing) i lo ("info" - give values of all local variables) h (help -- pretty good messages -- lists topics) h topic (help on named topic) h p (help on command p for printf) q TO QUIT (leave debugger)

Use of gdb (cont’d) More complex gdb commands in Reference Guide. Setting breaks/conditional breaks at line numbers: b 36 b fn.c:22 if i = = 3 Getting line numbers from "list" or "l" command: l 22 print 10 lines around line 22 in main l after listing some lines, then l means next 10 lines i b to get info on breakpoints d 3 to delete bkpt 3 c for continue after bkpt encountered Show some demos