Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.

Slides:



Advertisements
Similar presentations
Lecture 2 Introduction to C Programming
Advertisements

Introduction to C Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 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
TDBA66, VT-03 Lecture - Ch. 21 A complete C-program Display Fig. 2.1 and comment on different things such as Preprocessor directives Header files Identifiers.
1 CS 201 Introduction to C (1) Debzani Deb. 2 Outline Overview of C General form of a C program C Language Elements.
Software Development Method & C Language Elements H&K Chapter 1-2
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
Software Development Method. Assignments Due – Homework 0, Warmup Reading – Chapter 2 –
C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX.
Topic 2 – Introduction to the C Programming Language.
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
1 ICS103 Programming in C Lecture 2: Introduction to C (1)
Introduction to C Programming
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.
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
COMPUTER SCIENCE I C++ INTRODUCTION
Chapter 2 Getting Started in C Programming
Chapter 2 : Overview of C By Suraya Alias. /*The classic HelloWorld */ #include int main(void) { printf(“Hello World!!"); return 0; }
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
(1 - 2) C Language Elements H&K Chapter 2 Instructor - Andrew S. O’Fallon CptS 121 (August 28, 2015) Washington State University.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
Week 1 Algorithmization and Programming Languages.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
ELE118 Introduction to Programming
CC112 Structured Programming Lecture 2 1 Arab Academy for Science and Technology and Maritime Transport College of Engineering and Technology Computer.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
Overview of C. C—a high-level programming language developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories. We will discuss: –the elements of a.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
CISC105 – General Computer Science Class 2 – 6/7/2006.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
A.Abhari CPS1251 Topic 2: C Overview C Language Elements Variable Declaration and Data Types Statement Execution C Program Layout Formatting Output Interactive.
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
C Language Elements Preprocessor Directives # (sign for preprocessor directive commands) #include Standard header file (.h) Library.
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.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
Lecture2.
Numbers in ‘C’ Two general categories: Integers Floats
Chapter Topics The Basics of a C++ Program Data Types
Chapter 6 JavaScript: Introduction to Scripting
Chapter 2 Introduction to C++ Programming
CSC201: Computer Programming
Chapter 2 - Introduction to C Programming
Chapter 2, Part I Introduction to C Programming
Basic Elements of C++.
Revision Lecture
ICS103 Programming in C Lecture 3: Introduction to C (2)
Chapter 2 - Introduction to C Programming
Basic Elements of C++ Chapter 2.
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.
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
INPUT & OUTPUT scanf & printf.
Introduction to C++ Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
Lecture3.
WEEK-2.
Chapter 2 - Introduction to C Programming
Presentation transcript:

Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of Technology

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.2-2 C Language Elements -- Preprocessor Directives Preprocessor directive –a C program line beginning with # that provides an instruction to the preprocessor Preprocessor –a system program that modifies a C program prior to its compilation Library –a collection of useful functions and symbols that may be accessed by a program

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.2-3 Figure 2.1 C Language Elements in Miles-to-Kilometers Conversion Program

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.2-4 C Language Elements -- Preprocessor Directives (Cont’) Constant macro –a name that is replaced by a particular constant value before the program is sent to the compiler Comment –text beginning with /* and ending with */ that provides supplementary information but is ignored by the preprocessor and compiler

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.2-5 C Language Elements -- Syntax for Preprocessor Directives (Cont’)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.2-6 C Language Elements -- Syntax for Preprocessor Directives (Cont’)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.2-7 C Language Elements -- Function main Declarations –the part of a program that tells the compiler the names of memory cells in a program Statements –program lines that are converted to machine language instructions and executed by the computer

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.2-8 C Language Elements -- Function main

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.2-9 C Language Elements -- Function main

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.2-10 C Language Elements -- Reserved Words Reserved word –a word that has special meaning in C

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.2-11 C Language Elements -- Standard Identifiers Standard identifier –a word having special meaning but one that a programmer may redefine (but redefinition is not recommended!)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.2-12 TABLE 2.1 Reserved Words in Fig. 2.1

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.2-13 C Language Elements -- User-Defined Identifiers User-defined identifiers –used to name memory cells that will hold data and program results and to name operations that we define Valid identifiers: –An identifier must consist only of letters, digits, and underscores. –An identifier cannot begin with a digit. –A C reserved word cannot be used as an identifier. –An identifier defined in a C standard library should not be redefined.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.2-14 TABLE 2.2 Invalid Identifiers

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.2-15 TABLE 2.3 Reserved Words and Identifiers in Fig. 2.1

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.2-16 C Language Elements -- Uppercase and Lowercase Letters Uppercase and lowercase letters are viewed by the compiler as different identifiers. Adopting a consistent pattern in the way you use uppercase and lowercase letters is helpful to the readers of your programs. One style that has been widely adopted in industry uses all uppercase letters in the names of constant macros.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.2-17 C Language Elements -- Program Style Most programs will be examined or studied by someone other than the original programmers. –pick a meaningful name –placing the underscore character (_) between words –choose identifiers to convey your meaning –avoid excessively long names –do not choose names that are similar to each other

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.2-18 Variable Declarations and Data Types -- Variable Declarations Variable –a name associated with a memory cell whose value can change Variable declarations –statements that communicate to the compiler the names of variables in the program and the kind of information stored in each variable

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.2-19 Variable Declarations and Data Types -- Variable Declarations (Cont’)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.2-20 Variable Declarations and Data Types -- Data Types Data type –a set of values and operations that can be performed on those values Data Type “int” –-10500, 435, +15, Data Type “double” –1.23 × 105 is equivalent to –1.23e5 or 1.23E5 Read the letter e or E as “times 10 to the power”. Data Type “char” –'A' 'z' '2' '9' '*' ':' '"' ' '

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.2-21 Variable Declarations and Data Types -- Data Types (Cont’)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.2-22 Figure 2.2 Memory(a) Before and (b) After Execution of a Program

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.2-23 Executable Statements -- Assignment Statements assignment statement –an instruction that stores a value or a computational result in a variable –E.g. kms = KMS_PER_MILE * miles; Read = as “becomes,” “gets,” or “takes the value of ” rather than “equals” because it is not equivalent to the equal sign of mathematics.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.2-24 Figure 2.3 Effect of kms = KMS_PER_MILE * miles;

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.2-25 Executable Statements -- Assignment Statements

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.2-26 Figure 2.4 Effect of sum = sum + item;

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.2-27 Executable Statements -- Input/Output Operations and Functions input operation –an instruction that copies data from an input device into memory output operation –an instruction that displays information stored in memory input/output function –a C function that performs an input or output operation function call –Calling or activating a function

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.2-28 Executable Statements -- The printf Function function argument –enclosed in parentheses following the function name; provides information needed by the function format string –in a call to printf, a string of characters enclosed in quotes ("), which specifies the form of the output line print list –in a call to printf, the variables or expressions whose values are displayed

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.2-29 Executable Statements -- The printf Function (Cont’)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.2-30 Executable Statements -- The printf Function (Cont’) placeholder a symbol beginning with % in a format string that indicates where to display the ouput value newline escape sequence the character sequence \n, which is used in a format string to terminate an output line

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.2-31 Executable Statements -- The printf Function (Cont’)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.2-32 Executable Statements -- The printf Function (Cont’)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.2-33 Executable Statements -- The printf Function (Cont’)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.2-34 Executable Statements -- The printf Function (Cont’) cursor –a moving place marker that indicates the next position on the screen where information will be displayed –When executing a printf function call, the cursor is advanced to the start of the next line on the screen if the \n escape sequence is encountered in the format string. –printf("Here is the first line\n"); printf("\nand this is the second.\n"); –Here is the first line and this is the second.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.2-35 Executable Statements -- The printf Function (Cont’) prompt (prompting message) –a message displayed to indicate what data to enter and in what form –printf("Enter the distance in miles> "); scanf("%lf", &miles);

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.2-36 Executable Statements -- The scanf Function Fig 2.5 Effect of scanf("%lf", &miles); The name of each variable that is to be given a value is preceded by the ampersand character (&), the C address-of operator. & tells the scanf function where to find each variable into which it is to store a new value. Once or is pressed, the data are processed exactly as typed. Fig 2.6 scanf("%c%c%c", &letter_1, &letter_2, &letter_3);

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.2-37 Figure 2.5 Effect of scanf("%lf", &miles);

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.2-38 Figure 2.6 Scanning Data Line Bob

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.2-39 Executable Statements -- The scanf Function (Cont’) format placeholder –reflect the type of the variable in which the data will be stored –one input character is used for a %c –For %lf or %d, the program first skips any spaces and then scans characters until it reaches a character that cannot be part of the number. –If you type more data characters on a line than are needed by the current call to scanf, the extra characters will be processed by the next call to scanf.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.2-40 Executable Statements -- The scanf Function

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.2-41 Executable Statements -- The return Statement

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.2-42 General Form of a C Program

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.2-43 General Form of a C Program (Cont’) C treats most line breaks like spaces so a C statement can extend over more than one line. You should not split a statement that extends over more than one line in the middle of an identifier, a reserved word, a constant, or a format string.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.2-44 General Form of a C Program -- Program Style Spaces in Programs The consistent and careful use of blank spaces can improve the readability and the style of a program. Indent the body of the main function and insert blank lines between sections of the program.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.2-45 General Form of a C Program -- Comments in Programs Use comments to describe the purpose of the program, the use of identifiers, and the purpose of each program step. program documentation –information (comments) that enhances the readability of a program E.g. double miles, /* input - distance in miles */ kms; /* output - distance in kilometers */

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.2-46 General Form of a C Program -- Comments in Programs

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.2-47 General Form of a C Program -- Program Style Using Comments Each program should begin with a header section that consists of a series of comments specifying –the programmer’s name –the date of the current version –a brief description of what the program does E.g. /* * Programmer:William Bell Date completed: May 9, 2003 * Instructor:Janet Smith Class: CIS61 * * Calculates and displays the area and circumference of a * circle */

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.2-48 General Form of a C Program -- Program Style Using Comments Before you implement each step in the initial algorithm, you should write a comment that summarizes the purpose of the algorithm step. Comment should describe what the step does rather than simply restate the step in English. E.g. /* Convert the distance to kilometers. */ kms = KMS_PER_MILE * miles;