Structures, Operators

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

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

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

JAVA OPERATORS GENERAL

Operators and Expressions:

Fundamentals of Programming

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

Operators & Expressions

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

Operators. Java operators are classified into three categories:

Review of the C Programming Language for Principles of Operating Systems

Fundamental of Programming (C)

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

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.

Lecture 3. More About C

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

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

A flow chart is a graphical or symbolic representation of a process.

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

Information Science 1

Slide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng

Expressions and Precedence. Last updated 12/10/18

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Review of the C Programming Language

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Operators in C. Staff Incharge: S.Sasirekha

C-LANGUAGE CURRICULAM

Chapter 3: Operators, Expressions and Type Conversion

DEPARTMENT OF MATHS, MJ COLLEGE

Computers Programming Course 6. Iulian Năstac

Data Types and Variables in C language

Recursion Enums. Basics of Programming 1. Department of Networked Systems and Services G. Horváth, A.B. Nagy, Z. Zsóka, P. Fiala, A.

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

Lecture 02 C FUNDAMENTALS

9/2/2016. Expressions are Used to Perform Calculations. ECE 120: Introduction to Computing. Five Arithmetic Operators on Numeric Types

1/31/2017. Expressions are Used to Perform Calculations. ECE 120: Introduction to Computing. Five Arithmetic Operators on Numeric Types

Programming for Engineers: Operators, Expressions, and Statem

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

Writing Program in C Expressions and Control Structures (Selection Statements and Loops)

Part I Part 1 Expressions

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

Chapter 4: Basic C Operators

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

LECTURE 3 C++ Basics Part 2


Programming for Engineers Iteration

Operators And Expressions

Operators. Lecture 3 COP 3014 Spring January 16, 2018

High Performance Computing in C and C++

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

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

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

Department of Computer Science

CSCE 110 PROGRAMMING FUNDAMENTALS

C Programming a Q & A Approach

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

SECTION II: LANGUAGE BASICS

Computer System and programming in C

Unit 3. Operators. School of Science and Technology INTRODUCTION

Data Types. Data Types. Integer Types. Signed Integers

A Fast Review of C Essentials Part I

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Reserved Words and Identifiers

Programming in C++ 5. Integral data types

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Operators and Expression. Dr Muhamad Zaini Yunos JKBR, FKMP

Fall, 2015 Prof. Jungkeun Park

Operators and Expressions

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

Guide for The C Programming Language Chapter 1. Q1. Explain the structure of a C program Answer: Structure of the C program is shown below:

Computer Organization & Systems Exam I Example Questions

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

Type Checking. Prof. James L. Frankel Harvard University

Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100

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

Programming. Elementary Concepts

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

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

CSC 1214: Object-Oriented Programming

ANSI C Programming Simple Programs

Programming and Data Structures

XC Specification. 1 Lexical Conventions. 1.1 Tokens. The specification given in this document describes version 1.0 of XC.

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

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

Fundamental of C programming. - Ompal Singh

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

Slide 1 CS 170 Java Programming 1 Expressions Duration: 00:00:41 Advance mode: Auto

Lecture 02 Summary. C/Java Syntax 1/14/2009. Keywords Variable Declarations Data Types Operators Statements. Functions

I Internal Examination Sept Class: - BCA I Subject: - Principles of Programming Lang. (BCA 104) MM: 40 Set: A Time: 1 ½ Hrs.

OBJECT ORIENTED PROGRAMMING

Prof. Navrati Saxena TA: Rochak Sachan

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for

1.00 Lecture 4. Promotion

ISA 563 : Fundamentals of Systems Programming

Course Outline Introduction to C-Programming

More Programming Constructs -- Introduction

Transcription:

Structures Typedef Operators Type conversion Structures, Operators Basics of Programming 1 G. Horváth, A.B. Nagy, Z. Zsóka, P. Fiala, A. Vitéz 10 October, 2018 c based on slides by Zsóka, Fiala, Vitéz Structures, Operators 10 October, 2018 1 / 43

