Appendix A Developing a C Program on the UNIX system

Similar documents
Unit 3, Lesson 2 Data Types, Arithmetic,Variables, Input, Constants, & Library Functions. Mr. Dave Clausen La Cañada High School

Numbers and Computers. Debdeep Mukhopadhyay Assistant Professor Dept of Computer Sc and Engg IIT Madras

Chapter 2 Bits, Data Types, and Operations

o Echo the input directly to the output o Put all lower-case letters in upper case o Put the first letter of each word in upper case

1.1. INTRODUCTION 1.2. NUMBER SYSTEMS

Data Representation and Binary Arithmetic. Lecture 2

Fundamentals of Programming (C)

Number System (Different Ways To Say How Many) Fall 2016

Chapter 2 Bits, Data Types, and Operations

Chapter 2 Bits, Data Types, and Operations

Bits and Bytes. Data Representation. A binary digit or bit has a value of either 0 or 1; these are the values we can store in hardware devices.

CPS 104 Computer Organization and Programming Lecture-2 : Data representations,

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

SWEN-250 Personal SE. Introduction to C

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

Fundamentals of Programming. Lecture 11: C Characters and Strings

Number Systems for Computers. Outline of Introduction. Binary, Octal and Hexadecimal numbers. Issues for Binary Representation of Numbers

Reminder. Sign up for ee209 mailing list. Precept. If you haven t received any from ee209 yet Follow the link from our class homepage

Chapter 3. Information Representation

Chapter 2 Bits, Data Types, and Operations

C Examples. Goals of this Lecture. Overview of this Lecture

today cs3157-fall2002-sklar-lect05 1

CSE 30 Fall 2012 Final Exam

APPENDIX A : KEYWORDS... 2 APPENDIX B : OPERATORS... 3 APPENDIX C : OPERATOR PRECEDENCE... 4 APPENDIX D : ESCAPE SEQUENCES... 5

CSE 30 Fall 2013 Final Exam

Chapter 2 Bits, Data Types, and Operations

EE 109 Unit 3. Analog vs. Digital. Analog vs. Digital. Binary Representation Systems ANALOG VS. DIGITAL

Fundamentals of Programming

C PROGRAMMING. Characters and Strings File Processing Exercise

6.096 Introduction to C++ January (IAP) 2009

Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. C Data Types

Number Systems II MA1S1. Tristan McLoughlin. November 30, 2013

CS/ECE 252: INTRODUCTION TO COMPUTER ENGINEERING UNIVERSITY OF WISCONSIN MADISON

Data Representa5on. CSC 2400: Computer Systems. What kinds of data do we need to represent?

Data Representa5on. CSC 2400: Computer Systems. What kinds of data do we need to represent?

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

Contents. Preface. Introduction. Introduction to C Programming

CS/ECE 252: INTRODUCTION TO COMPUTER ENGINEERING UNIVERSITY OF WISCONSIN MADISON

Binary Numbers. The Basics. Base 10 Number. What is a Number? = Binary Number Example. Binary Number Example

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

C mini reference. 5 Binary numbers 12

CSE 30 Winter 2014 Final Exam

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

Chapter 8 - Characters and Strings

EE 109 Unit 2. Analog vs. Digital. Analog vs. Digital. Binary Representation Systems ANALOG VS. DIGITAL

Number Representations

DATA REPRESENTATION. Data Types. Complements. Fixed Point Representations. Floating Point Representations. Other Binary Codes. Error Detection Codes

CS341 *** TURN OFF ALL CELLPHONES *** Practice NAME

Midterm CSE 131 Winter 2012

Multiple Choice Questions ( 1 mark)

Week 1 / Lecture 2 8 March 2017 NWEN 241 C Fundamentals. Alvin Valera. School of Engineering and Computer Science Victoria University of Wellington

CSCI 171 Chapter Outlines

UNIT IV-2. The I/O library functions can be classified into two broad categories:

