The scanf Function The scanf function reads input from the standard input device into one or more variables Example: scanf(“%lf”, &miles); Reads a real.

Slides:



Advertisements
Similar presentations
Lecture 2 Introduction to C Programming
Advertisements

Introduction to C Programming
 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
C Language Elements (II) H&K Chapter 2 Instructor – Gokcen Cilingir Cpt S 121 (June 22, 2011) Washington State University.
1 ICS103 Programming in C Lecture 4: Data Types, Operators & Expressions.
1 ICS103 Programming in C Lecture 3: Introduction to C (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.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
CS201 – Expressions & I/O. Precedence What is the value of * 4 ? 20 or 14 ? Without parentheses, there are a set of rules to fall back on. Unary.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
1 CS 201 Introduction to C (2) Debzani Deb. 2 Overview C Arithmetic Expressions Formatting Numbers in Program Output Interactive Mode, Batch Mode, and.
Introduction to C Programming
Ping Zhang 10/08/2010.  You can get data from the user (input) and display information to the user (output).  However, you must include the library.
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
Chapter 2 Getting Started in C Programming
A First Book of ANSI C Fourth Edition
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
Chapter 2 : Overview of C By Suraya Alias. /*The classic HelloWorld */ #include int main(void) { printf(“Hello World!!"); return 0; }
Chapter 2 Overview of C++ Lecture Notes Prepared By: Blaise W. Liffick, PhD Department of Computer Science Millersville University Millersville, PA
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Overview of C++ Problem Solving, Abstraction, and Design using.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
Chapter 2 Overview of C Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
Pengantar C/C++. 2 Outline  C overview  C language elements  Variable declarations and data types  Executable statements  General form of a C program.
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
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Khalid Rasheed Shaikh Computer Programming Theory 1.
Chapter-4 Managing input and Output operation.  Reading, processing and writing of data are three essential functions of a computer program.  Most programs.
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.
Principles of Programming Chapter 4: Basic C Operators  In this chapter, you will learn about:  Arithmetic operators  Unary operators  Binary operators.
1 ELE118 lecture 3 Dr. Mehmet Demirer Dr. Seniha Esen Yuksel.
© 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.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
 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.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
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.
SCP1103 Basic C Programming SEM1 2010/2011 Arithmetic Expressions Week 5.
Input and Output: I/O Finish reading chapters 1 and 2 of the text
ICS103 Programming in C Lecture 4: Data Types, Operators & Expressions
Chapter 2 - Introduction to C Programming
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
ICS103 Programming in C Lecture 3: Introduction to C (2)
Chapter 2 - Introduction to C Programming
Introduction to C Programming
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
INPUT & OUTPUT scanf & printf.
Introduction to C++ Programming
Chapter 2 - Introduction to C Programming
Introduction to C Programming
Chapter 2 - Introduction to C Programming
Lecture3.
CS150 Introduction to Computer Science 1
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Introduction to Java Applications
Arithmetic Operations
Chapter 2 - Introduction to C Programming
Chapter 2: Overview of C++
DATA TYPES There are four basic data types associated with variables:
Introduction to C Programming
Presentation transcript:

The scanf Function The scanf function reads input from the standard input device into one or more variables Example: scanf(“%lf”, &miles); Reads a real number that (by default) the user has typed in into the variable miles Each variable must be preceded by the ampersand (&) address-of operator

The scanf Function We can read values into multiple variables with a single scanf as long as we have correpsonding format strings for each variable Example: scanf(“%c%c%c”, &letter_1, &letter_2, &letter_3); The user must press the return or enter key to cause the value(s) to be read

The scanf Function For numeric values (associated with a %lf or %d placeholder) blank spaces are skipped For character values (associated with a %c placeholder) blank spaces are not skipped (since a space is a character!)

The scanf Function Example: scanf(“%c%c%c”,&x,&y,&z); input: cat result: input: c a t Example: scanf(“%d%d”,&n1,&n2); input: 12 3

The return Statement The statement- return(0); - transfers control from your program to the OS It should be the last statement in your program. (Why?) 0 is the result of the execution of function main and signifies that the program completed without error

General Form of a C Program At this point, we have learned enough of the elements of a C program to be able to combine them into a working program Figure 1.9 shows an example of a working C program. Note the order in which the elements occur in this program

General Form of a C Program

Program Style It is important to develop a consistent approach to programming style For starters, follow the style illustrated in the text Use spaces and indentation consistently Use meaningful comments A header section given info. on the programmer and program should be used

Arithmetic Expressions Arithmetic expressions are formed of operators and operands The C arithmetic operators are: +, -, *, /, and % (modulo or remainder) The operands in an arithmetic expression can be either numeric literals (numbers) or numeric variables

Arithmetic Expressions The division operator (/) can be applied to integers values, real values, or a combination When applied to reals or mixed reals and integers, the result is a real value When applied to two integers, the result is the integral part of the result Example: 7/2 is 3 7.0/2 is 3.5

Arithmetic Expressions The modulo operator can be applied to integers and it gives the remainder of integer division Example: 7 % 2 is 1 Example: 8 % 2 is 0 Division by zero will cause a run-time error (execution of the program will be halted at that point)

Arithmetic Expressions C allows mixed-type expressions in which the two operands are of different types C defines numerical data types int and double (among others) If both operands are of type int, the result is also an int Otherwise, the result is a double

Arithmetic Expressions An expression may involve multiple arithmetic operators Operators may be either unary or binary Unary operators are unary + and - Examples: x = -y; p = +x * y;

Arithmetic Expressions What is the value of 5 + 7 * 2? How about 10 / 5 * 2 In order to know, we must examine how C evaluates expressions There are three rules C uses to evaluate expressions If there are parentheses, evaluate the subexpression within the parentheses first

Arithmetic Expressions Example 3 * (5 + 2) Parentheses can also be nested 2 * ( 5 * (2 + 1)) In this case, evaluate the most deeply nested subexpression first The second rule involves operator precedence We evaluate the operator which has highest precedence first C precedence: (unary) + and - have highest precedence; *, /, and % are next; (binary) + and - have the lowest precedence

Arithmetic Expressions If we have multiple operators with the same precedence we use associativity rules Unary + and - are evaluated using right associativity (from right to left) The binary operators are evaluated using left associativity (from left to right) In order to better understand evaluation of complex expressions, use evaluation trees

Formatting Numbers in Output Unless otherwise instructed, C displays numeric values in printf statements using a default notation To change the default notation for integers, add a numeric field width between the ‘%’ and ‘d’ printf(“%3d %4d\n”, 11, 1); /* Note the use of a numeric literal in the print list! */

Formatting Numbers in Output Real numbers can be formatted by specifying both a field width and the number of decimal places to be displayed Printf(“%5.2f %3.2f %4.1f\n”, 3.14,3.14,3.14); Note that if the field width isn’t wide enough for the numeric value, it will be expanded This doesn’t work with character variables!

Interactive and Batch Modes So far, we have seen programs which employ interactive mode In this mode, input comes from the keyboard and output goes to the monitor In batch mode, input and output are to and from files To work in this mode, we need to redirect input, output or both

Interactive and Batch Modes In DOS and UNIX, redirect input by following the name of your program with a ‘<‘ and the name of the file you want to read data from (one line for each scanf) prog1 < myinput.dat Redirect output by following the name of your program with a ‘>‘ and the name of the file you want to write data to prog1 > myoutput.dat

Types of Errors There are three categories of programming errors Syntax errors are discovered by the compiler Run-time errors cause execution of the program to be halted Logic errors cause the program to give incorrect results