1 Review of Class on Oct 12. 2 Outline of Chapter 4  How to write a function?  Function Prototypes  Function Invocation  Function Definition  The.

Slides:



Advertisements
Similar presentations
Lecture 2 Introduction to C Programming
Advertisements

Introduction to C Programming
C Programming Lecture 7 Functions. Structured Programming b Keep the flow of control in a program as simple as possible. b Use top-down design. Keep decomposing.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection.
1 Lecture 7  Fundamental data types in C  Data type conversion:  Automatic  Casting  Character processing  getchar()  putchar()  Macros on ctype.h.
1 Review of Chapter 3--- Flow of Control  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound.
1 Chapter 6 The Fundamental Data Types. 2 Outline  Declarations and expressions  Fundamental data types  Characters and the data type char  The Data.
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
1 Functions (covered by Chapter 5 in ABC). 2 Type function_name( parameter list ) { Declarations Statements } Function Definition Header body Partitioning.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
1 Lecture 6  Functions  C Standard Libraries  Invocation  Definition  Call-by-value  Prototype  Multi-file program  Macros  Readings: Chapter.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
1 Chapter 4 (II) Functions and Structured Programming.
1 Character Processing How characters can be treated as small integers? How characters are stored and manipulated in a machine? How use is made of certain.
Introduction to C Programming
1 Agenda Variables (Review) Example Input / Output Arithmetic Operations Casting Char as a Number (if time allow)
1 Review of Chapter 6: The Fundamental Data Types.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Chapter 18 I/O in C. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Standard C Library I/O commands.
CMPE13 Cyrus Bazeghi Chapter 18 I/O in C. CMPE Standard C Library I/O commands are not included as part of the C language. Instead, they are part.
Chapter 5: Data Input and Output Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
BBS514 Structured Programming (Yapısal Programlama)1 Character Processing, Strings and Pointers,
C Programming Lecture 4 : Variables , Data Types
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
C Programming Lecture 10 Instructor: Wen, Chih-Yu Department of Electrical Engineering National Chung Hsing University.
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
CS140: Intro to CS An Overview of Programming in C (part 3) by Erin Chambers.
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
CS115 FALL Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3.
CSC Programming for Science Lecture 8: Character Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
FUNCTIONS. Midterm questions (1-10) review 1. Every line in a C program should end with a semicolon. 2. In C language lowercase letters are significant.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
CPT: Chars/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to look at how C processes characters 4. Character.
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
CCSA 221 Programming in C INPUT AND OUTPUT OPERATIONS IN C – PART 1 1.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
Chapter 1 slides1 What is C? A high-level language that is extremely useful for engineering computations. A computer language that has endured for almost.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
CSCE 206 Structured Programming in C
Character Processing How characters can be treated as small integers?
Chapter 2 - Introduction to C Programming
Chapter 18 I/O in C.
Introduction to C CSE 2031 Fall /3/ :33 AM.
Chapter 2 - Introduction to C Programming
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Functions I Creating a programming with small logical units of code.
CSCE 206 Lab Structured Programming in C
Chapter 2 - Introduction to C Programming
Functions, Part 1 of 3 Topics Using Predefined Functions
Program Breakdown, Variables, Types, Control Flow, and Input/Output
Chapter 2 - Introduction to C Programming
Your questions from last session
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
Character Processing How characters can be treated as small integers?
Functions, Part 1 of 3 Topics Using Predefined Functions
Chapter 2 - Introduction to C Programming
Department of Statistics St. Joseph’s College (Autonomous)
CSCE 206 Lab Structured Programming in C
Functions, Part 1 of 3 Topics Using Predefined Functions
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Introduction to C Programming
Functions I Creating a programming with small logical units of code.
Presentation transcript:

1 Review of Class on Oct 12

2 Outline of Chapter 4  How to write a function?  Function Prototypes  Function Invocation  Function Definition  The return Statement  More about function  Program Correctness: The assert() Macro  Function Declarations from the Compiler’s Viewpoint  Invocation and Call-by-Value  Developing a Large Problem

3 How to write a function? #include void prn_message(void); int main(void) { prn_message(); prn_message(); printf(“Back to main function\n”); return 0; } void prn_message(void) { printf(“A message for you: “); printf(“A message for you: “); printf(“Have a nice day!\n”); printf(“Have a nice day!\n”); return; return;}  How to give the compiler information about the function? Function prototype:  How to pass control to the function? Function Invocation  How to specify the function? Function Definition  How to get the control back? return statement

