Chapter 3. Section 3.10 Type of Expressions and Automatic Conversion. CS 50 Hathairat Rattanasook

Similar documents
1.3b Type Conversion

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

Fundamentals of Programming Session 7

Day05 A. Young W. Lim Sat. Young W. Lim Day05 A Sat 1 / 14

Chapter 3 Structured Program Development in C Part II

MA 511: Computer Programming Lecture 3: Partha Sarathi Mandal

Arithmetic Expressions in C

In Fig. 3.5 and Fig. 3.7, we include some completely blank lines in the pseudocode for readability. programs into their various phases.

Computer System and programming in C

C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga

Operators and Expression. Dr Muhamad Zaini Yunos JKBR, FKMP

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

Arithmetic type issues

Note: unless otherwise stated, the questions are with reference to the C Programming Language. You may use extra sheets if need be.

Programming for Engineers Iteration

Programming in C++ 6. Floating point data types

Primitive Types. Four integer types: Two floating-point types: One character type: One boolean type: byte short int (most common) long

Arithmetic Operators. Portability: Printing Numbers

Tutorial 1: C-Language

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

These are reserved words of the C language. For example int, float, if, else, for, while etc.

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

Lecture 3. More About C

Chapter 2: Using Data

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

Data Types and Variables in C language

Basic Assignment and Arithmetic Operators

Operators and Expressions:

Fundamentals of Programming Session 20

Lecture 2: C Programming Basic

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Work relative to other classes

Chapter 4. Section 4.5 Initialization of Arrays. CS 50 Hathairat Rattanasook

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Informatics Ingeniería en Electrónica y Automática Industrial

Chapter 2, Part III Arithmetic Operators and Decision Making

Chapter 5. Section 5.1 Introduction to Strings. CS 50 Hathairat Rattanasook

Data Types. Data Types. Integer Types. Signed Integers

LECTURE 3 C++ Basics Part 2

Chapter 2. Section 2.5 while Loop. CS 50 Hathairat Rattanasook

Fundamental of Programming (C)

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

C: How to Program. Week /Mar/05

A Fast Review of C Essentials Part I

Expressions and Statementst t. Assignment Operator. C Programming Lecture 6 : Operators. Expression

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

CHAPTER 3 BASIC INSTRUCTION OF C++

Beginning C Programming for Engineers

Introduction to C. Systems Programming Concepts

ANSI C Programming Simple Programs

COP 2000 Introduction to Computer Programming Mid-Term Exam Review

27-Sep CSCI 2132 Software Development Lecture 10: Formatted Input and Output. Faculty of Computer Science, Dalhousie University. Lecture 10 p.

EL2310 Scientific Programming

Chapter 2 - Introduction to C Programming

CS2141 Software Development using C/C++ C++ Basics

C Program. Output. Hi everyone. #include <stdio.h> main () { printf ( Hi everyone\n ); }

CS2351 Data Structures. Lecture 7: A Brief Review of Pointers in C

Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used.

BRANCHING if-else statements

Fundamentals of Programming

Operators & Expressions

Chapter 4. Section 4.4 Passing Parameters to Functions. CS 50 Hathairat Rattanasook

COMP Primitive and Class Types. Yi Hong May 14, 2015

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

Programming and Data Structures

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

by Pearson Education, Inc. All Rights Reserved.

Data Handing in Python

Have the same meaning as variables in algebra Single alphabetic character Each variable needs an identifier that distinguishes it from the others a =

MA 511: Computer Programming Lecture 2: Partha Sarathi Mandal

Fundamentals of Programming. Lecture 3: Introduction to C Programming

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

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Chapter 7. Expressions and Assignment Statements ISBN

Creating, Compiling and Executing

CSCI 2132: Software Development. Norbert Zeh. Faculty of Computer Science Dalhousie University. Introduction to C. Winter 2019

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Structured programming. Exercises 3

Chapter 3. Fundamental Data Types

Basics of Java Programming

Operators and Type Conversion. By Avani M. Sakhapara Assistant Professor, IT Dept, KJSCE

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

CS102: Variables and Expressions

CS242 COMPUTER PROGRAMMING

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Computers in Engineering. Moving From Fortran to C Michael A. Hawker

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

2/5/2018. Expressions are Used to Perform Calculations. ECE 220: Computer Systems & Programming. Our Class Focuses on Four Types of Operator in C

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

Introduction to Programming Using Java (98-388)

n Group of statements that are executed repeatedly while some condition remains true

Programming for Engineers: Operators, Expressions, and Statem

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

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

Prof. Navrati Saxena TA: Rochak Sachan

Chapter 4: Basic C Operators

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

WARM UP LESSONS BARE BASICS

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

Transcription:

