1 Structured Programming in C Welcome to CPSC 206.

Slides:



Advertisements
Similar presentations
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Advertisements

1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
1 Structured Programming in C Welcome to CPSC 206.
1 Lexical Elements Chapter 2 in ABC. 2 Tokens Token = word / symbol, does not contain white spaces. Tokens in C are either: –keywords –identifiers –constants.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Chapter 2: Basic Elements of C++
Guide To UNIX Using Linux Third Edition
JavaScript, Third Edition
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
Chapter 2: Basic Elements of C++
1 Review of Chapter 6: The Fundamental Data Types.
1 Lecture 3  Lexical elements  Some operators:  /, %, =, +=, ++, --  precedence and associativity  #define  Readings: Chapter 2 Section 1 to 10.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Basic Elements of C++ Chapter 2.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Computer Science 210 Computer Organization Introduction to C.
A First Book of ANSI C Fourth Edition
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
UniMAP Sem2-08/09 DKT121: Fundamental of Computer Programming1 Introduction to C – Part 2.
2440: 211 Interactive Web Programming Expressions & Operators.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Sales person receive RM200/week plus 9% of their gross sales for that week. Write an algorithms to calculate the sales person’s earning from the input.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Input, Output, and Processing
UniMAP Sem1-07/08EKT120: Computer Programming1 Week2.
Lecture #5 Introduction to C++
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
1 C++ Syntax and Semantics, and the Program Development Process.
Object Oriented Programming with C++ Diploma in Computer System Design.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Lexical Elements, Operators, and the C Cystem. C overview recap functions –structured programming –return value is typed –arguments(parameters) pointers.
CHAPTER 2 C++ SYNTAX & SEMANTICS #include using namespace std; int main() { cout
Minimal standard C program int main(void) { return 0 ; }
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
1 Lexical Elements, Operators, and the C System. 2 Outline Characters and Lexical Elements Syntax Rules Comments Keywords Identifiers Constants String.
Silberschatz and Galvin  C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University College of Education.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
UNIMAP Sem2-07/08EKT120: Computer Programming1 Week 2 – Introduction to Programming.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
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.
L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
Chapter 2: Basic Elements of C++
Chapter Topics The Basics of a C++ Program Data Types
Computer Science 210 Computer Organization
BASIC ELEMENTS OF A COMPUTER PROGRAM
Tokens in C Keywords Identifiers Constants
Basic Elements of C++.
C Language VIVA Questions with Answers
ICS103 Programming in C Lecture 3: Introduction to C (2)
Introduction to C++.
Java Programming: From Problem Analysis to Program Design, 4e
Basic Elements of C++ Chapter 2.
Computer Science 210 Computer Organization
Lexical Elements, Operators, and the C Cystem
Basics of ‘C’.
Lexical Elements, Operators, and the C Cystem
Chapter 2: Basic Elements of Java
A First Book of ANSI C Fourth Edition
Lectures on Numerical Methods
Lexical Elements & Operators
C Programming Lecture-3 Keywords, Datatypes, Constants & Variables
Presentation transcript:

1 Structured Programming in C Welcome to CPSC 206

2 Lecture Information

3 Lecture Topics: 0. Introduction to Computer Science 1. Overview of CCh 1, 2 2. Flow of control and functionsCh 3, 4 3. Character processing & fundamental data typesCh 5, 6 4. File I/O Ch Pointers, Arrays, and StringsCh 8, 9, Structures, and linked listsCh 12 Features of C: 7. Enumeration type and storage classesCh 7, 8 8. RecursionCh 11

4 Review of Class on Sept. 16, Thursday

5 Chapter 2: Lexical Elements, Operators, and the C System

6 Lexical Elements, Operators, and the C System  C is a language  Alphabet  syntax  What is C program? A C program is a sequence of characters Lexical Elements, Operators, and the C System How a computer understands this sequence of characters?

7 Introduction  How to check a C program is correct? 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. Lexical Elements, Operators, and the C System

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

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

10 Lexical Elements  Keywords  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  Keywords cannot be redefined or used in other contexts. Lexical Elements, Operators, and the C System Lexical Elements