4 How to write a function? Preprocessing directives function prototype of fucntion1 function prototype of fucntion2 int main(void) { Body of function definition } Header of function1 definition { Body of function definition } Header of function2 definition { Body of function definition } function invocations

5 How to write a function?  Example: write a code to computer the minimum value of three integers.  Define a function min2 which compute the minimum value of two integers.  Define a function min3 which calls function min2 to compute the minimum value of three integers.

6 #include Preprocessing directives Function prototype of min2 Function prototype of min3 int main(void) { ……. …… Call function min3 …… } Header of min2 definition { Body of function definition } Header of min3 definition { Body of function definition (Call function min2) } int min2(int a, int b); int min3(int a, int b, int c); int main(void) { printf("min of 11,23,24 is %d\n", min3(11,23,24)); return 0; } int min2(int a, int b) { if (a<b) return a; else return b; } int min3(int a, int b, int c) { int mofab = min2(a,b); return min2(mofab, c); }

7 Outline of Chapter 4  How to write a function?  Function Invocation  Function Definition  The return Statement  Function Prototypes  More about function  Program Correctness: The assert() Macro  Function Declarations from the Compiler’s Viewpoint  Invocation and Call-by-Value  Developing a Large Program

8 Program Correctness: The assert() Macro  C provides the assert() macro in assert.h to guarantee certain conditions  assert(exp) ensures the value of exp is true if exp is false, the system prints out a message and abort the program.

9 Function Declarations from the Compiler’s Viewpoint Preprocessing directives function prototype of fucntion1 function prototype of fucntion2 int main(void) { Body of function definition } Header of function1 definition { Body of function definition } Header of function2 definition { Body of function definition } Preprocessing directives Header of function1 definition { Body of function definition } Header of function2 definition { Body of function definition } int main(void) { Body of function definition } A good program style is to give either the function definition or the function prototype or both before a function is used

10 Invocation and Call-by-Value  Function Invocation fun_name(exp1, exp2);  All arguments are passed call-by-value Each argument is evaluated, and its value is used locally in place of the corresponding formal parameter. If a variable is passed to a function, the stored value of that variable in the calling environment is not changed.

11 #include int min2(int a, int b); int min3(int a, int b, int c); min.h #include “min.h” int main(void) { printf("%d\n", min3(1,3,4) ); return 0; } main.c int min2(int a, int b) { if (a<b) return a; else return b; } int min3(int a, int b, int c) { int mofab = min2(a,b); return min2(mofab, c); } min.c f.h: function prototypes f1.c, f2.c, f3.c  function definitions  in each.c file #include “f.h”  gcc f1.c f2.c f3.c gcc main.c min.c Developing a Large Program

12 End of Chapter 4: Function Read

13 Chapter 5 Character Processing

14 Outline  The Data Type Char  Input and output characters  getchar()  putchar()  end-of-file signal: EOF

15 The Data Type char  How character is stored in a machine?  Each character is stored in one byte according to a specific encoding.  Examples of character codes: ASCII oA table of the ASCII code in Appendix E EBCDIC

16 The Data Type char Examples:  How character ‘a’ is stored in ASCII code?  How many bits in one byte? 8  Appendix E  A table of the ASCII code bit 7bit 6bit 5bit 4bit 3bit 2bit 1bit

17 The Data Type char  When a character is stored in a byte,  the contents of that byte can be thought of as either a character or as a small integer.

18 The Data Type char  Example:  char c=‘a’; c is character ‘a’ c is a small integer represented in binary form , oWhat is the decimal value of this small integer? 97 bit 7bit 6bit 5bit 4bit 3bit 2bit 1bit

19 The Data Type char When a character is stored in a byte, it can be thought of as either  a character or  as a small integer. ‘a’: the decimal value of is 97 #include int main() { printf("%c\n", 'a'); printf("%d\n", 'a'); printf("%d\n", 97); printf("%c\n", 97); if ('a'==97) printf("'a'==97\n"); else printf("'a'!=97\n"); } %a.out a 97 a 'a'==97

20 The Data Type char When a character is stored in a byte, it can be thought of as either  a character or  as a small integer. ‘A’: the decimal value of ‘A’ is 65 #include int main() { printf("%c\n", ‘A'); printf("%d\n", ‘A'); printf("%d\n", 65); printf("%c\n", 65); if (‘A'==65) printf("‘A'==65\n"); else printf("‘A'!=65\n"); } %a.out A 65 A ‘A'==65

