Gator Engineering Project 1 Grades released Re-grading –Within one week –TA: Fardad, email or office hours: MW 2:00 – 4:00 PM TA Huiyuan’s office hour.

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

1 Lecture 7  Fundamental data types in C  Data type conversion:  Automatic  Casting  Character processing  getchar()  putchar()  Macros on ctype.h.
Lecture 071 CS 192 Lecture 7 Winter 2003 December 15-16, 2003 Dr. Shafay Shamail.
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.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
22-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
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.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
Data types and variables
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
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.
Chapter 2 Data Types, Declarations, and Displays
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
Basic Elements of C++ Chapter 2.
C Programming. Chapter – 1 Introduction Study Book for one month – 25% Learning rate Use Compiler for one month – 60%
Objectives You should be able to describe: Data Types
A First Book of ANSI C Fourth Edition
Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 4 Expressions.
Programming III SPRING 2015
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
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.
Chapter 2: Using Data.
Week 1 Algorithmization and Programming Languages.
Data Types. Data types Data type tells the type of data, that you are going to store in memory. It gives the information to compiler that how much memory.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Chapter 7: Basic Types Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 7 Basic Types.
Simple Data Types Built-In and User Defined Chapter 10.
Chapter 3 The Basic Data Types C/C++ Programming © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
CS115 FALL Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3.
Copyright © – Curt Hill Types What they do.
Chapter-4 Managing input and Output operation.  Reading, processing and writing of data are three essential functions of a computer program.  Most programs.
Chapter 4: Expressions Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 4 Expressions.
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
Chapter 7: Basic Types Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 7 Basic Types Last item in Page 89: check stdint.h.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 1.
Recap……Last Time [Variables, Data Types and Constants]
Module B - Computation1/61 Module-B-Computation Variables Basic Memory Operations Expressions.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
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.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
CCSA 221 Programming in C INPUT AND OUTPUT OPERATIONS IN C – PART 1 1.
Basic Data Types & Memory & Representation. Basic data types Primitive data types are similar to JAVA: char int short long float double Unlike in JAVA,
7. BASIC TYPES. Systems of numeration Numeric Types C’s basic types include integer types and floating types. Integer types can be either signed or unsigned.
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
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.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Functions.
Basic Types Chapter 7 Copyright © 2008 W. W. Norton & Company.
Chapter 4 C Program Control Part I
7. BASIC TYPES.
ITEC113 Algorithms and Programming Techniques
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
Java Programming: From Problem Analysis to Program Design, 4e
Fundamental Data Types
Conversions of the type of the value of an expression
Expressions Chapter 4 Copyright © 2008 W. W. Norton & Company.
Rock, paper, scissors example
CS111 Computer Programming
Basic Types Chapter 7 Copyright © 2008 W. W. Norton & Company.
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
Fundamental Data Types
Presentation transcript:

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 change –From Thursday to Friday (only this week) 1:00 – 3:00 PM –Makeup exam 1 Copyright © 2008 W. W. Norton & Company. All rights reserved. 1

Gator Engineering Homework for Chapter Copyright © 2008 W. W. Norton & Company. All rights reserved. 2

Gator Engineering Homework for Chapter Copyright © 2008 W. W. Norton & Company. All rights reserved. 3

Gator Engineering Homework for Chapter Copyright © 2008 W. W. Norton & Company. All rights reserved. 4

Gator Engineering Reading and Writing Characters Using getchar and putchar For single-character input and output, getchar and putchar are an alternative to scanf and printf. putchar writes a character: putchar(ch); Each time getchar is called, it reads one character, which it returns: ch = getchar(); Like scanf, getchar doesn’t skip white-space characters as it reads. Copyright © 2008 W. W. Norton & Company. All rights reserved. 5

Gator Engineering Reading and Writing Characters Using getchar and putchar Using getchar and putchar (rather than scanf and printf ) saves execution time. –getchar and putchar are much simpler than scanf and printf, which are designed to read and write many kinds of data in a variety of formats. –They are usually implemented as macros for additional speed. Copyright © 2008 W. W. Norton & Company. All rights reserved. 6

Gator Engineering Reading and Writing Characters Using getchar and putchar Consider the scanf loop that we used to skip the rest of an input line: do { scanf("%c", &ch); } while (ch != '\n'); Rewriting this loop using getchar gives us the following: do { ch = getchar(); } while (ch != '\n'); Copyright © 2008 W. W. Norton & Company. All rights reserved. 7

