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

Similar documents
THE FUNDAMENTAL DATA TYPES

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

Chapter 3. Fundamental Data Types

Number Systems. Binary Numbers. Appendix. Decimal notation represents numbers as powers of 10, for example

Chapter 7. Basic Types

Introduction to C. Systems Programming Concepts

Computer System and programming in C

Programming in C++ 6. Floating point data types

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

First of all, it is a variable, just like other variables you studied

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

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

int main(void) { int a, b, c; /* declaration */

CHAPTER 3 Expressions, Functions, Output

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

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

Data Types. Data Types. Integer Types. Signed Integers

ISA 563 : Fundamentals of Systems Programming

Basic Types and Formatted I/O

Programming in C++ 5. Integral data types

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

10.1. Unit 10. Signed Representation Systems Binary Arithmetic

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O

1.3b Type Conversion

Work relative to other classes

Data Type Fall 2014 Jinkyu Jeong

Operators and Expressions:

Internal representation. Bitwise operators

Internal representation. Bitwise operators

COMP2611: Computer Organization. Data Representation

Expressions and Casting

More Programming Constructs -- Introduction

Today. o main function. o cout object. o Allocate space for data to be used in the program. o The data can be changed

Internal representation. Bitwise operators

Number Systems. Both numbers are positive

Variables and Constants

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

Computer Systems Programming. Practice Midterm. Name:

World Inside a Computer is Binary

Fundamentals of Programming

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

Fundamental of Programming (C)

On a 64-bit CPU. Size/Range vary by CPU model and Word size.

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

Arithmetic Operators. Portability: Printing Numbers

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

INTRODUCTION TO COMPUTER SCIENCE - LAB

Exercise: Using Numbers

Pointers (2) Applications

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

LECTURE 3 C++ Basics Part 2

CS 265. Computer Architecture. Wei Lu, Ph.D., P.Eng.

Main Memory Organization

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

Floating Point Numbers

Floating Point Numbers

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

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

Computer Organization & Systems Exam I Example Questions

CS107 Handout 13 Spring 2008 April 18, 2008 Computer Architecture: Take II

CSC 1107: Structured Programming

3. Java - Language Constructs I

CS101 Lecture 04: Binary Arithmetic

ECE232: Hardware Organization and Design

Lecture 02 C FUNDAMENTALS

BSM540 Basics of C Language

1 class Lecture2 { 2 3 "Elementray Programming" / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch.

5.Coding for 64-Bit Programs

Data Representation Floating Point

COP 3275: Chapter 07. Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA

Floating Point Puzzles. Lecture 3B Floating Point. IEEE Floating Point. Fractional Binary Numbers. Topics. IEEE Standard 754

UNIT 3 OPERATORS. [Marks- 12]

Applied C and C++ Programming

As stated earlier, the declaration

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

UNIT- 3 Introduction to C++

Programming for Engineers Iteration

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

Question 4: a. We want to store a binary encoding of the 150 original Pokemon. How many bits do we need to use?

ECE2049: Embedded Computing in Engineering Design C Term Spring Lecture #3: Of Integers and Endians (pt. 2)

ESc101 : Fundamental of Computing

Physics 306 Computing Lab 5: A Little Bit of This, A Little Bit of That

Time: 8:30-10:00 pm (Arrive at 8:15 pm) Location What to bring:

Data Types and Variables in C language

ECE 2020B Fundamentals of Digital Design Spring problems, 6 pages Exam Two Solutions 26 February 2014

DEPARTMENT OF MATHS, MJ COLLEGE

CS16 Exam #1 7/17/ Minutes 100 Points total

A Fast Review of C Essentials Part I

CS101 Introduction to computing Floating Point Numbers

Floating Point Arithmetic

Agenda. CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language 8/29/17. Recap: Binary Number Conversion

Data Representation Floating Point

CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language. Krste Asanović & Randy Katz

Chapter 2 Float Point Arithmetic. Real Numbers in Decimal Notation. Real Numbers in Decimal Notation

Java Notes. 10th ICSE. Saravanan Ganesh

CS429: Computer Organization and Architecture

Basics of Programming

Engineering Computing I

Up next. Midterm. Today s lecture. To follow

FLOATING POINT NUMBERS

Transcription:

Declaration Fundamental Data Types All variables must be declared before being used. Tells compiler to set aside an appropriate amount of space in memory to hold a value. Enables the compiler to perform operations using the declared variables. 1 2 char int float double void Basic Data Types character integer floating-point double floating-point valueless 3 Modifying the Basic Types Type modifier: signed unsigned long short When a type modifier is used by itself, then int is assumed. Modifier Same As signed signed int unsigned unsigned int long long int short short int 4 Data Type char Have Seen:Chars are treated as small integers & conversely small ints are treated as chars. char c = a ; a printf ( %c, c + 1) printf ( %d, c + 2) 99 Each char variable stored in 1 Byte 8 Bits: 2 7 2 6 2 5 2 4 2 3 2 2 2 1 2 0 128 64 32 16 8 4 2 1 b String of binary digits are called bit strings. A bit string is interpreted as a binary number. b n b n-1 b 2 b 1 b 0 0 1 1 0 0 0 0 1 a This bit string has the value: 1 2 6 + 1 2 5 + 1 2 0 = 97 0 1 1 0 0 0 0 1 5 6 1

3 types: 1) char 2) unsigned char 3) signed char Each uses 1 Byte. Signed char -128 to 127 Unsigned char 0 to 255 Data Type int INTEGERS : Include the natural counting numbers and their negatives. INTEGRAL DATA TYPES: int short long unsigned 7 8 The magnitude of the number that an int variable can hold depends on? word size(bits) of the machine is machine dependent 2 Byte word ~ -32 K To +32K -2 15,, -3, -2, -1, 0, 1, 2, 3,, 2 15-1 Integer overflow: Value too large for defined Storage Location. Typically program continues to run but incorrect results. 4 Byte word ~ -2 Billion To +2 Billion -2 31,. -3, -2, -1, 0, 1, 2, 3,, 2 31-1 9 The programmer must strive at all times to avoid integer overflow. 10 Types short, long, unsigned short - used where conserving storage is a concern (usually 2 Bytes). -32 Thousand To +32 Thousand -2 15, -2 15 +1,, -3, -2, -1, 0, 1, 2, 3,, 2 15-1 long - needed for larger integers (usually 4 bytes). -2 Billion To +2 Billion Unsigned - no sign bit, same number bytes as int. The range, u, of values: 0 u 2 wordsize -1 2 Byte word ~ 0 To + 64 Thousand 0, 1, 2, 3,, 2 16-1 4 Byte word ~ 0 To + 4 Billion 0, 1, 2, 3,, 2 32-1 -2 31, -2 31 +1,, -3, -2, -1, 0, 1, 2, 3,, 2 31-111 12 2