21 The Data Type char When a character is stored in a byte, it can be thought of as either  a character or  as a small integer. #include int main() { printf("printf1: %c\n", '\n'); printf("printf2: %d\n", '\n'); printf("printf3: %c\n", 10); printf("printf4: %d\n", 10); } % a.out printf1: printf2: 10 printf3: printf4: 10 % ‘\n’: the decimal value of ‘A’ is 10

22 The Data Type char In ASCII, Character ‘1’ == int 1? #include int main() { printf("%c \n", '1'); printf("%d \n", '1'); if (1=='1') printf("1=='1' \n"); else printf("1!='1' \n"); } % a.out !='1'

23 The Data Type char In ASCII,  Code for 0 through 9 are contiguous, letters A through Z are contiguous, and letters a through z are contiguous.  the difference between a capital letter and the corresponding lowercase letter is 32. #include int main() { printf("%c %d\n", 'a', 'a'); printf("%c %d\n", 'A', 'A'); printf("%d \n", 'a'-'A'); printf("%c \n", 'A'+32); printf("%c \n", 'a'-32); printf("%c \n", 'a'+3); printf("%c \n", 'A'+3); printf("%c \n", 'Y'+32); } % a.out a 97 A a A d D y %

24 The Data Type char  Summary  each character is stored in one byte  it can be interpreted as either a character or as a small integer.  In ASCII, Character ‘1’ != int 1 Code for o0 through 9 are contiguous, oletters A through Z are contiguous, and oletters a through z are contiguous. the difference between a capital letter and the corresponding lowercase letter is 32.

25 Outline  The Data Type Char  Input and output characters  getchar()  putchar()  EOF: end-of-file

26 Input and output characters — getchar() and putchar()  Read and Write characters Use format %c in printf and scanf functions  printf(“%c”, ‘A’);  char c; scanf(“%c”, &c);

27 Input and output characters — getchar() and putchar()  putchar()  defined in stdio.h  To write a character to the screen #include int main(){ putchar('H'); putchar('e'); putchar('l'); putchar('o'); putchar('\n'); } % a.out Hello %

28 Input and output characters — getchar() and putchar()  getchar(): reads a character from the keyboard  defined in stdio.h  c = getchar() getchar() gets a character from the keyboard and assigns it to the variable c. #include int main(){ char c; c=getchar(); putchar(c); putchar('\n'); } % a.out d d % scanf(“%c”, &c);

29 Outline  The Data Type Char  Input and output characters  getchar()  putchar()  EOF: end-of-file

30 end-of-file: EOF  Example:  Get inputs from the keyboard and print out each input twice on the screen #include int main(void){ char c; while (1){ c = getchar(); putchar(c); } return 0; } How many times the while loop is executed? Infinite loop How to indicate the input is ended? while(the input is not ended){

31 end-of-file: EOF  How to indicate the input is ended?  User can use end-of-file signal to indicate the end of the input. How to represent end-of-file signal in C code? How to enter an end-of-file signal?

32 end-of-file: EOF  EOF in C code:  end-of-file signal: EOF  the value of EOF is system-dependent int value -1 is often used oLibrary stdio.h provides the following line:  #define EOF (-1) different systems can have different values  In order to avoid confusion, whatever is used to signal the end of a file, it cannot be a value that represents a character.

33 end-of-file: EOF  Example:  Get inputs from the keyboard and print out each input twice on the screen;  User enters EOF to end the input. #include int main(void){ int c; while ((c=getchar())!=EOF){ putchar(c); } return 0; } The value of EOF cannot be a value that represents a character.  In order to store the value of both characters (char) and EOF, c’s type should be int instead of char. #include int main(void){ char c; while (1){ c = getchar(); putchar(c); } return 0; }

34 end-of-file: EOF  How to input an end-of-file signal (EOF)?  Unix: carriage return followed by a control-d  MS-DOS: control-z

35 end-of-file: EOF  Summary  end-of-file: EOF  the value of EOF is system-dependent, provided in stdio.h  different systems can have different values Whatever is used to signal the end of a file, it cannot be a value that represents a character.  Input EOF in Unix: carriage return followed by a control-d

36 Chapter 5  Summary  each character is stored in one byte, and it can be interpreted as either a character or as a small integer.  In ASCII, Code for 0 through 9 are contiguous, letters A through Z are contiguous, and letters a through z are contiguous. the difference between a capital letter and the corresponding lowercase letter is 32.  putchar() To write a character to the screen  c = getchar() getchar() gets a character from the keyboard and assigns it to the variable c.  EOF

37 End of Chapter 5: Character Processing Read 5.1 – 5.8