Gator Engineering Reading and Writing Characters Using getchar and putchar Moving the call of getchar into the controlling expression allows us to condense the loop: while ((ch = getchar()) != '\n') ; The ch variable isn’t even needed; we can just compare the return value of getchar with the new-line character: while (getchar() != '\n') ; Copyright © 2008 W. W. Norton & Company. All rights reserved. 8

Gator Engineering Reading and Writing Characters Using getchar and putchar getchar is useful in loops that skip characters as well as loops that search for characters. A statement that uses getchar to skip an indefinite number of blank characters: while ((ch = getchar()) == ' ') ; When the loop terminates, ch will contain the first nonblank character that getchar encountered. Copyright © 2008 W. W. Norton & Company. All rights reserved. 9

Gator Engineering Type Conversion For a computer to perform an arithmetic operation, the operands must usually be of the same size (the same number of bits) and be stored in the same way. When operands of different types are mixed in expressions, the C compiler may have to generate instructions that change the types of some operands so that hardware will be able to evaluate the expression. –If we add a 16-bit short and a 32-bit int, the compiler will arrange for the short value to be converted to 32 bits. –If we add an int and a float, the compiler will arrange for the int to be converted to float format. Copyright © 2008 W. W. Norton & Company. All rights reserved. 10

Gator Engineering Type Conversion Because the compiler handles these conversions automatically, without the programmer’s involvement, they’re known as implicit conversions. C also allows the programmer to perform explicit conversions, using the cast operator. The rules for performing implicit conversions are somewhat complex, primarily because C has so many different arithmetic types. Copyright © 2008 W. W. Norton & Company. All rights reserved. 11

Gator Engineering Type Conversion Implicit conversions are performed: –When the operands in an arithmetic or logical expression don’t have the same type. (C performs what are known as the usual arithmetic conversions.) –When the type of the expression on the right side of an assignment doesn’t match the type of the variable on the left side. –When the type of an argument in a function call doesn’t match the type of the corresponding parameter. –When the type of the expression in a return statement doesn’t match the function’s return type. Chapter 9 discusses the last two cases. Copyright © 2008 W. W. Norton & Company. All rights reserved. 12

Gator Engineering The Usual Arithmetic Conversions The usual arithmetic conversions are applied to the operands of most binary operators. If f has type float and i has type int, the usual arithmetic conversions will be applied to the operands in the expression f + i. Clearly it’s safer to convert i to type float (matching f ’s type) rather than convert f to type int (matching i ’s type). Copyright © 2008 W. W. Norton & Company. All rights reserved. 13

Gator Engineering The Usual Arithmetic Conversions Neither operand type is a floating type. First perform integral promotion on both operands. Then use the following diagram to promote the operand whose type is narrower: unsigned long int long int unsigned int int Copyright © 2008 W. W. Norton & Company. All rights reserved. 14

Gator Engineering The Usual Arithmetic Conversions When a signed operand is combined with an unsigned operand, the signed operand is converted to an unsigned value. This rule can cause obscure programming errors. It’s best to use unsigned integers as little as possible and, especially, never mix them with signed integers. Copyright © 2008 W. W. Norton & Company. All rights reserved. 15

Gator Engineering The Usual Arithmetic Conversions Example of the usual arithmetic conversions: char c; short int s; int i; unsigned int u; long int l; unsigned long int ul; float f; double d; long double ld; i = i + c; /* c is converted to int */ i = i + s; /* s is converted to int */ u = u + i; /* i is converted to unsigned int */ l = l + u; /* u is converted to long int */ ul = ul + l; /* l is converted to unsigned long int */ f = f + ul; /* ul is converted to float */ d = d + f; /* f is converted to double */ ld = ld + d; /* d is converted to long double */ Copyright © 2008 W. W. Norton & Company. All rights reserved. 16

Gator Engineering Conversion During Assignment The usual arithmetic conversions don’t apply to assignment. Instead, the expression on the right side of the assignment is converted to the type of the variable on the left side: char c; int i; float f; double d; i = c; /* c is converted to int */ f = i; /* i is converted to float */ d = f; /* f is converted to double */ Copyright © 2008 W. W. Norton & Company. All rights reserved. 17

