C Language Elements (II) H&K Chapter 2 Instructor – Gokcen Cilingir Cpt S 121 (June 22, 2011) Washington State University.

Slides:



Advertisements
Similar presentations
Types and Arithmetic Operators
Advertisements

1 Chapter 2 Basic Elements of Fortran Programming.
Lecture 2 Introduction to C Programming
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
Software Development Method & C Language Elements H&K Chapter 1-2
1 ICS103 Programming in C Lecture 4: Data Types, Operators & Expressions.
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
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.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
1 CS 201 Introduction to C (2) Debzani Deb. 2 Overview C Arithmetic Expressions Formatting Numbers in Program Output Interactive Mode, Batch Mode, and.
Data types and variables
Introduction to C Programming
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
Chapter 2 Getting Started in C Programming
A First Book of ANSI C Fourth Edition
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Chapter 2 Overview of C Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
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.
A First Book of ANSI C Fourth Edition Chapter 3 Processing and Interactive Input.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
Input, Output, and Processing
Chapter 2: Using Data.
© 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.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Java Programming: From Problem Analysis to Program Design, 5e Chapter 2 Basic Elements of Java.
CPS120: Introduction to Computer Science Operations Lecture 9.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Chapter 7 Expressions and Assignment Statements. Outline Introduction Arithmetic Expressions Overloaded Operators Type Conversions Assignment Statements.
Chapter 4: Expressions Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 4 Expressions.
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
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.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Dr. Sajib Datta Jan 21,  Declare a variable ◦ int height; [note that no value is still assigned]  Assign a variable a value ◦ height =
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
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 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.
Arithmetic Instructions. Integer and Float Conversions.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output Samples.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
ICS103 Programming in C Lecture 4: Data Types, Operators & Expressions
Lecture 3: Operators, Expressions and Type Conversion
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)
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2 - Introduction to C Programming
Expressions Chapter 4 Copyright © 2008 W. W. Norton & Company.
Introduction to C++ Programming
Chapter 2: Basic Elements of Java
Lecture3.
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.
Data Types and Expressions
EECE.2160 ECE Application Programming
Chapter 2 Primitive Data Types and Operations
EECE.2160 ECE Application Programming
Introduction to C Programming
Data Types and Expressions
Data Types and Expressions
Presentation transcript:

C Language Elements (II) H&K Chapter 2 Instructor – Gokcen Cilingir Cpt S 121 (June 22, 2011) Washington State University

C. Hundhausen, A. O’Fallon, G. Cilingir2 Arithmetic Expressions Most programming problems require arithmetic expressions as part of solution Arithmetic expressions contain operands which can be anything that evaluates to a numeric value; constant numbers, variables, function calls (assuming function used returns a numeric value) Form: operand1 operator operand2 Examples: / ( 5 * 6 + 4%5 ) x + y * sin(2) //assuming you included x * average(1, 6, 7) /*assuming you have a user defined function named average*/ Type of result depends on operand types

C. Hundhausen, A. O’Fallon 3 Arithmetic Operators in C (1) OperatorRepresentationExample +Addition = = = Subtraction10 – 5 = – 10.0 = – 5.0 = 5.0 *Multiplication1 * 5 = * 10.0 = * 5.0 = 25.0

C. Hundhausen, A. O’Fallon 4 Arithmetic Operators in C (2) OperatorRepresentationExample /Division2 / 3 = / 4.0 = / 3.0 = %Modulus5 % 2 = 1 2 % 5 = 2 6 % 0 = undefined 6.0 % 3 = won’t compile

C. Hundhausen, A. O’Fallon5 Mixed-Type Expressions Types of operands in expression are different ◦ An integer value and a double value The result is always the more precise data type intdouble

C. Hundhausen, A. O’Fallon6 Mixed-Type Assignment Statements Assignment statements are evaluated from right-to-left Expression is first evaluated (what’s on right-hand-side) and then assigned to variable (what’s on left-hand-side) Examples: int op1_int = 5, op2_int = 42, result_int = 0, result_int = 0; double result_double = 0.0, op1_double = 5.5; result_int = op1_int + op1_double; /* mixed expression, integer assignment, result_int becomes 10 (truncation occurs) */ result_double = op1_int + op2_int; /* integer expression, double assignment, result_double = 47.0*/ result_double = op1_int + op1_double; /* mixed expression, double assignment, result_double = 10.5*/

