A First C Program (mixing datatypes)

Slides:



Advertisements
Similar presentations
Type Conversion. C provides two methods of changing the type of an expression: Type conversion (done implicitly) Cast expressions (done explicitly) Examples.
Advertisements

STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
1 9/13/06CS150 Introduction to Computer Science 1 Type Casting.
1 Lecture 7  Fundamental data types in C  Data type conversion:  Automatic  Casting  Character processing  getchar()  putchar()  Macros on ctype.h.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
Primitives in Java Java has eight primitive types –boolean –integral types: signed: long, int, short, byte unsigned: char –floating point types: double,
ECP4136 Java Technology Tutorial 2. Overview Replacement class? Previous tutorials What you’ve learnt Tasks for this tutorial session.
Computer Science 1620 Other Data Types. Quick Review: checklist for performing user input: 1) Be sure variable is declared 2) Prompt the user for input.
1 9/17/07CS150 Introduction to Computer Science 1 Type Casting.
1 Fundamental Data Types. 2 Declaration All variables must be declared before being used. –Tells the compiler to set aside an appropriate amount of space.
Constants Variables change, constants don't final = ; final double PI = ; … area = radius * radius * PI; see Liang, p. 32 for full code.
1 Lecture 3 Bit Operations Floating Point – 32 bits or 64 bits 1.
CS150 Introduction to Computer Science 1
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.
Lectures on Numerical Methods1 Tokens in C zKeywords  These are reserved words of the C language. For example int, float, if, else, for, while etc. zIdentifiers.
Fundamental data types Horstmann Chapter 4. Constants Variables change, constants don't final = ; final double PI = ; … areaOfCircle = radius *
Java Building Elements Lecture 2 Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Information Representation: Negative and Floating Point.
COMP 116: Introduction to Scientific Programming Lecture 28: Data types.
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
Dale Roberts Program Control using Java - Boolean Expressions Dale Roberts, Lecturer Computer Science, IUPUI Department of.
CISC105 – General Computer Science Class 9 – 07/03/2006.
Java Simple Types CSIS 3701: Advanced Object Oriented Programming.
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.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
Data Type. Syntax Rules Recap keywords breakdoubleifsizeofvoid caseelseintstatic..... Identifiers not#me123th scanfprintf _idso_am_igedd007 Constant ‘a’‘+’
COMP Primitive and Class Types Yi Hong May 14, 2015.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Information Representation: Negative Integer Representation.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
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.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
Basic Data Types & Memory & Representation. Basic data types Primitive data types are similar to JAVA: char int short long float double Unlike in JAVA,
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
1 Section 3.2b Arithmetic Operators Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Primitive Types Four integer types: Two floating-point types:
Basic Data Types & Memory & Representation
Data Types, Variables & Arithmetic
Tokens in C Keywords Identifiers Constants
Abstract Data Types Polynomials CSCI 240
A First C Program (mixing datatypes)
Assignment and Arithmetic expressions
Variable Declarations, Data types, Expressions
Variable Declarations, Data types, Expressions
Variable Declaration, Data types, Expressions
Fundamental Data Types
Data Type.
Variable Declarations, Data types, Expressions
Conversions of the type of the value of an expression
Explicit and Implicit Type Changes
Lecture 3 Expressions Richard Gesick.
Negative Integer Representation
Numerical Data Types.
More about Numerical Computation
Chapter-3 Operators.
Lectures on Numerical Methods
A First C Program (mixing datatypes)
Fundamental Data Types
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.
Presentation transcript:

A First C Program (mixing datatypes) Department of Computer and Information Science, School of Science, IUPUI A First C Program (mixing datatypes) Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu

Data Type Conversion Rule #1 float  double char, short  int float  double Rule #2 (double ← long ← unsigned ← int) If either operand is double, the other is converted to double, and the result is double Otherwise, if either operand is long, the other is converted to long, and the result is long Otherwise, if either operand is unsigned, the other is converted to unsigned, and the result is unsigned Otherwise, the operand must be int

Examples Example: c: char, u: unsigned, i: int, d: double, f:float, s: short, l: long, Expression Final Data Type Explanation c – s / i int shortint, int/int, charint, int-int u * 3 – i unsigned int(3)unsigned, unsigned*unsigned=unsigned, intunsigned, unsigned-unsigned=unsigned u * 3.0 – i double unsigneddouble, double*double, intdouble, double-double=double c + i int charint c + 1.0 double charint (rule 1), intdouble(rule 2) 3 * s * l long shortint, int*int, intlong, long*long

Data Type Conversion (cont.) Note: Conversion of int to long preserves sign, so does short Var = expr f = d; /* round off */ i = f; /* truncates fractions part, if the number is too big to fit, the result is undetermined */ i = l; s = i; and c = i; /* may eliminate high order bits */

If a specific type is required, the following syntax may be used, called cast operator. (type) expr Example: float f=2.5; x = (int)f + 1; /* the result is 3, Q: will f value be changed? */ Unsigned int to int: there is not actual conversion between int and unsigned int. Example:(Assuming 2’s complement machine and int is 2 bytes long) unsigned i = 65535; int j; j = i; /* j will be –1 */ j = -2; unsigned i = 1 + j; /* i= 65535 */