Chapter 2, Part I Introduction to C Programming

Similar documents
Fundamentals of Programming. Lecture 3: Introduction to C Programming

Programming for Engineers Introduction to C

Fundamentals of Programming Session 4

C: How to Program. Week /Mar/05

Chapter 2 - Introduction to C Programming

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

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

Intro to Computer Programming (ICP) Rab Nawaz Jadoon

Full file at C How to Program, 6/e Multiple Choice Test Bank

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

Introduction to C++ Programming Pearson Education, Inc. All rights reserved.

Fundamental of Programming (C)

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

IT 374 C# and Applications/ IT695 C# Data Structures

Chapter 1 & 2 Introduction to C Language

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Chapter 2, Part III Arithmetic Operators and Decision Making

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 1

Introduction to C Programming

Some Computer Preliminaries

3. Except for strings, double quotes, identifiers, and keywords, C++ ignores all white space.

Data types, variables, constants

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

Chapter 1 Introduction to Computers and C++ Programming

Introduction to Programming

Introduction to Java Applications; Input/Output and Operators

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Lecture 2: C Programming Basic

4. Inputting data or messages to a function is called passing data to the function.

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

Introduction to C++ Programming. Adhi Harmoko S, M.Komp

Introduction to C# Applications

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

LESSON 4. The DATA TYPE char

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Tutorial No. 2 - Solution (Overview of C)

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

AN OVERVIEW OF C. CSE 130: Introduction to Programming in C Stony Brook University

Should you know scanf and printf?

CS 241 Computer Programming. Introduction. Teacher Assistant. Hadeel Al-Ateeq

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

Computer Programming C++ Classes and Objects 6 th Lecture

Introduction to C CMSC 104 Spring 2014, Section 02, Lecture 6 Jason Tang

CC112 Structured Programming

2 nd Week Lecture Notes

C Programming for Engineers Functions

Lecture 2 Tao Wang 1

Introduction to C# Applications Pearson Education, Inc. All rights reserved.

1/25/2018. ECE 220: Computer Systems & Programming. Write Output Using printf. Use Backslash to Include Special ASCII Characters

Your First C++ Program. September 1, 2010

Basic Elements of C. Staff Incharge: S.Sasirekha

Instructor. Mehmet Zeki COSKUN Assistant Professor at the Geodesy & Photogrammetry, Civil Eng. (212)

Introduction to Computing Lecture 03: Basic input / output operations

Programming - 1. Computer Science Department 011COMP-3 لغة البرمجة 1 لطالب كلية الحاسب اآللي ونظم المعلومات 011 عال- 3

Chapter 2: Introduction to C++

2.8. Decision Making: Equality and Relational Operators

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Chapter 2 Author Notes

Input/Output Week 5:Lesson 16.1

A A B U n i v e r s i t y

Rule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead.

Chapter 2. Lexical Elements & Operators

ANSI C Programming Simple Programs

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

C Introduction Lesson Outline

CS102: Variables and Expressions

C Language, Token, Keywords, Constant, variable

بسم اهلل الرمحن الرحيم

4. Structure of a C++ program

Programming I Laboratory - lesson 01

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual

Introduction to OOP Using Java Pearson Education, Inc. All rights reserved.

Objectives. In this chapter, you will:

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

The C Programming Language. (with material from Dr. Bin Ren, William & Mary Computer Science)

Full file at

PYTHON. Values and Variables

C Fundamentals & Formatted Input/Output. adopted from KNK C Programming : A Modern Approach

B.V. Patel Institute of BMC & IT, UTU 2014

GCC C++: Comment Text

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

PHP and MySQL for Dynamic Web Sites. Intro Ed Crowley

1.1 Introduction to C Language. Department of CSE

Lab # 02. Basic Elements of C++ _ Part1

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

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

1 EEE1008 C Programming

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

Course Outline. Introduction to java

Fundamentals of Programming. Lecture 11: C Characters and Strings

Crayon (.cry) Language Reference Manual. Naman Agrawal (na2603) Vaidehi Dalmia (vd2302) Ganesh Ravichandran (gr2483) David Smart (ds3361)

Creating a C++ Program

Transcription:

Chapter 2, Part I Introduction to C Programming C How to Program, 8/e, GE 2016 Pearson Education, Ltd. All rights reserved. 1

2016 Pearson Education, Ltd. All rights reserved. 2

2016 Pearson Education, Ltd. All rights reserved. 3

2.1 Introduction The C language facilitates a structured and disciplined approach to computer program design. In this chapter we introduce C programming and present several examples that illustrate many important features of C. In Chapters 3 and 4 we present an introduction to structured programming in C. 2016 Pearson Education, Ltd. All rights reserved. 4

Printing a Line of Text We begin by considering a simple C program. Our first example prints a line of text (Fig. 2.1). 2016 Pearson Education, Ltd. All rights reserved. 5

2016 Pearson Education, Ltd. All rights reserved. 6