C. Hundhausen, A. O’Fallon7 Type Conversions & Type Casts Changing one entity of a data type into another Two kinds exist: ◦ Implicit ◦ Explicit Implicit type conversion example: int num1 = 12; double num2; num2 = num1; /* num1 implicitly casted to type double, 12.0 */ Explicit type conversion example: double num1; num1 = ((double) 1 / 5); /* integer 1 explicitly casted to type double, 1.0 */

C. Hundhausen, A. O’Fallon8 Multiple Operator Expressions May contain unary and binary operators Unary operators consists of one operand Binary operators require two operands Example: y = -x + x * x / 10; /* -x applies the unary sign operator for negation */

C. Hundhausen, A. O’Fallon9 Operator Precedence (1) Operator Precedence ◦ How is x – y / z evaluated?  (x – y) / z ?  x – (y / z) ? ◦ Important to understand operator precedence rules:  Evaluation proceeds left to right  Sub-expressions in parentheses are evaluated first  In cases where no parentheses are used, *, /, and % take precedence over + and – ◦ So x – y / z is evaluated as x – (y / z), because / takes precedence over – ◦ Note: The unary operators + and – are used to indicate the sign of a number (e.g., +5, -3.0). They take precedence over all binary operators, and are evaluated right to left:  Example: * 4 would be evaluated as (-3) + (5 * 4) = 17. ◦ Recommendation: Liberally use parentheses to avoid confusion and unexpected results!

C. Hundhausen, A. O’Fallon10 Operator Precedence (2) Operator Precedence Example (H & K p.78)

C. Hundhausen, A. O’Fallon11 Formatting Numbers (1) C defines "default" output style for each data type ◦ No leading blanks for int and double ◦ double displayed with default number of digits to right of decimal point (how many?) You can override these defaults by specifying custom format strings to printf function int x; double y; x = 3; y = 2.17; printf("x is %3d. y is %5.1f.",x,y); Output: x is 3. y is 2.2.

C. Hundhausen, A. O’Fallon12 Formatting Numbers (2) Notes: ◦ For double output, format string is of form %n.mlf, where n is total width (number of columns) of formatted number, and m is the number of digits to the right of decimal point to display. ◦ It is possible to omit n. In that case, no leading spaces are printed. m can still specify the number of decimal places (e.g., %.2lf )

C. Hundhausen, A. O’Fallon13 Formatting Numbers (3) You try it: ◦ If the values of the variables a, b, and c are 504, , and , write a statement that will display the following line ( □ is used to denote a blank): □□ 504 □□□□□ □□□□ printf(“%5d%11.2lf%9.1lf”,a,b,c);

C. Hundhausen, A. O’Fallon14 Programming Errors (1) Rarely will you write a program that is free of errors You'll need to diagnose and correct three kinds of errors: ◦ Syntax errors (code violates syntax rules for a proper C program)  Detected at compile time  Your program will not execute unless they are corrected  Examples:  Missing semi-colon  Unmatched brace  Undeclared identifiers  Failure to close a comment properly  Note: Removing one error may make others disappear (the compiler gets confused easily).

C. Hundhausen, A. O’Fallon15 Programming Errors (2) Run-time errors ◦ Commonly called "bugs" ◦ Cause the program to "crash": an error is reported, and control is turned over to the operating system ◦ Examples  Division by zero  Referencing a memory cell that's out of range  Getting into an infinite loop, which may ultimately cause a "stack overflow"  Referencing a null pointer (more on this later…)

C. Hundhausen, A. O’Fallon16 Programming Errors (3) Logic Errors ◦ Cause the program to compute incorrect results ◦ Often go unnoticed, at least at first ◦ Examples  Your algorithm is wrong because you misunderstand the problem  You do not obtain input data properly, so your computations work on the wrong data. For example: int year; char first, middle, last; printf("Enter the current year and press return: "); scanf("%d", &year); printf("Type in 3 initials and press return: "); scanf("%c%c%c", &first, &middle, &last); What goes wrong here?