Fundamentals of Programming Session 4

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

Chapter 2, Part I Introduction to C Programming

Programming for Engineers Introduction to C

C: How to Program. Week /Mar/05

Chapter 2 - Introduction to C Programming

Fundamentals of Programming Session 8

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

Introduction to Programming

Chapter 1 & 2 Introduction to C Language

Fundamentals of Programming Session 12

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

Intro to Computer Programming (ICP) Rab Nawaz Jadoon

Fundamentals of Programming Session 14

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

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

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

Fundamentals of Programming Session 23

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

Fundamentals of Programming Session 25

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

Fundamental of Programming (C)

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

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

Fundamentals of Programming Session 7

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

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

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

Introduction to C Programming

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

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

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

Data types, variables, constants

Fundamentals of Programming Session 24

LESSON 4. The DATA TYPE char

CC112 Structured Programming

Fundamentals of Programming Session 15

Chapter 1 Introduction to Computers and C++ Programming

ANSI C Programming Simple Programs

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

Introduction to Java Applications; Input/Output and Operators

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

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

1 EEE1008 C Programming

Fundamentals of Programming. Lecture 11: C Characters and Strings

ET156 Introduction to C Programming

Fundamentals of Programming Session 20

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

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

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

printf( Please enter another number: ); scanf( %d, &num2);

Data Types and Variables in C language

Some Computer Preliminaries

Fundamentals of Programming Session 9

Full file at

Fundamentals of Programming Session 13

Introduction to Computing Lecture 03: Basic input / output operations

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

Lecture 2 Tao Wang 1

A Fast Review of C Essentials Part I

2. Numbers In, Numbers Out

Introduction to C# Applications

Input/Output Week 5:Lesson 16.1

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..

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

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

2. Numbers In, Numbers Out

Should you know scanf and printf?

Fundamentals of Programming Session 19

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

Chapter 2 Basic Elements of C++

Basic Elements of C. Staff Incharge: S.Sasirekha

Programming Languages & Translators. XML Document Manipulation Language (XDML) Language Reference Manual

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

Fundamentals of Programming Session 20

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

CSc Introduction to Computing

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

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

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

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

Chapter 2 Author Notes

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

Introduction to Java Applications

Introduction to Python Programming

CS102: Variables and Expressions

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

2.5 Another Application: Adding Integers

Preview from Notesale.co.uk Page 6 of 52

Visual C# Instructor s Manual Table of Contents

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

Chapter 2. Lexical Elements & Operators

Full file at

Lecture 2: C Programming Basic

2.8. Decision Making: Equality and Relational Operators

6.096 Introduction to C++

BITG 1233: Introduction to C++

Tutorial No. 2 - Solution (Overview of C)

1. What type of error produces incorrect results but does not prevent the program from running? a. syntax b. logic c. grammatical d.

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

Objectives. In this chapter, you will:

Transcription:

Fundamentals of Programming Session 4 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2011 These slides are created using Deitel s slides, ( 1992-2010 by Pearson Education, Inc). Sharif University of Technology

Outlines A Simple C Program: Printing a Line of Text Another Simple C Program: Adding Two Integers 2

3 A Simple C Program: Printing a Line of Text

A Simple C Program: Printing a Line of Text Lines beginning with # are processed by the preprocessor before the program is compiled. 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. Line 6 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. 4

A Simple C Program: Printing a Line of Text 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. 5

A Simple C Program: Printing a Line of Text 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. 6

7 A Simple C Program: Printing a Line of Text

A Simple C Program: Printing a Line of Text A left brace, {, begins the body of every function (line 7). A corresponding right brace ends each function (line 11). This pair of braces and the portion of the program between the braces is called a block. Line 8 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. 8 A string is sometimes called a character string, a message or a literal.

9 A Simple C Program: Printing a Line of Text The entire line, including printf, 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. 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.

A Simple C Program: Printing a Line of Text 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. 10

11 A Simple C Program: Printing a Line of Text

A Simple C Program: Printing a Line of Text Line 10 return 0; /* indicate that program ended successfully */ is included at the end of every main function. The keyword return is one of several means we ll use to exit a function. When the return statement is used at the end of main as shown here, the value 0 indicates that the program has terminated successfully. 12 The right brace, }, (line 12) indicates that the end of main has been reached.