Gator Engineering Conversion During Assignment Assigning a floating-point number to an integer variable drops the fractional part of the number: int i; i = ; /* i is now 842 */ i = ; /* i is now -842 */ Assigning a value to a variable of a narrower type will give a meaningless result (or worse) if the value is outside the range of the variable’s type: c = 10000; /*** WRONG ***/ i = 1.0e20; /*** WRONG ***/ f = 1.0e100; /*** WRONG ***/ Copyright © 2008 W. W. Norton & Company. All rights reserved. 18

Gator Engineering Casting Although C’s implicit conversions are convenient, we sometimes need a greater degree of control over type conversion. For this reason, C provides casts. A cast expression has the form ( type-name ) expression type-name specifies the type to which the expression should be converted. Copyright © 2008 W. W. Norton & Company. All rights reserved. 19

Gator Engineering Casting Using a cast expression to compute the fractional part of a float value: float f, frac_part; frac_part = f - (int) f; The difference between f and (int) f is the fractional part of f, which was dropped during the cast. Cast expressions enable us to document type conversions that would take place anyway: i = (int) f; /* f is converted to int */ Copyright © 2008 W. W. Norton & Company. All rights reserved. 20

Gator Engineering Casting Cast expressions also let us force the compiler to perform conversions. Example: float quotient; int dividend, divisor; quotient = dividend / divisor; To avoid truncation during division, we need to cast one of the operands: quotient = (float) dividend / divisor; Casting dividend to float causes the compiler to convert divisor to float also. Copyright © 2008 W. W. Norton & Company. All rights reserved. 21

Gator Engineering Casting C regards ( type-name ) as a unary operator. Unary operators have higher precedence than binary operators, so the compiler interprets (float) dividend / divisor as ((float) dividend) / divisor Other ways to accomplish the same effect: quotient = dividend / (float) divisor; quotient = (float) dividend / (float) divisor; Copyright © 2008 W. W. Norton & Company. All rights reserved. 22

Gator Engineering Type Definitions The #define directive can be used to create a “Boolean type” macro: #define BOOL int There’s a better way using a feature known as a type definition: typedef int Bool; Bool can now be used in the same way as the built-in type names. Example: Bool flag; /* same as int flag; */ Copyright © 2008 W. W. Norton & Company. All rights reserved. 23

Gator Engineering Advantages of Type Definitions Type definitions can make a program more understandable. If the variables cash_in and cash_out will be used to store dollar amounts, declaring Dollars as typedef float Dollars; and then writing Dollars cash_in, cash_out; is more informative than just writing float cash_in, cash_out; Copyright © 2008 W. W. Norton & Company. All rights reserved. 24

Gator Engineering Advantages of Type Definitions Type definitions can also make a program easier to modify. To redefine Dollars as double, only the type definition need be changed: typedef double Dollars; Without the type definition, we would need to locate all float variables that store dollar amounts and change their declarations. Copyright © 2008 W. W. Norton & Company. All rights reserved. 25

Gator Engineering The sizeof Operator The value of the expression sizeof ( type-name ) is an unsigned integer representing the number of bytes required to store a value belonging to type- name. sizeof(char) is always 1, but the sizes of the other types may vary. On a 32-bit machine, sizeof(int) is normally 4. Copyright © 2008 W. W. Norton & Company. All rights reserved. 26

Gator Engineering The sizeof Operator The sizeof operator can also be applied to constants, variables, and expressions in general. –If i and j are int variables, then sizeof(i) is 4 on a 32-bit machine, as is sizeof(i + j). When applied to an expression—as opposed to a type— sizeof doesn’t require parentheses. –We could write sizeof i instead of sizeof(i). Parentheses may be needed anyway because of operator precedence. –The compiler interprets sizeof i + j as (sizeof i) + j, because sizeof takes precedence over binary +. Copyright © 2008 W. W. Norton & Company. All rights reserved. 27

Gator Engineering The sizeof Operator Printing a sizeof value requires care, because the type of a sizeof expression is an implementation-defined type named size_t. In C89, it’s best to convert the value of the expression to a known type before printing it: printf("Size of int: %lu\n", (unsigned long) sizeof(int)); The printf function in C99 can display a size_t value directly if the letter z is included in the conversion specification: printf("Size of int: %zu\n", sizeof(int)); Copyright © 2008 W. W. Norton & Company. All rights reserved. 28