Suffix: u - 20u l 20l ul - 20ul (case not significant) IF no suffix with constant - system will choose the first of : int - long - unsigned long That can hold the value. 13 The Floating Types float double long double Suffixes for constants f or F float 3.7 F l or L long double 3.7 L Any unsuffixed floating constant is of type double (working type). 14 Notation: exponential or decimal Must have Exponent or Dec pt or Both. 1.234567e5 123456.7 1.234567e-3 0.001234567 0e0 correct 0.0.e0 wrong Precision: The number of significant decimal digits that floating value carries. Range: Limits of largest & smallest possible values that can be in a variable of that type. Float: 4 Bytes - about 6 decimal places of accuracy- single precision. May not contain any blanks or special characters. Typical Storage: Float < Double 15 Double: 8 Bytes - about 15 decimal places of accuracy- double precision. 16 FLOAT: Internal Representation 01 8 9 31 SEEEEEEEE mmmmm m Float: Precision: 6 significant digits. Range: 10-38 to 10 +38 0.d 1 d 2 d 3 d 4 d 5 d 6 10 n DOUBLE: 01 11 12 63 SEEEEEEEEEEE mmmmmmm m Implied mantissa of 1 is not stored. Double: Precision: 15 significant digits (2 52 10 15 ) Range: 10-308 to 10 +308 Sign bit (1 for neg-otherwise pos). 17 18 0.123451234512345 10 3 3

Note: 1. Not all Real numbers are exactly representable in binary memory. 2. Floating Arithmetic ops, unlike integer arithmetic, may not be exact. Compile-Time Operator sizeof Unary operator used to find the number of Bytes needed to store an object. sizeof(object) Object Data type int, float, Expression a + b 19 Array Structure Will cover later 20 sizeof(char) = 1 Assuming that integers are 4 bytes and doubles are 8 bytes. sizeof(short) sizeof(int) sizeof(long) sizeof(signed)=sizeof(unsigned)=sizeof(int) double f; printf("%d ",sizeof (f)); 8 printf(''%d", sizeof(int)); 4 sizeof(float) <= sizeof(double ) <= sizeof( long double) 21 22 Conversions When constants and variables of different types are mixed in an, the compiler converts all operands to the type of the largest operand- Called Type Promotion. - First, all char and short values are automatically elevated to int. Called integral promotion. - int + int = int - short + short = int char ch; int i; float f; double d,result; result= (ch / i) + (f * d) (f + i); int double float - Arithmetic Conversion (See pg. 217) double 23 24 4

Casts Casts - Explicit conversions. (type) (float) i/2 If i is int: (float) i will change value to float, i is not changed. 25 Apply to an Expression: (float) ( c + 3) Cannot apply to an Assignment: (int) f = 3: illegal As an unary operator, a cast has the same precedence as any other unary operator. (float) i + 3 ( (float) i ) + 3 26 /* print i and i/2 with fractions */ int main(void) { int i; for(i=l; i<=100; ++i) printf(''%d //2 is: %f \n", i, (float) i /2); return 0; } 27 General forms: (int) char - (char) int - (int) float - (float) int - (double) float exp- ordinal value of char character with the ordinal value of int truncates the float converts int to float converts float to double 28 5