Structures Typedef Operators Type conversion Content 1 Structures Motivation Definition Assignment of value 2 Typename-assignment 3 Operators Definitions Operators Precedence 4 Type conversion c based on slides by Zsóka, Fiala, Vitéz Structures, Operators 10 October, 2018 2 / 43

Structures Typedef Operators Type conversion Motivation Definition Assignment of value Chapter 1 Structures c based on slides by Zsóka, Fiala, Vitéz Structures, Operators 10 October, 2018 3 / 43

Structures Typedef Operators Type conversion Motivation Definition Assignment of value User defined types Built-in types of C language sometimes are not appropriate for storing more complex data. Types introduced by the user (programmer) Enumeration Structures today s topic Bitfields Union c based on slides by Zsóka, Fiala, Vitéz Structures, Operators 10 October, 2018 4 / 43

Structures Typedef Operators Type conversion Motivation Definition Assignment of value Data elements that are coupled Storing date 1 int year ; 2 int month ; 3 int day ; Storing student data 1 char neptun [6]; 2 unsigned int smalltests ; 3 unsigned int missings ; Data of a chess game (white player, black player, when, where, moves, result) Data of one move (chess piece, from where, where to) Data of one square of the board (column, row) c based on slides by Zsóka, Fiala, Vitéz Structures, Operators 10 October, 2018 5 / 43

Structures Typedef Operators Type conversion Motivation Definition Assignment of value Storing data elements that are coupled Let s write a function to calculate scalar product (dot product) of 2D vectors! 1 double v_ scalarproduct ( double x1, double y1, 2 double x2, double y2) 3 { 4... 5 } How shall we pass coupled parameters? The number of parameters may become too large Let s write a function to calculate difference of two vectors! 1?????? v_ difference ( double x1, double y1, 2 double x2, double y2) 3 { 4... 5 } How does the function returns with coupled data? c based on slides by Zsóka, Fiala, Vitéz Structures, Operators 10 October, 2018 6 / 43

Structures Typedef Operators Type conversion Motivation Definition Assignment of value Encapsulation Structure compound data type consisting of data elements (maybe of different types) that are coupled (belong together) student neptun data elements are called fields or members can be copied with one assignment small test results can be parameter of function missings can be return value of function This is the most effective type of C language c based on slides by Zsóka, Fiala, Vitéz Structures, Operators 10 October, 2018 7 / 43

Structures Typedef Operators Type conversion Motivation Definition Assignment of value Structures in C 1 struct vector { /* definition of structure type */ 2 double x; double y; 3 }; 4 5 struct vector v_ difference ( struct vector a, 6 struct vector b) { 7 struct vector c; 8 c.x = a.x - b.x; 9 c.y = a.y - b.y; 10 return c; 11 } 12 13 int main ( void ) { 14 struct vector v1, v2, v3; 15 v1.x = 1.0; v1.y = 2.0; 16 v2 = v1; 17 v3 = v_difference (v1, v2 ); 18 return 0; 19 } c based on slides by Zsóka, Fiala, Vitéz Structures, Operators 10 October, 2018 8 / 43

Structures Typedef Operators Type conversion Motivation Definition Assignment of value Syntax of structures Declaration of structures struct [<structure label>] opt {<structure member declarations>} [<variable identifiers>] opt ; 1 /* structure type for storing date */ 2 struct date { 3 int year ; 4 int month ; 5 int day ; 6 } d1, d2; /* two instances ( variables ) */ [<structure label>] opt can be omitted if we don t refer to it later [<variable identifiers>] opt declaration of variables of structure type c based on slides by Zsóka, Fiala, Vitéz Structures, Operators 10 October, 2018 9 / 43