13 A Simple C Program: Printing a Line of Text

14 A Simple C Program: Printing a Line of Text

A Simple C Program: Printing a Line of Text 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 (line 8) prints Welcome followed by a space and the second printf (line 9) begins printing on the same line immediately following the space. 15

16 A Simple C Program: Printing a Line of Text

A Simple C Program: Printing a Line of Text 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. 17

18 A Simple C Program: Printing a Line of Text

Another Simple C Program: Adding Two Integers Our next program (fig. 3.8) uses the Standard Library function scanf to obtain two integers typed by a user at the keyboard, computes the sum of these values and prints the result using printf. [In the input/output dialog of Fig. 2.8, we emphasize the numbers input by the user in bold.] 19

20 Another Simple C Program: Adding Two Integers

21 Another Simple C Program: Adding Two Integers

Another Simple C Program: Adding Two Integers Lines 8 10 int integer1; /* first number to be input by user */ int integer2; /* second number to be input by user */ int sum; /* variable in which sum will be stored */ are definitions. The names integer1, integer2 and sum are the names of variables. A variable is a location in memory where a value can be stored for use by a program. These definitions specify that the variables integer1, integer2 and sum are of type int, which means that these variables will hold integer values, i.e., whole numbers such as 7, 11, 0, 31914 and the like. 22

Another Simple C Program: Adding Two Integers 23 All variables must be defined with a name and a data type immediately after the left brace that begins the body of main before they can be used in a program. The preceding definitions could have been combined into a single definition statement as follows: int integer1, integer2, sum; but that would have made it difficult to describe the variables in corresponding comments as we did in lines 8 10. A variable name in C is any valid identifier. An identifier is a series of characters consisting of letters, digits and underscores (_) that does not begin with a digit.

Another Simple C Program: Adding Two Integers An identifier can be of any length, but only the first 31 characters are required to be recognized by C compilers according to the C standard. C is case sensitive uppercase and lowercase letters are different in C, so a1 and A1 are different identifiers. 24

25 Another Simple C Program: Adding Two Integers

26 Another Simple C Program: Adding Two Integers

27 Another Simple C Program: Adding Two Integers

Another Simple C Program: Adding Two Integers Definitions must be placed after the left brace of a function and before any executable statements. A syntax error is caused when the compiler cannot recognize a statement. The compiler normally issues an error message to help you locate and fix the incorrect statement. Syntax errors are violations of the language. Syntax errors are also called compile errors, or compile-time errors. 28

29 Another Simple C Program: Adding Two Integers

Another Simple C Program: Adding Two Integers Line 12 printf( "Enter first integer\n" ); /* prompt */ prints the literal Enter first integer on the screen and positions the cursor to the beginning of the next line. This message is called a prompt because it tells the user to take a specific action. The next statement scanf( "%d", &integer1 ); /* read an integer */ uses scanf to obtain a value from the user. The scanf function reads from the standard input, which is usually the keyboard. 30

Another Simple C Program: Adding Two Integers 31 This scanf has two arguments, "%d" and &integer1. The first argument, the format control string, indicates the type of data that should be input by the user. The %d conversion specifier indicates that the data should be an integer (the letter d stands for decimal integer ). The % in this context is treated by scanf (and printf as we ll see) as a special character that begins a conversion specifier. The second argument of scanf begins with an ampersand (&) called the address operator in C followed by the variable name.

Another Simple C Program: Adding Two Integers The ampersand, when combined with the variable name, tells scanf the location (or address) in memory at which the variable integer1 is stored. The computer then stores the value for integer1 at that location. The use of ampersand (&) is often confusing to novice programmers or to people who have programmed in other languages that do not require this notation. For now, just remember to precede each variable in every call to scanf with an ampersand. 32