Chapter 3 Section 3.10 Type of Expressions and Automatic Conversion CS 50 Hathairat Rattanasook

Types of Expressions and Automatic Conversions In C, every expression has an associated type. Operators and operands within the expression combine to determine the expression s type. Some type conversions are automatically performed in calculating the final value of expressions involving the arithmetic operators. CS 50 - Hathairat Rattanasook 1

Expressions with a relational operator An expression provides a value with a type int either 1 (true) or 0 (false). An expression contains relational operators which are!, &&,, ==,!=, <, <=, > and >=. CS 50 - Hathairat Rattanasook 2

A list of the dominating types from most to least dominating long doble double float unsigned long long unsigned int CS 50 - Hathairat Rattanasook 3

Exception For any expression involving only char, signed or unsigned char, short, or unsigned short, the dominating expression will be of type int or unsigned. For expressions consisting of types long int and unsigned int, the dominating type depends on the implementation. It may be either unsigned long int or long int. CS 50 - Hathairat Rattanasook 4

Example Int short char short + double will have type double + int will have type int + int will have type int + char will have type int A different compiler may represent a different size for each type. CS 50 - Hathairat Rattanasook 5

The actual rules for automatic conversions on the types in an arithmetic expression 1. The type char, short, unsigned char, and unsigned short are converted to type int if all the values of the original type can be represented in type int. If some values cannot be represented in type int, the conversion is to type unsiged int. CS 50 - Hathairat Rattanasook 6

The rules for automatic conversion cont. 2. After the initial conversions of types char, short, unsigned char, and unsigned short, expressions involving the unary operators -, --, and ++ will have a type matching the type of the operand. Example: -int, --int, and int++ are all type int -float, --float, and float++ are all type float -char, --char, and char++ are all type char CS 50 - Hathairat Rattanasook 7

The rules for automatic conversion cont. 3. If both the operands are of the same type, the expression will have that type. Example: int + int, int int, int * int and int / int are all type int float + float, float float, float * float and float/float are all type float CS 50 - Hathairat Rattanasook 8

Attention! int / int is type int What is the value of the results in the following program? #include <stdio.h> int main() { printf("result of 10/3 is %d\n", 10 / 3); printf("result of 5/2 is %d", 5 / 2); return 0; } CS 50 - Hathairat Rattanasook 9

Answer CS 50 - Hathairat Rattanasook 10

The rules for automatic conversion cont. 4. If and expression contains an operand of type long double, the other operand is converted to long double, before calculating the expression s value. The conversion is finished. Example: long double + int int will be converted to long double before calculating long double + char char will be converted to long double before calculating CS 50 - Hathairat Rattanasook 11

The rules for automatic conversion cont. 5. Otherwise, if there exits an operand of type double, the other operand is converted to type double before calculation. Example: double + int int will be converted to double double + short short will be converted to double double + char char will be converted to double double + long double double will be converted to long double ( previous rule) CS 50 - Hathairat Rattanasook 12

The rules for automatic conversion cont. 6. Similarly, if one operand is of type float, the other operand is converted to type float and the conversion terminates. 7. Of these, the type unsigned long dominates in that one operand of type unsigned long causes the other operand to be converted to that type. CS 50 - Hathairat Rattanasook 13

The rules for automatic conversion cont. 8. Operands of types long int and unsigned int in the same expression are converted to type long int if the range of values that can be represented by type unsigned int is a subset of the range that can be represented by type long. Otherwise, both values are converted to type unsigned long int. CS 50 - Hathairat Rattanasook 14

The rules for automatic conversion cont. 9. Otherwise, if one operand has type long, the other is converted to type long. 10. If one operand has type unsigned, the other operand is converted to type unsigned. 11.Otherwise, both operands are of type int. CS 50 - Hathairat Rattanasook 15

Chapter 3 Section 3.11 Forced Type Conversions CS 50 Hathairat Rattanasook

Forced Type Conversions Sometimes the default conversions do not give the desired resultant type. A type cast can be used in these situations. Any type in a C program can be cast to any other type. However, data might be lost. CS 50 - Hathairat Rattanasook 17

The type cast The cast consists of a pair of parentheses enclosing a type specify. It is a unary operator in C. Example: int number = 5; // The number variable is integer with value 5. (float)number; // The number is now type float (unsigned)number;// The number is now type unsigned (short)number; // The number is now type short CS 50 - Hathairat Rattanasook 18

Question? What is the output of the following program? #include <stdio.h> int main() { int ten = 10; int three = 3; } printf("1. 10 / 3 is %f\n", (double)(ten / three)); printf("2. 10 / 3 is %f", ((double)ten) / ((double)three)); return 0; CS 50 - Hathairat Rattanasook 19

Answer CS 50 - Hathairat Rattanasook 20