Structures Typedef Operators Type conversion Motivation Definition Assignment of value Syntax of structures Using structure type Declaration of variables struct <structure label> <variable identifiers>; Accessing structure members <structure identifier>.<member identifier> Structure members can be used in the same way as variables 1 struct date d1, d2; 2 d1. year = 2012; 3 d2. year = d1. year ; 4 scanf ("%d", &d2. month ); Initialization of structures is possible in the same way as for arrays: 1 struct date d3 = {2011, 5, 2}; c based on slides by Zsóka, Fiala, Vitéz Structures, Operators 10 October, 2018 10 / 43

Structures Typedef Operators Type conversion Motivation Definition Assignment of value Assignment of value to structures Value of a structure variable (value of all members) can be updated with one single assignment. 1 struct date d3 = {2013, 10, 22}, d4; 2 d4 = d3; c based on slides by Zsóka, Fiala, Vitéz Structures, Operators 10 October, 2018 11 / 43

Structures Typedef Operators Type conversion Chapter 2 Typename-assignment c based on slides by Zsóka, Fiala, Vitéz Structures, Operators 10 October, 2018 12 / 43

Structures Typedef Operators Type conversion Definition We can rename types in C 1 typedef int rabbit ; 2 3 rabbit main () { 4 rabbit i = 3; 5 return i; 6 } Typename-assignment typedef assigns a nickname to the type. It does not create a new type, the type of all variables created with the nicname will be the original type. c based on slides by Zsóka, Fiala, Vitéz Structures, Operators 10 October, 2018 13 / 43

Structures Typedef Operators Type conversion What is the use of it? More meaningful source code, more easy to read 1 typedef float voltage ; /* we need a smaller */ 2 3 voltage V1 = 1.0; 4 double c = 2.0; 5 voltage V2 = c * V1; Easy to maintain We can get rid of typenames of more than one word 1 typedef struct vector vector ; c based on slides by Zsóka, Fiala, Vitéz Structures, Operators 10 October, 2018 14 / 43

Structures Typedef Operators Type conversion Vector example with typedef 1 typedef struct { /* we can omit the label */ 2 double x; double y; 3 } vector ; 4 5 6 vector v_ difference ( vector a, vector b) { 7 vector c; 8 c.x = a.x - b.x; 9 c.y = a.y - b.y; 10 return c; 11 } 12 13 int main ( void ) { 14 vector v1, v2, v3; 15 v1.x = 1.0; v1.y = 2.0; 16 v2 = v1; 17 v3 = v_difference (v1, v2 ); 18 return 0; 19 } c based on slides by Zsóka, Fiala, Vitéz Structures, Operators 10 October, 2018 15 / 43

Structures Typedef Operators Type conversion A more complex structure 1 typedef struct { 2 double x; 3 double y; 4 } vector ; 5 6 typedef struct { 7 vector centrepoint ; 8 double radius ; 9 } circle ; y k v x 1 circle k = {{3.0, 2.0}, 1.5}; 2 vector v = k. centrepoint ; 3 k. centrepoint. y = -2.0; c based on slides by Zsóka, Fiala, Vitéz Structures, Operators 10 October, 2018 16 / 43

Structures Typedef Operators Type conversion Definitions Operators Prec. Chapter 3 Operators c based on slides by Zsóka, Fiala, Vitéz Structures, Operators 10 October, 2018 17 / 43

Structures Typedef Operators Type conversion Definitions Operators Prec. Operations Denoted with operators (special symbols) They work with operands They result a data with type Polymorphic: have different behaviour on different operand types 2.0 3.0 2 3 / / 0. 6 0 c based on slides by Zsóka, Fiala, Vitéz Structures, Operators 10 October, 2018 18 / 43

Structures Typedef Operators Type conversion Definitions Operators Prec. Expressions and operators Expressions eg. 8 + 5 < -a - 2 Built up of constants, variable references and operations a (3) 8 5 + 13 < - -3 - -5 2 FALSE by evaluating them the result is one data element with type. c based on slides by Zsóka, Fiala, Vitéz Structures, Operators 10 October, 2018 19 / 43