11 Lexical Elements  Identifiers  What is identifier? The names of variables, functions, labels and other user-defined items are called identifier.  Special identifier Keywords, names of functions in C library, main  Rules: 1.composed of letters, digits, and underscore _. 2.The first character must be a letter or underscore. 3.case-sensitive 4.would not be defined as the special identifiers: Lexical Elements, Operators, and the C System Lexical Elements

12 Lexical Element  Constants  Integer constants: 0, 17 Decimal integer: 17 Octal integer: 017 Hexadecimal integer: 0x17  Floating constants: double, float, long double  Character constants: (enclosed between single quotes) Lexical Elements Lexical Elements, Operators, and the C System

13 Lexical Elements  String Constants  String constant is a sequence of characters enclosed in a pair of double quote marks.  String constants are differently form character constants.  Special characters: \”, \\  You mustn't split a string constant across lines  Two string constants that are separated only by white space are concatenated by the compiler into a single string. Lexical Elements Lexical Elements, Operators, and the C System

14 Lexical Elements  Operators and Punctuator  Precedence and Associativity determine precisely how expressions are evaluated. when they will be evaluated  Precedence of operators indicates when they will be evaluated.  Associativity “left to right”: Operations are performed from left to right oExamples: +,-,/,% “right to left”: Operations are performed from right to left oExamples: ++(prefix), --(prefix) Lexical Elements Lexical Elements, Operators, and the C System

15 Class on Sept 21

16 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 Lexical Elements, Operators, and the C System

17 Operators and Punctuators — Outline  Examples of Operators and Punctuators  Precedence and Associativity of Operators  Increment and Decrement Operators  Assignment Operators Lexical Elements Lexical Elements, Operators, and the C System

18 Operators and Punctuators — Increment ++ and Decrement Operators --  Semantics  Precedence and Associativity  Rules Lexical Elements Lexical Elements, Operators, and the C System

19 Operators and Punctuators — Increment ++ and Decrement Operators --  Increment ++i, i++  Each causes the stored value of i in memory to be incremented by 1.  Each of the expressions ++i and i++ has a value. ++i othe stored value of i is incremented first othe expression takes as its value the new stored value of i i++ othe expression takes as its value the current stored value of i othe stored value of i is incremented Lexical Elements Lexical Elements, Operators, and the C System

20 Operators and Punctuators — Increment ++ and Decrement Operators -- #include int main(void) { int i, j, a, b; i=0; j=0; a = ++i; b = j++; printf("a=%d, b=%d\n",a,b); return 0; } % gcc id.c % a.out a=1, b=0 id.c Lexical Elements Lexical Elements, Operators, and the C System

21 Operators and Punctuators — Increment ++ and Decrement Operators --  ++ and +  ++ Cause the value of a variable in memory to be changed  + Does not change the value of a variable. Lexical Elements Lexical Elements, Operators, and the C System

22 Operators and Punctuators — Increment ++ and Decrement Operators --  Decrement Operator i-- and --i  The value of i is decremented by 1.  Each expression has a value. --i othe stored value of i is decremented by 1 othe expression takes as its value the new stored valued of i i-- othe expression takes as its value the current stored valued of i othe stored value of i is decremented by 1 Lexical Elements Lexical Elements, Operators, and the C System

23 Operators and Punctuators — Increment ++ and Decrement Operators --  Semantics  Precedence and Associativity  Rules Lexical Elements Lexical Elements, Operators, and the C System

24 Operators and Punctuators — Increment ++ and Decrement Operators --  Precedence and Associativity Associativity  ++ (postfix) -- (postfix) Left to right  +(unary) –(unary) ++(prefix) --(prefix) R ight to left  * / % Left to right  + -Left to right Lexical Elements Lexical Elements, Operators, and the C System

25 Operators and Punctuators — Increment ++ and Decrement Operators --  Precedence and Associativity  +(unary) –(unary) ++(prefix) --(prefix) Right to left #include int main(void) { int a=2; int result; result = - --a; printf("a=2, - --a = %d\n",result); return 0; } % gcc id2.c % a.out a=2, - --a = -1 id2.c Lexical Elements Lexical Elements, Operators, and the C System Question1: What is the value of a after the operation --a ? Question2: What is the value of the expression --a?

