Conversions of the type of the value of an expression

Slides:



Advertisements
Similar presentations
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Advertisements

Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Expressions.
Review Question What kind error is it when I try to multiply a number in a program by 1000 and store in a variable, but the variable is too small for the.
Lecture 071 CS 192 Lecture 7 Winter 2003 December 15-16, 2003 Dr. Shafay Shamail.
1 9/17/07CS150 Introduction to Computer Science 1 Type Casting.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
CS150 Introduction to Computer Science 1
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
CS 1400 Chapter 3: sections Variable initialization Variables may be initialized when declared –Form; type name = initial_value; –Example: int.
CS Jan 2007 Chapter 3: sections Variable initialization Variables may be initialized when declared –Form; type name = initial_value; –Example:
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
Expressions An expression is a sequence of operands and operators that reduces to a single value expression operator operand An operator is a language-specific.
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
Representation and Conversion of Numeric Types 4 We have seen multiple data types that C provides for numbers: int and double 4 What differences are there.
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
Operator Overloading and Type Conversions
***** SWTJC STEM ***** Chapter 2-3 cg 29 Java Operators Recall Java’s programming components: Packages - Collection of classes (Programs) Classes - Collections.
CS 363 Comparative Programming Languages Expressions and Assignment Statements.
Operators Using Java operators An operator takes one or more arguments and produces a new value. All operators produce a value from their.
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
CISC105 – General Computer Science Class 9 – 07/03/2006.
Integer numerical data types. The integer data types The integer data types use the binary number system as encoding method There are a number of different.
CP104 Introduction to Programming Overview of C Lecture 4__ 1 Assignment Statements An assignment statement is to store a value in a variable variable.
Expressions and Assignment Statements
Executable Statements 1. Def. Executable statements are instructions to the computer to perform specific tasks. 2. Two examples: method calls and assignment.
Chapter 7 Expressions and Assignment Statements. Outline Introduction Arithmetic Expressions Overloaded Operators Type Conversions Assignment Statements.
Data Type. Syntax Rules Recap keywords breakdoubleifsizeofvoid caseelseintstatic..... Identifiers not#me123th scanfprintf _idso_am_igedd007 Constant ‘a’‘+’
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
March 31, ICE 1341 – Programming Languages (Lecture #11) In-Young Ko Programming Languages (ICE 1341) Lecture #11 Programming Languages (ICE 1341)
Chapter Seven: Expressions and Assignment Statements Lesson 07.
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Chapter 3 The New Math. C++ Data Types simple integral charshort intlong bool floating float double Long double enum address pointer reference structured.
Gator Engineering Project 1 Grades released Re-grading –Within one week –TA: Fardad, or office hours: MW 2:00 – 4:00 PM TA Huiyuan’s office hour.
LESSON 5 – Assignment Statements JAVA PROGRAMMING.
1 Primitive Types n Four integer types:  byte  short  int (most common)  long n Two floating-point types:  float  double (most common) n One character.
Programming Fundamentals. Summary of Previous Lectures Phases of C++ Environment Data Types cin and cout.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
Chapter 7 Expressions and Assignment Statements. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 7 Topics Introduction Arithmetic Expressions.
ISBN Chapter 7 Expressions and Assignments Statements.
7-1/27 Chapter 7 Expressions and Assignment Statements Introduction Arithmetic Expressions Overloaded Operators Type Conversions Relational and Boolean.
Primitive Types Four integer types: Two floating-point types:
Expressions and Assignment Statements
Expressions and Assignment Statements
Chapter 7: Expressions and Assignment Statements
Expressions and Assignment Statements
Chap. 2. Types, Operators, and Expressions
Chapter 7: Expressions and Assignment Statements
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.
Assignment and Arithmetic expressions
Expressions and Assignment Statements
Operators and Expressions
Arithmetic Expressions
Explicit and Implicit Type Changes
Lecture 8.
More about Numerical Computation
Expressions and Assignment Statements
Basic Types Chapter 7 Copyright © 2008 W. W. Norton & Company.
Doing Arithmetic Today’s lecture is in chapter 2 Assignment statement:
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.
Chapter 3 Operators and Expressions
Homework Finishing Chapter 2 of K&R. We will go through Chapter 3 very quickly. Not a lot is new. Questions?
Type Conversion It is a procedure of converting one data type values into another data type In C programming language we are having two types of type conversion.
Operator King Saud University
PRESENTED BY ADNAN M. UZAIR NOMAN
Expressions and Assignment Statements
Chapter 3 The New Math.
Presentation transcript:

Conversions of the type of the value of an expression ANSI-C

Careful rules to follow You should know the type of the resulting expression You should match the type of the resulting expression with the expected type of The variable being assigned The parameter being matched by the expression as argument The type of the result of the function Knowing that you matching or did not match it EXPLICITELY will reduce your errors.

Autmatic type conversions for matching If the type of the value of the expression does not match the type expected (by the variable, by the parameter, by the function return type) the C language will convert the type of the resulting value to the expected type. This implicitely, automatic match performed by the language can produce be the source of hidden errors in your code.

Type of an expression When all the operands of an operation are of the same type, the resulting value will be of that type. We can write in C expressions where the operands are of different types. How to compute it’s the type of the resulting value? Based on the type of the dominand operand long double double float unsigned int

Exceptions For any expression involving only char, signed or unsigned char, short, or unsigned short, dominating type will be int or unsigned. For any expression involving only long int and unsigned int, dominating type is implementation dependent; should be unsigned long int, or long int.

Explicit conversions: Casting The language will implicitely find the type of the value of a resulting expression (important when the expression has mixed type operands). Sometimes programmer knows the type that the resulting value ought to have, and it can direct the machine to compute it: (type) expression Example: int x; int y; x/y or (float) x / y Important: the precedence of (cast) : lower than any unary operator but hiegher than any binary operator.

Formatted numbers for output: As you know you can specify the format for output values %d %c %s %f %e These can include a specification for field width: %10d %14.5f