Printing a Line of Text (Cont.) // Fig. 2.1: fig02_01.c // A first program in C begin with //, indicating that these two lines are comments. Comments document programs and improve program readability. Comments do not cause the computer to perform any action when the program is run. 2016 Pearson Education, Ltd. All rights reserved. 7

Printing a Line of Text (Cont.) Comments are ignored by the C compiler and do not cause any machine-language object code to be generated. Comments also help other people read and understand your program. 2016 Pearson Education, Ltd. All rights reserved. 8

Printing a Line of Text (Cont.) You can also use /* */ multi-line comments in which everything from /* on the first line to */ at the end of the line is a comment. We prefer // comments because they re shorter and they eliminate the common programming errors that occur with /* */ comments, especially when the closing */ is omitted. #include Preprocessor Directive #include <stdio.h> is a directive to the C preprocessor. 2016 Pearson Education, Ltd. All rights reserved. 9

Printing a Line of Text (Cont.) Lines beginning with # are processed by the preprocessor before compilation. Line 3 tells the preprocessor to include the contents of the standard input/output header (<stdio.h>) in the program. This header contains information used by the compiler when compiling calls to standard input/output library functions such as printf. Blank Lines and White Space You use blank lines, space characters and tab characters (i.e., tabs ) to make programs easier to read. Together, these characters are known as white space. White-space characters are normally ignored by the compiler. 2016 Pearson Education, Ltd. All rights reserved. 10

Printing a Line of Text (Cont.) The main Function int main( void ) is a part of every C program. The parentheses after main indicate that main is a program building block called a function. 2016 Pearson Education, Ltd. All rights reserved. 11

Printing a Line of Text (Cont.) C programs contain one or more functions, one of which must be main. Every program in C begins executing at the function main. The keyword int to the left of main indicates that main returns an integer (whole number) value. We ll explain what it means for a function to return a value when we demonstrate how to create your own functions in Chapter 5. 2016 Pearson Education, Ltd. All rights reserved. 12

Printing a Line of Text (Cont.) For now, simply include the keyword int to the left of main in each of your programs. Functions also can receive information when they re called upon to execute. The void in parentheses here means that main does not receive any information. 2016 Pearson Education, Ltd. All rights reserved. 13

Printing a Line of Text (Cont.) A left brace, {, begins the body of every function A corresponding right brace ends each function This pair of braces and the portion of the program between the braces is called a block. An Output Statement printf( "Welcome to C!\n" ); instructs the computer to perform an action, namely to print on the screen the string of characters marked by the quotation marks. A string is sometimes called a character string, a message or a literal. 2016 Pearson Education, Ltd. All rights reserved. 14

Printing a Line of Text (Cont.) The entire line, including the printf function (the f stands for formatted ), its argument within the parentheses and the semicolon (;), is called a statement. Every statement must end with a semicolon (also known as the statement terminator). When the preceding printf statement is executed, it prints the message Welcome to C! on the screen. The characters normally print exactly as they appear between the double quotes in the printf statement. Escape Sequences Notice that the characters \n were not printed on the screen. The backslash (\) is called an escape character. It indicates that printf is supposed to do something out of the ordinary. 2016 Pearson Education, Ltd. All rights reserved. 15

Printing a Line of Text (Cont.) When encountering a backslash in a string, the compiler looks ahead at the next character and combines it with the backslash to form an escape sequence. The escape sequence \n means newline. When a newline appears in the string output by a printf, the newline causes the cursor to position to the beginning of the next line on the screen. Some common escape sequences are listed in Fig. 2.2. 2016 Pearson Education, Ltd. All rights reserved. 16

2016 Pearson Education, Ltd. All rights reserved. 17

Printing a Line of Text (Cont.) Because the backslash has special meaning in a string, i.e., the compiler recognizes it as an escape character, we use a double backslash (\\) to place a single backslash in a string. Printing a double quote also presents a problem because double quotes mark the boundaries of a string such quotes are not printed. By using the escape sequence \" in a string to be output by printf, we indicate that printf should display a double quote. The right brace, }, indicates that the end of main has been reached. 2016 Pearson Education, Ltd. All rights reserved. 18

Printing a Line of Text (Cont.) We said that printf causes the computer to perform an action. As any program executes, it performs a variety of actions and makes decisions. Chapter 3 discusses this action/decision model of programming in depth. 2016 Pearson Education, Ltd. All rights reserved. 19

Printing a Line of Text (Cont.) Using Multiple printfs The printf function can print Welcome to C! several different ways. For example, the program of Fig. 2.3 produces the same output as the program of Fig. 2.1. This works because each printf resumes printing where the previous printf stopped printing. The first printf prints Welcome followed by a space and the second printf begins printing on the same line immediately following the space. 2016 Pearson Education, Ltd. All rights reserved. 20

2016 Pearson Education, Ltd. All rights reserved. 21

Printing a Line of Text (Cont.) One printf can print several lines by using additional newline characters as in Fig. 2.4. Each time the \n (newline) escape sequence is encountered, output continues at the beginning of the next line. 2016 Pearson Education, Ltd. All rights reserved. 22

2016 Pearson Education, Ltd. All rights reserved. 23