26 Operators and Punctuators — Increment ++ and Decrement Operators --  Examples #include int main(void) { int a=1, b=2, c=3, d=4; int result1, result2; result1 = ++ a * b - c --; result2 = b * ++ d; printf("++ a * b – c -- = %d\n",result1); printf(“ b * ++ d = %d\n",result2); return 0; } ++ a * b – c (postfix) -- (postfix) Left to right +(unary) –(unary) ++(prefix) --(prefix)right to left * / % left to right + -left to right Precedence and Associativity ) ( ) ( ) ( ++ a * b – 3 2 * b – 3 4 – 3 1 ) ( ) ( ) ( id2.c Lexical Elements Lexical Elements, Operators, and the C System

27 Operators and Punctuators — Increment ++ and Decrement Operators --  Examples #include int main(void) { int a=1, b=2, c=3, d=4; int result1, result2; result1 = ++ a*b - c --; result2 = b * ++ d; printf("++ a * b – c -- = %d\n",result1); printf(“ b * ++ d = %d\n",result2); return 0; } b * ++ d ++ (postfix) -- (postfix) Left to right +(unary) –(unary) ++(prefix) --(prefix)right to left * / % left to right + -left to right Precedence and Associativity 7 - (-2) * (-10) 17 ) ( ) ( ) ( id2.c Lexical Elements Lexical Elements, Operators, and the C System

28 Operators and Punctuators — Increment ++ and Decrement Operators --  Examples #include int main(void) { int a=1, b=2, c=3, d=4; int result1, result2; result1 = ++ a*b - c --; result2 = b* ++ d; printf("++ a * b – c -- = %d\n",result1); printf(“ b* ++ d = %d\n",result2); return 0; } % gcc id1.c % a.out ++ a * b - c -- = b* ++ d = 17 id2.c Lexical Elements Lexical Elements, Operators, and the C System

29 Operators and Punctuators — Increment ++ and Decrement Operators --  Semantics  Precedence and Associativity  Rules Lexical Elements Lexical Elements, Operators, and the C System

30 Operators and Punctuators — Increment ++ and Decrement Operators --  Rules  Applied to variables but not to constants or ordinary expressions Lexical Elements Lexical Elements, Operators, and the C System

31 Operators and Punctuators — Increment ++ and Decrement Operators --  Examples: #include int main(void) { int a, result1, result2; a = 1; result1 = ++1; result2 = -- -a; return 0; } % gcc id3.c id3.c: In function `main': id3.c:6: error: invalid lvalue in increment id3.c:7: error: invalid lvalue in decrement id3.c Lexical Elements Lexical Elements, Operators, and the C System

32 Operators and Punctuators — Increment ++ and Decrement Operators --  Summary  ++ ++i: the stored value of i is incremented; the expression takes as its value the new stored valued of i i++: the expression takes as its value the current stored valued of i; the stored value of i is incremented by 1.  -- --i: the stored value of i is decremented by 1; the expression takes as its value the new stored valued of i i--: the expression takes as its value the current stored valued of i; the stored value of i is decremented by 1.  Applied to variables but not to constants or ordinary expression Lexical Elements Lexical Elements, Operators, and the C System

33 Operators and Punctuators — Outline  Examples of Operators and Punctuators  Precedence and Associativity of Operators  Increment and Decrement Operators  Assignment Operators Lexical Elements Lexical Elements, Operators, and the C System

34 Operators and Punctuators — Assignment Operators  Example: An assignment expression with =  Format: variable = right_side Two operands ovariable oright_side: an expression Lexical Elements Lexical Elements, Operators, and the C System

35 Operators and Punctuators — Assignment Operators  Example: An assignment expression with =  Format: variable = right_side  Results: The value of right_side is assigned to variable Assignment expression variable = right_size has a value. oThe value of right_side is the value of the assignment expression.  Example: the value of c is 9 Assignment expression: a=2+(b=c+1) Lexical Elements Lexical Elements, Operators, and the C System