3.1. Unit 3. Binary Representation

ASSIGNMENT 5 TIPS AND TRICKS

Midterm CSE 131 Winter 2014

Chapter 2 - Introduction to C Programming

CMSC 313 Lecture 03 Multiple-byte data big-endian vs little-endian sign extension Multiplication and division Floating point formats Character Codes

C: How to Program. Week /Mar/05

Exercises Software Development I. 03 Data Representation. Data types, range of values, internal format, literals. October 22nd, 2014

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

Model Viva Questions for Programming in C lab

Do not start the test until instructed to do so!

The Binary Number System

Unit 3. Analog vs. Digital. Analog vs. Digital ANALOG VS. DIGITAL. Binary Representation

Simple Data Types in C. Alan L. Cox

Chapter 2 Number System

Oberon Data Types. Matteo Corti. December 5, 2001

UNIT- 3 Introduction to C++

EE 109 Unit 2. Binary Representation Systems

CSE 30 Winter 2009 Final Exam

Chapter 8 C Characters and Strings

Final CSE 131 Winter 2010

Fundamental of Programming (C)

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

C: How to Program. Week /May/28

CSE 30 Spring 2006 Final Exam

Do not start the test until instructed to do so!

Do not start the test until instructed to do so!

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

DEPARTMENT OF MATHS, MJ COLLEGE

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

CSE 30 Fall 2007 Final Exam

2a. Codes and number systems (continued) How to get the binary representation of an integer: special case of application of the inverse Horner scheme

Number Systems Base r

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

LESSON 4. The DATA TYPE char

Experiment 3. TITLE Optional: Write here the Title of your program.model SMALL This directive defines the memory model used in the program.

The following are the data types used in the C programming language:

Midterm CSE 131 Fall 2014

CSE 30 Spring 2007 Final Exam

Positional Number System

This is great when speed is important and relatively few words are necessary, but Max would be a terrible language for writing a text editor.

Computer Language. It is a systematical code for communication between System and user. This is in two categories.

Data types, variables, constants

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

Midterm CSE 131 Winter 2013

AIR FORCE SCHOOL,BAMRAULI COMPUTER SCIENCE (083) CLASS XI Split up Syllabus (Session ) Contents

Split up Syllabus (Session )

The Waite Group's. New. Primer Plus. Second Edition. Mitchell Waite and Stephen Prata SAMS

Transcription:

Appendix A Developing a C Program on the UNIX system 1. Key in and save the program using vi - see Appendix B - (or some other editor) - ensure that you give the program file a name ending with.c - to indicate that it is a C program file. 2. Compile and link the program using cc <program name> This will produce two files: a) an object file with the same name as your original C program except that the extension will be.o b) an executable file (i.e. a program ready to run) called a.out. e.g. cc test.c compiles test.c producing an object program called test.o then automatically links the object program with relevant library files to produce a runnable program called a.out. 3. Run your program by typing a.out If you would prefer your executable program to be called something else then simply copy a.out to a new name. e.g. cp a.out test copies a.out to produce a runnable program called test. Alternatively, when compiling the program you can specify a particular name for the runnable program by using the -0 option followed by the desired name. e.g. cc -0 test test.c This will compile the program test.c producing - as before - an object file called test.o - and then link the program producing an executable file called test. So, the program can now be run simply by typing test. 265