Structures Typedef Operators Type conversion Definitions Operators Prec. Types of operators Considering the number of operands unary with one operand -a binary with two operands 1+2 Considering the interpretation of the operand arithmetic relational logical bitwise misc c based on slides by Zsóka, Fiala, Vitéz Structures, Operators 10 October, 2018 20 / 43

Structures Typedef Operators Type conversion Definitions Operators Prec. Arithmetic operators operation unary plus unary minus addition subtraction multiplication syntax +<expression> -<expression> <expression> + <expression> <expression> - <expression> <expression> * <expression> division <expression> / <expression> type of the result depends on type of the operands, if both are integer, then it is an integer division modulus <expression> % <expression> c based on slides by Zsóka, Fiala, Vitéz Structures, Operators 10 October, 2018 21 / 43

Structures Typedef Operators Type conversion Definitions Operators Prec. True or false Boolean in C (repeated) Every boolean like result is int type, and its value is 0, if false 1, if true 1 printf ("%d\t%d", 2<3, 2==3); 1 0 A value interpreted as boolean is false, if its value is represented with 0 bits only true, if its value is represented with not only 0 bits 1 while (1) { /* infinite loop */ } 2 while ( -3.0) { /* infinite loop */ } 3 while (0) { /* this here is never executed */ } c based on slides by Zsóka, Fiala, Vitéz Structures, Operators 10 October, 2018 22 / 43

Structures Typedef Operators Type conversion Definitions Operators Prec. Relational operators operation relational operators checking equality checking non-equality syntax <left value> < <expression> <left value> <= <expression> <left value> > <expression> <left value> >= <expression> <left value> == <expression> <left value>!= <expression> They give logical value (int, 0 or 1) as result. c based on slides by Zsóka, Fiala, Vitéz Structures, Operators 10 October, 2018 23 / 43

Structures Typedef Operators Type conversion Definitions Operators Prec. Logical operators operation logical NOT (complement) syntax!<expression> 1 int a = 0 x5c ; /* 0101 1100, true */ 2 int b =! a; /* 0000 0000, false */ 3 int c =! b; /* 0000 0001, true */ Conlusion:!!a a, only if we look at their boolean value. 1 int finish = 0; 2 while (! finish ) { 3 int b; 4 scanf ("%d", &b); 5 if (b == 0) 6 finish = 1; 7 } c based on slides by Zsóka, Fiala, Vitéz Structures, Operators 10 October, 2018 24 / 43

Structures Typedef Operators Type conversion Definitions Operators Prec. Logical operators operation logical AND logical OR syntax <expression> && <expression> <expression> <expression> Logical short-cut: Operands are evaluated from left to right. But only until the result is not obvious. We make use of this feature very often. 1 int a [5] = {1, 2, 3, 4, 5}; 2 int i = 0; 3 while ( i < 5 && a[ i] < 20) 4 i = i +1; /* no over - indexing */ c based on slides by Zsóka, Fiala, Vitéz Structures, Operators 10 October, 2018 25 / 43

Structures Typedef Operators Type conversion Definitions Operators Prec. Some more operators We have used them so far, but never have called them operators before. operation function call array reference structure-reference syntax <function>(<actual arguments>) <array>[<index>] <structure>.<member> 1 c = sin (3.2); /* () */ 2 a [28] = 3; /* [] */ 3 v.x = 2.0; /*. */ c based on slides by Zsóka, Fiala, Vitéz Structures, Operators 10 October, 2018 26 / 43

Structures Typedef Operators Type conversion Definitions Operators Prec. Operators with side effects Some operators have side effects main effect: calculating the result of evaluation side effect: the value of the operand is modified Simple assignment operator = a 2 In C language, assignment is an expression! its side effect is the assignment (a is modified) its main effect is the new value of a = 2 Because of its main effect, this is also meaningful: 1 int a; 2 int b = a = 2; b is initialised with the value of expression a=2 (this also has a side effect), and the side effect of it is that a is also modified. c based on slides by Zsóka, Fiala, Vitéz Structures, Operators 10 October, 2018 27 / 43