36 Operators and Punctuators — Assignment Operators #include int main(void) { int a, b, c; int a1, b1, c1; b = 2; c = 3; a = b + c; printf("b = %d, c = %d, a = %d \n",b, c, a); a1=(b1 = 2) + (c1 = 3); printf("b1 = %d, c1 = %d, a1 = %d \n",b1, c1, a1); return 0; } % gcc ass1.c % a.out b = 2, c = 3, a = 5 b1 = 2, c1 = 3, a1 = 5 Lexical Elements Lexical Elements, Operators, and the C System

37 Operators and Punctuators — Assignment Operators  Assignment operators  =  op=: +=, -=, *=, / =, %=, ……  Semantics:  variable op= expression equivalent to variable = variable op (expression)  Example: var*= expr  var=var * expr a *= 3  a = a * 3 Lexical Elements Lexical Elements, Operators, and the C System

38 Operators and Punctuators — Assignment Operators  Assignment operators  Precedence: all the assignment operators have the same precedence Lower than all the other operators which have been introduced (such as + - )  Associativity: right to left Lexical Elements Lexical Elements, Operators, and the C System

39 Operators and Punctuators — Assignment Operators #include int main(void) { int a, b, c; a = b = c = 0; printf("b = %d, c = %d, a = %d \n", b, c, a); return 0; } % gcc ass2.c % a.out b = 0, c = 0, a = 0 Lexical Elements Lexical Elements, Operators, and the C System

40 Operators and Punctuators — Assignment Operators #include int main(void) { int i=1, j=2, k=3, m=4; i += j + k; printf(" j = %d, k = %d, i += j+k = %d \n",j, k, i); printf(" m = %d, k = %d, ",m, k); j *= k = m + 5; printf("j *= k = m + 5 = %d \n",j); printf("k = %d \n",k); return 0; } % gcc ass3.c % a.out j = 2, k = 3, i += j+k = 6 m = 4, k = 3, j *= k = m + 5 = 18 k = 9 Lexical Elements Lexical Elements, Operators, and the C System

41 Operators and Punctuators — Assignment Operators  Summary  Assignment operators Precedence: they have the same precedence oLower than all the other operators which have been introduced (such as + - ) Associativity: right to left  variable op= expr  variable = variable op (expr) The value of the expression is the value of the expr Lexical Elements Lexical Elements, Operators, and the C System

42 Operators and Punctuators — Summary  Precedence and Associativity of Operators  Increment and Decrement Operators  i++, i++  i--, --i  Assignment Operators  Variable op= expression  Variable = variable op (expression) The value of the expression is the value of the expression Lexical Elements Lexical Elements, Operators, and the C System

43 Lexical Elements  Summary  Comments  Keywords  Identifiers  Constants  String Constants  Operators and Punctuators Lexical Elements, Operators, and the C System

44 Lexical Elements Comment  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. Lexical Elements, Operators, and the C System Lexical Elements

45 Lexical Elements  Keywords  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  Keywords cannot be redefined or used in other contexts. Lexical Elements, Operators, and the C System Lexical Elements

46 Lexical Elements  Identifiers  What is identifier? The names of variables, functions, labels and other user-defined items are called identifier.  Special identifier Keywords, names of functions in C library, main  Rules: 1.composed of letters, digits, and underscore _. 2.The first character must be a letter or underscore. 3.case-sensitive 4.would not be defined as the special identifiers: Lexical Elements, Operators, and the C System Lexical Elements

47 Lexical Element  Constants  Integer constants: 0, 17 Decimal integer: 17 Octal integer: 017 Hexadecimal integer: 0x17 An integer may be too large to be stored in a machine word.  Floating constants: double, float, long double  Character constants: (enclosed between single quotes) Lexical Elements Lexical Elements, Operators, and the C System

48 Lexical Elements  String Constants  String constant is a sequence of characters enclosed in a pair of double quote marks.  String constants are differently form character constants.  Special characters: \”, \\  You mustn't split a string constant across lines  Two string constants that are separated only by white space are concatenated by the compiler into a single string. Lexical Elements Lexical Elements, Operators, and the C System