Appendix B - Using vi One of the standard editors on the UNIX system is called vi. It is not particularly friendly, but you will quickly learn to use it - and can usually rely on it being available on most UNIX systems. Starting the vi Editor: vi <program name> Make up your own program name - but finish it with a suitable extension for the language you are using - which for C will be.c. e.g. vi test.c (allows you to key in a C program called test.c) Using vi Unlike a word processor you cannot simply start typing - but must tell the editor that you intend to do so by keying <escape> i. Every time you want to change what you are doing - say go from typing in text, to deleting text or saving the file - you will have to tell the editor that you intend to do so by pressing <escape> followed by the appropriate code. <ESC> I <ESC> x <ESC> dd <ESC> A <ESC> followed by an arrow key <ESC> :w <ESC> :w <program name> <ESC> :wq <ESC> :q! insert text delete a character (under the cursor). delete a line add text at the end of the line move around in the text save the program file save the program file with a new name save the program me and quit quit without saving Note that for the last four commands, when the colon (:) is pressed after the <escape> key, a colon will appear at the bottom ofthe screen ready for the w, wq or q! to be typed. Note that UNIXtreats capital letters andsmallletters as different. The vi editor has a wide range of additional commands which will generally be found in one of the manuals for your system. 266

Appendix C - The Binary System Our normal system of counting is denary. When we count past nine, we cannot represent 10 in a single digit so we 'carry' and use an additional digit. You could place column headings above a figure to help with additions, etc. (young children would probably do so). The column headings for the denary system would be:...1000s..100s..los..is - so that, for example, the number 271 would be arranged in columns as: 1005 105 15 2 7 1 In other words, 271 means 2 lots of 100 plus 7lots of 10 plus 1 unit. We use this number system (also called Base 10 because the column headings grow larger - as they move leftwards - by being multiplied by 10) because we have 10 fingers. A computer, however, obviously does not have 10 fingers; it counts by switching electric currents ON (to represent 1) and OFF (to represent 0). If the computer needs to count further than one, it has to carryjust as we have to do if we want to count past 9. A computer, therefore, uses Base2 (i.e the Binary System) to count. It is possible to place column headings above a binary number - these are multiplied by 2 as they move leftwards:...25612864 32168421 So - for example - the binary number 11001 would appear as: 16 8 4 2 1 1 1 001 meaning 1 lot of 16 + 1 lot of 8 + 1 lot of 1 =25 denary. Think of all the binary digits as being electric lamps - each one representing a column heading. Ifa particular lamp's value is needed to help make up a number, the light is switched ON - otherwise the light is switched OFF. The digit 0 is used to indicate that the light is off - and 1 to indicate that it is on. 0 1 I 0 0 0 I 1 ~ ~g- -g- ~ ~! -g--g- 128 64 32 16 8 4 2 ON ON ON ON 64 + 32 + 2 + 1 = 99 267