Structures Typedef Operators Type conversion Definitions Operators Prec. Left-value Assignement operator modifies value of the left side operand. There can be only modifiable entity on the left side. Left-value (lvalue) An expression that can appear on the left side of the assignment. As far as we know now, left-value can be a variable reference a = 2 element of an array array[3] = 2 member of a structure v.x = 2... Examples for non-left-value expressions constant 3 = 2 error arithmetic expression a+4 = 2 error logical expression a>3 = 2 error function value sin(2.0) = 2 error c based on slides by Zsóka, Fiala, Vitéz Structures, Operators 10 October, 2018 28 / 43

Structures Typedef Operators Type conversion Definitions Operators Prec. Expression or statement? An operation that has side effect can be a statement in a program. Expression statement <Expression>; Expression is evaluated, but the result is thrown away (but all side effects are completed). 1 a = 2 /* expression, its value is 2, it has side effect */ 1 a = 2; /* statement, it has no value */ 2 /* generates a side effect */ As the main effect is surpressed, there is no sense of making expression statements if the expression has no side effect. 1 2 + 3; /* valid statement, it generates nothing */ c based on slides by Zsóka, Fiala, Vitéz Structures, Operators 10 October, 2018 29 / 43

Structures Typedef Operators Type conversion Definitions Operators Prec. Assignement operators expression compound assignment syntax <left-value> += <expression> <left-value> -= <expression> <left-value> *= <expression> <left-value> /= <expression> <left-value> %= <expression> Almost: <left-value>=<left-value><op><expression> 1 a += 2; /* a = a + 2; */ 2 t[ rand ()] += 2; /* NOT t[ rand ()] = t[ rand ()] + 2; */ Left-value is evaluated only once. c based on slides by Zsóka, Fiala, Vitéz Structures, Operators 10 October, 2018 30 / 43

Structures Typedef Operators Type conversion Definitions Operators Prec. Other operators with side effects expression syntax post increment <left-value> ++ post decrement <left-value> -- it is increased/decreased by one after evaluation pre increment pre decrement ++<left-value> --<left-value> it is increased/decreased by one before evaluation 1 b = a ++; /* b = a; a += 1; */ 2 b = ++a; /* a += 1; b = a; */ 1 for (i = 0; i < 5; ++i) { /* five times */ } c based on slides by Zsóka, Fiala, Vitéz Structures, Operators 10 October, 2018 31 / 43

Structures Typedef Operators Type conversion Definitions Operators Prec. Other operators operation modifying type (casting) size for storage (in bytes) the expression is not evaluated syntax (<type>)<expression> sizeof <expression> 1 int a1 =2, a2 =3, storagesize ; 2 double b; 3 b = a1/( double )a2; 4 storagesize = sizeof 3/ a1; 5 storagesize = sizeof ( double ) a1; 6 storagesize = sizeof ( double ); c based on slides by Zsóka, Fiala, Vitéz Structures, Operators 10 October, 2018 32 / 43

Structures Typedef Operators Type conversion Definitions Operators Prec. Other operators operation comma syntax <expression>, <expression> Operands are evaluated from left to right. Value of first expression is thrown away. Value and type of the entire expression is the value and type of the second expression. 1 int step, j; 2 /* two - digit numbers with increasing step size */ 3 for ( step =1,j =10 ; j <100; j+= step, step ++) 4 printf ("%d\n", j); c based on slides by Zsóka, Fiala, Vitéz Structures, Operators 10 October, 2018 33 / 43

Structures Typedef Operators Type conversion Definitions Operators Prec. Other operators operation syntax (ternary) conditional expr. <cond.>? <expr.1> : <expr.2> if <cond.> is true, then <expr.1>, otherwise <expr.2>. only one of <expr.1> and <expr.2> is evaluated. It does not subtitute the if statement. 1 a = a < 0? -a : a; /* determining absolute value */ c based on slides by Zsóka, Fiala, Vitéz Structures, Operators 10 October, 2018 34 / 43