49 Lexical Elements  Operators and Punctuators  Precedence and Associativity of Operators  Increment and Decrement Operators i++, i++ i--, --i  Assignment Operators Variable op= expression o  Variable = variable op (expression) oThe value of the expression is the value of the expression Lexical Elements Lexical Elements, Operators, and the C System

50 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 Lexical Elements, Operators, and the C System

51 An Example: Computing Powers of 2 — Outline  Program pow_of_2  Dissection of the pow_of_2 Program Lexical Elements Comments Keywords Identifiers Constants String Constants Operators and Punctuators

52 Purpose: prints on a line some powers of 2. An Example: Computing Powers of 2 — power_of_2.c /* Some powers of 2 are printed. */ #include int main(void) { int e = 0, power_of_two = 1; while (++e <= 10) printf("%5d", power_of_two *= 2); printf("\n"); return 0; } pow_of_2.c Comments Keywords Identifiers Constants String Constants Operators and Punctuators How many times the body of the loop is executed? power_of_two = power_of_two * 2

53 An Example: Computing Powers of 2 — power_of_2.c /* Some powers of 2 are printed. */ #include int main(void) { int exponent = 0, power_of_two = 1; while (++exponent <= 10) printf("%5d", power_of_two *= 2); printf("\n"); return 0; } % gcc pow_of_2.c % a.out % Purpose: prints on a line some powers of 2. pow_of_2.c

54 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 Lexical Elements, Operators, and the C System

55 The C System C is a small language  The core language is small  Non-essential functionality, such as math functions or file handling, is provided by a standardized set of library routines. The standard library contains many useful functions that add considerable power and flexibility to the c System. What is standard library? How to use a function in standard library?

56 The C System — The Standard Library  What is Standard library? (Contd.)  A collection of header files and library files.  Header file: The names and characteristics of functions are included into computer files called header file.  Library file: The actual implementation of functions are separated into a library file. The library contains compiled code that is unreadable to humans.

57 The C System — The Standard Library  Examples of header files:  : For computing common mathematical functions  : Provides the core input and output capabilities of the C language.  : For performing a variety of operations, including conversion, pseudo-random numbers, memory allocation, process control, environment, signaling, searching, and sorting.  : For manipulating several kinds of strings. Check Appendix A for Details

58 The C System — The Standard Library  How to use a function in the standard library? /*The traditional first program in honor of Dennis Ritchie who invented C at Bell Labs in 1972.*/ #include int main(void) { printf(“Hello, world!\n”); return 0; }

59 The C System — The Standard Library  How to use a function in the standard library?  The programmer needs to provide the function prototype. Including appropriate header files.  Do we need to locate the function in the library file? No. The system knows where to find the code that corresponds to functions from the standard library.

60 The C System — The Standard Library  Steps to use a function in the standard library.  Find the header file which contains the prototype of the function.  Use #include preprocessing directive to include the appropriate file.

61 The C System — An example: prn_rand Program  Purpose of prn_rand program:  Use rand() to generate some randomly distributed integers.  Where is prototype of rand()?  stdlib.h

62 The C System — An example: prn_rand Program /*Printing random numbers. */ #include int main(void) { int i, n; printf("\n%s\n%s", "Some randomly distributed integers will be printed", "How many do you want to see? "); scanf("%d", &n); for(i=0;i<n;++i){ if (i%6==0) printf("\n"); printf("%9d", rand()); } printf("\n"); return 0; } Hearder files are included i=0; while (i++<n){ …… }

63 The C System — An example: prn_rand Program /*Printing random numbers. */ #include int main(void) { int i, n; printf("\n%s\n%s", "Some randomly distributed integers will be printed", "How many do you want to see? "); scanf("%d", &n); for(i=0;i<n;++i){ if (i%6==0) printf("\n"); printf("%9d", rand()); } printf("\n"); return 0; } % gcc prn_rand.c % a.out Some randomly distributed integers will be printed How many do you want to see? %

64 End of Chapter 2  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 Lexical Elements, Operators, and the C System Read Chapter 2 Sections

65 End of Chapter 2