Converting Binary numbersto Denary e.g. Convert 110011 Binary to Denary. Step 1. Arrange the numbers under binary column headings - so that the number finishes in the rightmost (Is) column. (To make the headings - start on the right with 1 - then keep multiplying by 2 as you move to the left - until there are enough headings to cover the whole number.) 32 16 8 4 2 1 1 100 1 1 Step 2. Add all the headings from those columns which contain Is to get the final answer. So, 32 + 16 + 2 + 1 = 51. (The 8 and 4 columns do not contain Is - so they are ignored. So, 1I 0011 in binary can be written as 51 in denary. Converting Denarynumbersto Binary e.g. convert39 to binary Step 1. Write down the binary column headings finishing on the left with the largest binary column heading which is less than the denary number that you are converting. In this case the largest heading which will go into 39 is 32. 32 16 8 4 2 1 Step 2. Subtract the largest heading from the number and write a I in that column (i.e. the 32 column in this case). Keep a note of the remainder. 32 16 8 4 2 1 1 Step 3. Repeat Step 2 with the remainder (in this case 7) and keep doing so until the whole number has been used up. So, the final answer is: 32 16 8 4 2 1 1 0 0 1 1 1 So, 39 Denary can be written as 1001I 1 Binary. 268

Signed Binary Numbers Ifa variable is set up as signed (so that it can hold positive or negative values) the most significant bit (i.e, the largest column heading) is used to denote whether the number is positive or negative. Possibly the most common notation is two's complement. The most significant bit's column heading is made negative (all the other columns remain positive). So for a one-byte location, the headings will be: -128, 64,32, 16,8,4,2, 1. For a two-byte location (e.g. a typical integer) the headings are: -32768,16384,8192,4096,2048, 1014,512,256.128,64,32,16,8,4,2,1 As for unsigned numbers, the value of a number is found by adding all the column headings that are switched 0". 1 000 1 0 10 -'g-~ - 12 8 6 4 32 16!! -'g-! -'g-ft 8 4 2 ON - 128 + 8 ON ON + 2-1 18 269

AppendixD - The Hexadecimal System The Hexadecimal System (i.e. base 16) is a convenient shorthand for Binary and is used - for example - in machine code programming. The column headings for the hexadecimal system are:... 4096s 256s 16s Is (multiplying by 16 as you move left) So - for example - 122 hexadecimal would mean: I x 256 + 2 x 16 + 2JU. =290 denary Because it is necessary to be able to count up to 15 in a single column without carrying - extra characters are needed to represent 10, II, 12, 13 and 14 as single digits; the letters A, B, C, D, E and F are used for this purpose. The Hexadecimal Digits are therefore: 0 1 2 34 5 6 78 9ABC D E F ConvertingBinary to Hexadecimal It is far simpler to convert binary to hexadecimal than to tum it into decimal. e.g. Convert11100111 BinaryintoHexadecimal. Step I Startingfrom the right, split the binary number into groups of four digits (quartets): llloolll Step 2 Again starting from the right place column headings above the binary digits - treating each quartetas separate. 8421 8421 1110 0111 Step 3 Now convert each quartet into a hexadecimal digit. 8421 1 I 10 =14 Decimal =E Hexadecimal 8421 01 I 1 =7 Decimal =7 Hexadecimal Step 4 Place the hexadecimal digits together. So the answer is E7 Hexadecimal 270

Appendix E Character Codes Letters of the alphabet, punctuation marks, control codes - such as carriage return, and numeric digits ready to be sent immediately for display are represented by a character code. One of the most common is ASCIT(American Standard Code for Information Interchange). If a character held in ASCII is sent to an output device such as printer or VOU, the device will automatically output the appropriate characters shape. If, for example, a numeric digit is not held in a character code such as ASCII, it will have to be converted first before the device will recognise it. So, the command printj("%d", numl} will convert the value in numl from pure binary to a suitable character code (one code for each digit of the number) before sending it to the screen for display. However, the command printj("%c". numl) will simply send the binary pattern stored in numl to the screen - on the assumption that it contains a recognisable code for a single digit (or other character). Note that ASCII is a seven bit code, so the most significant bit of a byte (e.g. a character location) is not used. 271

ASCllCodes Character Binary Hex Character Binary Hex <NUL> 00000oo 00 @ 100000o 40 <SOH> 0000001 01 A 1000001 41 <STX> ()()()()()10 02 B 1000010 42 <ETX> ()()()()()II 03 C 1000011 43 <EOT> 0000100 04 0 1000100 44 <ENQ> 0000101 05 E 1000101 45 <ACK> 0000110 06 F 1000110 46 <BEL> 0000111 07 0 1000111 47 <BS> 0001000 08 H 1001000 48 <HT> 0001001 09 1 1001001 49 <VT> 0001010 OA J 1001010 4A <LF> 0001011 OB K 1001011 4B <FF> 0001100 OC L 1001100 4C <CR> 0001101 00 M 1001101 40 <SO> 0001110 OE N 1001110 4E <SI> 0001111 OF 0 1001111 4F <DLE> 0010000 10 P 1010000 50 <DCl> 0010001 11 Q 1010001 51 <DC2> 0010010 12 R 1010010 52 <DC3> 0010011 13 S 1010011 53 <DC4> 0010100 14 T 1010100 54 <NAK> 0010101 15 U 1010101 55 <SYN> 0010110 IV 1010110 56 <ETB> 0010111 17 W 1010111 57 <CAN> 0011000 18 X 1011000 58 <EM> 0011001 19 Y 1011001 59 <SUB> 0011010 la Z 1011010 SA <ESC> 0011011 IB [ 1011011 5B <FS> 0011100 lc \ 1011100 5C <OS> 0011101 10 ) 1011101 50 A <RS> 0011110 IE 1011110 5E <US> 0011111 IF 1011111 SF ~ <SP> 0100000 20 1100000 60! 0100001 21 a 1100001 61 " 0100010 22 b 1100010 62 #/ 0100011 23 c 1100011 63 $ 0100100 24 d 1100100 64 % 0100101 25 e 1100101 65 & 0100110 26 f 1100110 66 0100111 27 g 1100111 67 ( 0101000 28 h 1101000 68 ) 0101001 29 i 1101001 69 0101010 2A j 1101010 6A + 0101011 2B k 1101011 68 0101100 2C 1 1101100 6C 0101101 20 m 1101101 60 0101110 2E n 1101110 6E / 0101111 2F 0 1101111 6F 0 0110000 30 p 1110000 70 I 0110001 31 q 1110001 71 2 0110010 32 r 1110010 72 3 0110011 33 s 1110011 73 4 0110100 34 t 1110100 74 5 0110101 35 u 1110101 75 6 0110110 36 v 1110110 76 7 0110111 37 w 1110111 77 8 0111000 38 x 1111000 78 9 0111001 39 y 1111001 79 0111010 3A z 1111010 7A 0111011 3B ( 1111011 7B < 0111100 3C I 1111100 7C 0111101 30 ) 1111101 70 > 0111110 3E 1111110 7E? 0111111 3F <DEL> 1111111 7F 272

Index #dejine 38 actual parameters 26 addition 11 address 130 Address arithmetic 133 ampersand 9 and 17 ar 221 argc 259 arguments 26, 226 argv 259 Arithmetic 11, 12 arithmetic shift 243 arithmetic signs 11 array 133,137,139,255 Array of pointers 259 Arrays - Single Dimensional 113, 114 Arrays - Two Dimensional 116, 117 Arrays of Pointers 139 Arrays of Pointers to Functions 255 Arrays of Strings 119 Arrays of Structures 121, 122 ASCn 271 Assignment 10, 106 Assignment Operators 12 atof 250 atoi 250 atol 250 automatic 37 binary stream 184 Binary System 267 Bit-fields 245, 247 Bitwise AND 239 Bitwise NOT 242 Bitwise Operators 239 Bitwise OR 240 Bitwise XOR 241 block 21,43,226 bounds checking 114 braces 3, 16, 19,21,43 break 18,44 buffer 50 buffered input 49 Buffered Keyboard Input 50 byte 124 case 18 cast 145,264 cc 215,265 Central Memory 124 char 6,56 character 6, 10 Character Codes 271 Character Type Functions 248 Circular Linked Lists 158 Command Line Arguments 259 comments 4 Comparing Pointers 135 compilation errors 2 Compile 265 compiler 1 Compiling and Linking Modules 215 Conditional Expressions 230 const 237 continue 44 Conversion of Data Types 261 ctype.h 248 Data Types 56 declaration 213 Decrementing 13 default 18 division II do..while 20, 52 Dynamic Memory Allocation 145, 147 end-of-string character 119 enum 40 EOF 193, 194, 198 Evaluating a Condition 52 executable file 265 exit 45 extern 216 extemallinkage 213 extemal variables 35 fclose 186, 189, 191, 194, 196, 199, 206 feof 189, 199 ferror 206 fgetc 198 field 184 FILE 185, 188, 191, 193, 196, 198 Files 184 Files - Handling Errors 205 Files - Reading Data from Disk one byte at a time using getc 198 Files - Reading Data from Disk using fread 188 Files - Reading Formatted Text from Disk using fscanf 193 Files - Storing Data on Disk using fwrite 185 Files - Writing Data to Disk - one byte at a time - using putc 196 Files - Writing Formatted Text to Disk using fprintf 191 float 6 float.h 58 floating point 6 fopen 185, 188, 191. 193, 196, 198, 205 for 21 273

formal parameters 26 Formatting Output 46 fprintf 191, 207 fputc 196 fread 188,206 free 146, 153, 161, 170, 183 fseanf 193,207 fseek 200, 208 Functions 22,24,26,28,30,213 Functions - 32 fwrlte 186, 206 getc 198,208 getehar 49, 50 gets 9,50 Global Variables 35,216 goto 44 header file 222 header files 3, 5, 22 Hexadecimal 270 hierarchical 70, 83 Hierarchical Coding 83, 86, 88 if 52 if.. else 14, 15, 16 Implementing a Complex Program Design using Hierarchical Code 90 Implementing a Complex Program Design using In-line Code 79 Implementing a Design 70 Implementing a Program Design 93 in-line 70 In-line Coding 71, 75, 77 Incrementing 13 initialise 10 initialising 19 Input Format 48 int 6,56 integer 6 isalnum 248 isalpha 248 isdigit 248 islower 248 isprint 249 isspace 249 isupper 248 iteration 61,75,86 Keywords 5 label 44 Layout of a C Program 4 Left Shift 243 library 221 library files 3, 265 limits.h 58 link 265 Linked Lists 149 Local Variables 24,226 logic error 2 Logical Operators 17 274 logical shift 243 long 56 long int 57 loop 19 machine code 1 macro 224, 226 Macros - using Arguments 226 mauoe 145, 147, ISO, 159, 168, 174 Mixing data types 263 Modular Programming 212 module 212 modulus 11 multiplication 11 Nested Structures 109, 111 newline 8 not 17 object file 265 Object Libraries 221 Operations on Structures 106 or 17 out-of-line 70, 83 parameter 106, 130, 137,257 Parameters 26 Passing Arrays as Parameter 137 Passing Function Addresses as Parameter 257 Passing Pointers as Parameters 130 pointer to a function 257 pointer variable 127, 128, 135 Pointers 126, 133, 139 Pointers and Arrays 133 Pointers to Arrays of Pointers 143 Pointers to Functions 251, 255 Pointers to Pointers 142 Pointers to Structures 131 preprocessor 224 print! 3,7,9,46,57 processor 236 Program Design 59 prototype 22 putc 196, 208 putchar 49 Queues 167 Random Files 200 record 184, 188 Recursion 34 recursive 179 register 236 Register Variables 236 relational expression 52 Relational Operators 14 remainder 11 reserved 5 return 3, 28, 44, 107 Right Shift 243 run-time error 2 scanf 7,9,48,50,57 selection 62,77,88

semi-colon 3 sequence 60, 71 short 56 short int 57 signed 56 Signed Binary Numbers 269 signed char 56 signed int 56 signed short int 57 sizeof 146 standard library 5 static 37,218 Static Local Variables 37 stdio.h 3 stdlib.h 45, 145,250 storage location 124 strcmp 42 strcpy 42 stream 184 string 106,113,119 String Functions 42 string.h 42 Strings 9, 10 strlen 42 struct 101, 104 structure 111,121,122,131,233,245 Structure Diagrams 59 Structure Types 104, 105 Structures 101, 102, 109 Subscripting Pointers 135 subtraction 11 switch 18 symbolic constant 38, 40 symbolic constants 58 tab 8 Test Plan 97, roo Testing Software 96 text format 184 text stream 184 tolower 249 toupper 249 Trees 173 typedef 228 unbuffered input 49 union 231,232,233 union type 231 Unions and Structures 233 UlVlX 1,215,265,266 unsigned 56 unsigned char 56 unsigned int 56 unsigned long int 57 unsigned short int 57 Using Pointers 128 Variables 6 vi 265,266 Vocabulary 5 void 22 275 volatile 237 while 19,52 white space characters 50 word 124