Structures Typedef Operators Type conversion Definitions Operators Prec. Features of operations performed on data Precedence If there are different operations, which is evaluated first? 1 int a = 2 + 3 * 4; /* 2 + (3 * 4) */ Associativity If there are equivalent operations, which is evaluated first? (Does it bind from left to right or from right to left?) 1 int b = 11-8 - 2; /* (11-8) - 2 */ Instead of memorizing the rules, use parentheses! c based on slides by Zsóka, Fiala, Vitéz Structures, Operators 10 October, 2018 35 / 43

Structures Typedef Operators Type conversion Definitions Operators Prec. List of operators in C Operateors are listed top to bottom, in descending precedence (operators in the same row have the same precedence) 1 () []. -> /* highest */ 2! ~ ++ -- + - * & (<type >) sizeof 3 * / % 4 + - 5 << >> 6 < <= > >= 7 ==!= /* forbidden to learn! */ 8 & /* use parentheses! */ 9 ^ 10 11 && 12 13?: 14 = += -= *= /= %= &= ^= = <<= >>= 15, /* lowest */ c based on slides by Zsóka, Fiala, Vitéz Structures, Operators 10 October, 2018 36 / 43

Structures Typedef Operators Type conversion Definitions Operators Prec. Operators of C language Summarized A lot of effective operators Some operators have side effects that will occur during evaluation We always try to separate main and side effects Instead of this: 1 t [++ i] = func (c -=2); we rather write this: 1 c -= 2; /* means the same */ 2 ++ i; /* not less effective */ 3 t[ i] = func ( c); /* and I will understand it tomorrow too */ c based on slides by Zsóka, Fiala, Vitéz Structures, Operators 10 October, 2018 37 / 43

Structures Typedef Operators Type conversion Chapter 4 Type conversion c based on slides by Zsóka, Fiala, Vitéz Structures, Operators 10 October, 2018 38 / 43

Structures Typedef Operators Type conversion What is that? In some cases the C-program needs to convert the type of our expressions. 1 long func ( float f) { 2 return f; 3 } 4 5 int main ( void ) { 6 int i = 2; 7 short s = func ( i); 8 return 0; 9 } In this example: int float long short int float rounding, if the number is large float long may cause overflow, rounding to integer long short may cause overflow c based on slides by Zsóka, Fiala, Vitéz Structures, Operators 10 October, 2018 39 / 43

Structures Typedef Operators Type conversion Converting types Basic principle preserve the value, if possible In case of overflow the result is theoretically undefined Conversion with one operand (we have seen that) at assignment of value at calling a function (when actualising the formal parameters) Conversion with two operands (eg. 2/3.4 ) evaluating an operation c based on slides by Zsóka, Fiala, Vitéz Structures, Operators 10 October, 2018 40 / 43

Structures Typedef Operators Type conversion Conversion with two operands The conversion of the two operands to the same, common type happens according to these rules operand one the other operand common, new type long double anything long double double anything double float anything float unsigned long anything unsigned long long anything (int, unsigned) long unsigned anything (int) unsigned int anything (int) int c based on slides by Zsóka, Fiala, Vitéz Structures, Operators 10 October, 2018 41 / 43

Structures Typedef Operators Type conversion Type conversions Example for conversion 1 int a = 3; 2 double b = 2.4; 3 a = a* b; 1 3 3.0 2 3.0 2.4 7.2 3 7.2 7 c based on slides by Zsóka, Fiala, Vitéz Structures, Operators 10 October, 2018 42 / 43

Structures Typedef Operators Type conversion Thank you for your attention. c based on slides by Zsóka, Fiala, Vitéz Structures, Operators 10 October, 2018 43 / 43