1 / 142

Welcome to CPSC 206

Welcome to CPSC 206. Structured Programming in C. Lecture Information. http://people.cs.tamu.edu/ychen/Teaching/CPSC206. 0. Introduction to Computer Science 1. Overview of C Ch 1, 2. Lecture Topics:. Features of C:. 2. Flow of control and functions Ch 3, 4

elata
Download Presentation

Welcome to CPSC 206

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Welcome to CPSC 206 Structured Programming in C

  2. Lecture Information http://people.cs.tamu.edu/ychen/Teaching/CPSC206

  3. 0. Introduction to Computer Science 1. Overview of C Ch 1, 2 Lecture Topics: Features of C: 2. Flow of control and functions Ch 3, 4 3. Character processing & fundamental data types Ch 5, 6 4. File I/O Ch 13 5. Pointers, Arrays, and Strings Ch 8, 9, 10 6. Structures, and linked lists Ch 12 7. Enumeration type and storage classes Ch 7, 8 8. Recursion Ch 11

  4. Review of Class on Sept. 14, Tuesday

  5. A Depth Program: Algorithm and code • Dissection of the depth program Declaration main Function printf function • Basics of C Programming General form of a program Declaration Variable Expression Data Type and Operation An Overview of C— Chapter 1 • A brief history of C • Features of C • Get Ready to Program • A First Program • The Basics of C Programming • Variables, Expressions, and Assignments • Initialization • Preprocessing directives: #define and #include • Print and Read formatted data: printf() and scanf() • The while statement • Problem Solving: Computing Sums • Style, Programming Errors and System Considerations.

  6. A Depth Program: Algorithm and code • Dissection of the depth program Declaration main Function printf function • Basics of C Programming General form of a program Declaration Variable Expression Data Type and Operation An Overview of C— Chapter 1 • A brief history of C • Features of C • Get Ready to Program • A First Program • The Basics of C Programming • Variables, Expressions, and Assignments • Initialization • Preprocessing directives: #define and #include • Print and Read formatted data: printf() and scanf() • The while statement • Problem Solving: Computing Sums • Style, Programming Errors and System Considerations. The general form of a program preprocessing directives int main () { declarations statements }

  7. A Depth Program: Algorithm and code • Dissection of the depth program Declaration main Function printf function • Basics of C Programming General form of a program Declaration Variable Expression Data Type and Operation An Overview of C— Chapter 1 • A brief history of C • Features of C • Get Ready to Program • A First Program • The Basics of C Programming • Variables, Expressions, and Assignments • Initialization • Preprocessing directives: #define and #include • Print and Read formatted data: printf() and scanf() • The while statement • Problem Solving: Computing Sums • Style, Programming Errors and System Considerations. • Declarations • Purpose: Tell the compiler what kind of data can be stored in each of the variables. • The compiler can set aside the appropriate amount of memory to hold the data. • Format: • Data_Type Variable1, Variable2;

  8. A Depth Program: Algorithm and code • Dissection of the depth program Declaration main Function printf function • Basics of C Programming General form of a program Declaration Variable Expression Data Type and Operation An Overview of C— Chapter 1 • A brief history of C • Features of C • Get Ready to Program • A First Program • The Basics of C Programming • Variables, Expressions, and Assignments • Initialization • Preprocessing directives: #define and #include • Print and Read formatted data: printf() and scanf() • The while statement • Problem Solving: Computing Sums • Style, Programming Errors and System Considerations. • Variables • A variable name can be a sequence of letters, digits, and underscores. • A variable name may not begin with a digit. • Certain keywords, also called reserved words, cannot be used as names of variables.

  9. A Depth Program: Algorithm and code • Dissection of the depth program Declaration main Function printf function • Basics of C Programming General form of a program Declaration Variable Expression Data Type and Operation An Overview of C— Chapter 1 • A brief history of C • Features of C • Get Ready to Program • A First Program • The Basics of C Programming • Variables, Expressions, and Assignments • Initialization • Preprocessing directives: #define and #include • Print and Read formatted data: printf() and scanf() • The while statement • Problem Solving: Computing Sums • Style, Programming Errors and System Considerations. • Expressions • How to use expressions? • on the right side of assignment • Arguments to functions • How to construct an expression? • Constants • The name of a variable • Meaningful combinations of operators with variables and constants

  10. A Depth Program: Algorithm and code • Dissection of the depth program Declaration main Function printf function • Basics of C Programming General form of a program Declaration Variable Expression Data Type and Operation An Overview of C— Chapter 1 • A brief history of C • Features of C • Get Ready to Program • A First Program • The Basics of C Programming • Variables, Expressions, and Assignments • Initialization • Preprocessing directives: #define and #include • Print and Read formatted data: printf() and scanf() • The while statement • Problem Solving: Computing Sums • Style, Programming Errors and System Considerations. • Integer: • Division a/b: an integer expression divided by another integer expression yields an integer value. • Any fractional part is discarded. • Modulus a%b: the remainder after a is divided by b. • If a or b is negative, the results of division and modulus are system-dependent • In a/b and a%b, the value of b cannot be zero.

  11. A Depth Program: Algorithm and code • Dissection of the depth program Declaration main Function printf function • Basics of C Programming General form of a program Declaration Variable Expression Data Type and Operation An Overview of C— Chapter 1 • A brief history of C • Features of C • Get Ready to Program • A First Program • The Basics of C Programming • Variables, Expressions, and Assignments • Initialization • Preprocessing directives: #define and #include • Print and Read formatted data: printf() and scanf() • The while statement • Problem Solving: Computing Sums • Style, Programming Errors and System Considerations. • Character: • Constants are written within single quotes • Example: • ‘A’ ‘1’

  12. A Depth Program: Algorithm and code • Dissection of the depth program Declaration main Function printf function • Basics of C Programming General form of a program Declaration Variable Expression Data Type and Operation An Overview of C— Chapter 1 • A brief history of C • Features of C • Get Ready to Program • A First Program • The Basics of C Programming • Variables, Expressions, and Assignments • Initialization • Preprocessing directives: #define and #include • Print and Read formatted data: printf() and scanf() • The while statement • Problem Solving: Computing Sums • Style, Programming Errors and System Considerations. • Floating types: • float: an F suffix • 1.22F • double: unsuffixed floating constant • 1.22 • long double: an L suffix • 1.22L

  13. Initialization • A variable can be initialized when it is declared. • Constants or constant expressions can be used to initialize a variable. • Declared variables can be used to initialize a variable. • a variable cannot be used before it has been declared. • A brief history of C • Features of C • Get Ready to Program • A First Program • The Basics of C Programming • Variables, Expressions, and Assignments • Initialization • Preprocessing directives: #define and #include • Print and Read formatted data: printf() and scanf() • The while statement • Problem Solving: Computing Sums • Style, Programming Errors and System Considerations. An Overview of C— Outline

  14. Preprocessing directives: • #include “filename” • The preprocessor replaces the line with a copy of the named file. • A brief history of C • Features of C • Get Ready to Program • A First Program • The Basics of C Programming • Variables, Expressions, and Assignments • Initialization • Preprocessing directives: #define and #include • Print and Read formatted data: printf() and scanf() • The while statement • Problem Solving: Computing Sums • Style, Programming Errors and System Considerations. An Overview of C— Outline

  15. Preprocessing directives: • #define A B • It affects only those lines in the file that come after it. • All occurrences (after this line)of the identifier A, except in quoted string, will be changed to B. • A brief history of C • Features of C • Get Ready to Program • A First Program • The Basics of C Programming • Variables, Expressions, and Assignments • Initialization • Preprocessing directives: #define and #include • Print and Read formatted data: printf() and scanf() • The while statement • Problem Solving: Computing Sums • Style, Programming Errors and System Considerations. An Overview of C— Outline

  16. Print and Read formatted data • Arguments: control_string and other_arguments • control_string contains formats, called conversion specifications, which are matched with other arguments. • A brief history of C • Features of C • Get Ready to Program • A First Program • The Basics of C Programming • Variables, Expressions, and Assignments • Initialization • Preprocessing directives: #define and #include • Print and Read formatted data: printf() and scanf() • The while statement • Problem Solving: Computing Sums • Style, Programming Errors and System Considerations. An Overview of C— Outline

  17. Print and Read formatted data • How to specify format using conversion specification? • %field_widthconversion_character • %field_width.precisionconversion_character • conversion_character: how the data is printed? • c: as a character • d: as a decimal integer • f: as a floating-point number • A brief history of C • Features of C • Get Ready to Program • A First Program • The Basics of C Programming • Variables, Expressions, and Assignments • Initialization • Preprocessing directives: #define and #include • Print and Read formatted data: printf() and scanf() • The while statement • Problem Solving: Computing Sums • Style, Programming Errors and System Considerations. An Overview of C— Outline

  18. Print and Read formatted data • Example: scanf(“%d”, &x); &: address operator • %d — how to interprete the input stream • the input characters typed at the keyboard is interpreted as a decimal integer • &x — where to store the value • the value of the decimal integer is stored at the address of x. • A brief history of C • Features of C • Get Ready to Program • A First Program • The Basics of C Programming • Variables, Expressions, and Assignments • Initialization • Preprocessing directives: #define and #include • Print and Read formatted data: printf() and scanf() • The while statement • Problem Solving: Computing Sums • Style, Programming Errors and System Considerations. An Overview of C— Outline

  19. The while statement while (expression) statement statement is executed as long as the value of expression is true • A brief history of C • Features of C • Get Ready to Program • A First Program • The Basics of C Programming • Variables, Expressions, and Assignments • Initialization • Preprocessing directives: #define and #include • Print and Read formatted data: printf() and scanf() • The while statement • Problem Solving: Computing Sums • Style, Programming Errors and System Considerations. An Overview of C— Outline

  20. Review of Class on Sept. 14, Tuesday End

  21. Chapter 2:Lexical Elements, Operators, and the C System

  22. Lexical Elements, Operators, and the C System Introduction • C is a language. •  C has an alphabet •  C has rules for putting together words and punctuation to make legal programs.

  23. Lexical Elements, Operators, and the C System Introduction • C is a language. • C has an alphabet • Lowercase letters: a b c …… z • Uppercase letters: A B C …… Z • Digits: 0 1 2 3 4 5 6 7 8 9 • Other characters: • + - * / = ( ) { } [ ] < > ‘ “ ! @ # $ % & _ | ^ ~ \ . , ; : ? • White space characters: • blank, newline, tab, etc.

  24. Lexical Elements, Operators, and the C System Introduction • C is a language. • C has rules for putting together words and punctuation to make legal programs. • These rules are called syntax

  25. Lexical Elements, Operators, and the C System Introduction • C is a language • Alphabet • syntax • What is C program? • A C program is a sequence of characters How a computer understands this sequence of characters?

  26. Lexical Elements, Operators, and the C System Introduction Machine code object code C code Preprocessor Compiler Loader errors How does a compiler know whether a C program is correct or not?

  27. Lexical Elements, Operators, and the C System Introduction • How to check a C program is correct? • Compiler • The program that checks the legality of a program. • How compiler checks the legality of a program? • The characters are collected by the compiler into syntactic units called tokens • The compiler checks whether the tokens can be formed into legal strings according to the syntax of C language.

  28. Lexical Elements, Operators, and the C System Introduction • In C language, there are six kinds of tokens: • Keywords • Identifiers • Constants • String constants • Operators • Punctuators

  29. Lexical Elements, Operators, and the C System Outline • An Example — Characters and Lexical Elements • Lexical Elements • Comments • Keywords • Identifiers • Constants • String Constants • Operators and Punctuators • An Example: Computing Powers of 2 • The C System

  30. Lexical Elements, Operators, and the C System Comment: be replaced with a single blank An Example — Characters and Lexical Elements sum.c /* Read in two scores and print their sum. */ #include <stdio.h> int main(void) { int score_1, score_2, sum; printf("Input two scores as integers: "); scanf("%d%d", &score_1, &score_2); sum = score_1 + score_2; printf("%d + %d = %d\n", score_1, score_2, sum); return 0; }

  31. Lexical Elements, Operators, and the C System File stdio.h is included An Example — Characters and Lexical Elements sum.c #include <stdio.h> int main(void) { int score_1, score_2, sum; printf("Input two scores as integers: "); scanf("%d%d", &score_1, &score_2); sum = score_1 + score_2; printf("%d + %d = %d\n", score_1, score_2, sum); return 0; }

  32. Lexical Elements, Operators, and the C System An Example — Characters and Lexical Elements sum.c int main(void) { int score_1, score_2, sum; printf("Input two scores as integers: "); scanf("%d%d", &score_1, &score_2); sum = score_1 + score_2; printf("%d + %d = %d\n", score_1, score_2, sum); return 0; } • Keyword: • int • void • Identifier: • main • score_1 • score_2 • sum • Operator: • Parentheses ( ) • Punctuators: • ; • , • { A Copy of stdio.h

  33. Lexical Elements, Operators, and the C System An Example — Character and Lexical Elements int main(void) { int score_1, score_2, sum; • How the compiler distinguishs tokens? • white space • For example: int score_1s • What if we write as follows? • intscore_1, score_2, sum; • The compiler considers intscore_1 as a single token.

  34. Lexical Elements, Operators, and the C System An Example — Characters and Lexical Elements % gcc sum1.c sum1.c: In function `main': sum1.c:8: error: `intscore_1' undeclared (first use in this function) sum1.c:8: error: (Each undeclared identifier is reported only once sum1.c:8: error: for each function it appears in.) sum1.c:8: error: `score_2' undeclared (first use in this function) sum1.c:8: error: `sum' undeclared (first use in this function) sum1.c:11: error: `score_1' undeclared (first use in this function) sum1.c /* Read in two scores and print their sum. */ #include <stdio.h> int main(void) { intscore_1, score_2, sum; printf("Input two scores as integers: "); scanf("%d%d", &score_1, &score_2); sum = score_1 + score_2; printf("%d + %d = %d\n", score_1, score_2, sum); return 0; }

  35. Lexical Elements, Operators, and the C System An Example — Characters and Lexical Elements • Identifier: • printf • scanf • Operator: • ( • ) • & • Punctuators: • , • ; • String Constant • A series of characters enclosed in “ ” sum.c int main(void) { int score_1, score_2, sum; printf("Input two scores as integers: "); scanf("%d%d", &score_1, &score_2); sum = score_1 + score_2; printf("%d + %d = %d\n", score_1, score_2, sum); return 0; } A Copy of stdio.h

  36. Lexical Elements, Operators, and the C System An Example — Characters and Lexical Elements • Identifier: • sum • score_1 • score_2 • Operator: • + • = • Punctuators: • ; • Constant • 0 sum.c int main(void) { int score_1, score_2, sum; printf("Input two scores as integers: "); scanf("%d%d", &score_1, &score_2); sum = score_1 + score_2; printf("%d + %d = %d\n", score_1, score_2, sum); return 0; } A Copy of stdio.h

  37. Lexical Elements, Operators, and the C System An Example — Characters and lexical Elements • Summary • Tokens: • Keyword: int void • Identifier:printf scanf sum • Operator: ( ) & • Punctuators: ;} • Constant: 0 • String Constant: “Input two scores as integers: “ • Separate two tokens: white space

  38. Lexical Elements, Operators, and the C System Outline • An Example — Characters and Lexical Elements • Lexical Elements • Comments • Keywords • Identifiers • Constants • String Constants • Operators and Punctuators • An Example: Computing Powers of 2 • The C System

  39. Lexical Elements, Operators, and the C System Comments Lexical Elements • What is comment? • Arbitrary strings of symbols placed between the delimiters /* and */. • Single line comment: // text • How compiler processes comments? • The compiler changes each comment into a single blank character. • Why comment? • Comments are used by the programmer as a documentation aid.

  40. Lexical Elements, Operators, and the C System Comments Lexical Elements • Rule1: Where can we place comments? • Anywhere? • No • Compiler replaces each comment by a single blank character  • Comments can be placed wherever a single blank character can appear. • Example x = 10+/* add the numbers */5;  x = 10+ 5;

  41. Lexical Elements, Operators, and the C System Comments Lexical Elements comment.c #include <stdio.h> int main(void) { int/* a comment*/x; x=10+5; printf("x=%d\n",x); return 0; } % gcc comment.c % a.out x=15 int x;

  42. Lexical Elements, Operators, and the C System Comments Lexical Elements • Question: Can a comment appear in the middle of a keyword or identifier? • Compiler will insert a single blank character into a keyword or identifier • Since a blank is used to separate two tokens, the keyword or identifier will be considered as two tokens. •  No

  43. Lexical Elements, Operators, and the C System int x, x y; Comments Lexical Elements #include <stdio.h> int main(void) { int x, x/* a comment */y; return 0; } comment1.c % gcc comment1.c comment1.c: In function `main': comment1.c:6: error: redeclaration of `x' comment1.c:6: error: `x' previously declared here comment1.c:6: error: parse error before "y"

  44. Lexical Elements, Operators, and the C System Comments Lexical Elements • Rule2: nested comments • Multi-Line comments may not be nested. Example: /* outer comment /* inner comment */ */ • A single-line (//) comment can be nested within a multi-line comment. Example: /* a //test of nested comments. */

  45. Lexical Elements, Operators, and the C System */ Comments Lexical Elements • An understanding of Rule2: Usually when a compiler encounters a pair /*, • it considers text between this /* and the first occurrence of */ after this /* as a comment; • this comment is replaced by a blank character. • /* outer comment /* inner comment */ */ • /* a //test of nested comments */

  46. Lexical Elements, Operators, and the C System Comments Lexical Elements #include <stdio.h> int main(void) { int x=1; /* This is a comment /* */ return x; } #include <stdio.h> int main(void) { int x; /* //*/ return 0; } comment3.c comment2.c

  47. Lexical Elements, Operators, and the C System Comment Lexical Elements Summary • What is comment? • Arbitrary strings of symbols placed between the delimiters /* and */. • Single line comment: // text • The compiler changes each comment into a single black character. • Rules: • Multi-line comments cannot be placed in the middle of a keyword or identifier. • Multi-line comments may not be nested.

  48. Lexical Elements, Operators, and the C System Outline Lexical Elements • An Example — Characters and Lexical Elements • Lexical Elements • Comments • Keywords • Identifiers • Constants • String Constants • Operators and Punctuators • An Example: Computing Powers of 2 • The C System

  49. Lexical Elements, Operators, and the C System Keywords Lexical Elements • What is Keywords? • Keywords are explicitly reserved words that have a strict meaning as individual tokens in C. • Examples of Keywords • Data Type: int, char, long, short C is small Compared to other major languages, C has only a small number of keywords.

  50. Lexical Elements, Operators, and the C System Keywords Lexical Elements • Rules: • Keywords cannot be redefined or used in other contexts. • Example: • keywords cannot be used as variable names.

More Related