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.

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.
Sizes of simple data types sizeof(char) = 1 size(short) = 2 sizeof(int) = 4 size(long) = 8 sizeof(char) = 1 size(short) = 2 sizeof(int) = 2 size(long)
1 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
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 Chapter 6 The Fundamental Data Types. 2 Outline  Declarations and expressions  Fundamental data types  Characters and the data type char  The Data.
Overview Order of presentation different than published handouts Run a program on ccc Finish Arithmetic operations Data types integer char floating point.
1 9/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
Introduction to C Programming CE Lecture 2 Basics of C programming.
Fundamental data types
Basic C Programming Data Types and Arithmetic Operations 01/30/15.
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.
CS180 Recitation 3. Lecture: Overflow byte b; b = 127; b += 1; System.out.println("b is" + b); b is -128 byte b; b = 128; //will not compile! b went out.
Data types and variables
The fundamental data type 제 4 주 강의. Declarations, Expressions, and Assignments Declaring the type of a variable  set an appropriate amount of space in.
CS150 Introduction to Computer Science 1
Chapter 2 Data Types, Declarations, and Displays
Representation and Conversion of Numeric Types 4 We have seen multiple data types that C provides for numbers: int and double 4 What differences are there.
1 Review of Chapter 6: The Fundamental Data Types.
Variables, Data Types and I/O in C
Chapter 2 Data Types, Declarations, and Displays.
Objectives You should be able to describe: Data Types
Computer Science 111 Fundamentals of Programming I Number Systems.
UniMAP Sem2-08/09 DKT121: Fundamental of Computer Programming1 Introduction to C – Part 2.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Chapter 7 Simple Date Types J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
COMP 116: Introduction to Scientific Programming Lecture 28: Data types.
Sales person receive RM200/week plus 9% of their gross sales for that week. Write an algorithms to calculate the sales person’s earning from the input.
UniMAP Sem1-07/08EKT120: Computer Programming1 Week2.
Lecture #5 Introduction to C++
CISC105 – General Computer Science Class 9 – 07/03/2006.
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Data Type. Syntax Rules Recap keywords breakdoubleifsizeofvoid caseelseintstatic..... Identifiers not#me123th scanfprintf _idso_am_igedd007 Constant ‘a’‘+’
CS115 FALL Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3.
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
Data Types Declarations Expressions Data storage C++ Basics.
Introducing constants, variables and data types March 31.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
Pointers *, &, array similarities, functions, sizeof.
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
Lecture 3 Introduction to Computer Programming CUIT A.M. Gamundani Presentation Layout from Lecture 1 Background.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 1.
Variables Symbol representing a place to store information
Module B - Computation1/61 Module-B-Computation Variables Basic Memory Operations Expressions.
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.
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.
UNIMAP Sem2-07/08EKT120: Computer Programming1 Week 2 – Introduction to Programming.
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.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
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.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
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.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Floating Point Numbers
CSE 220 – C Programming Bitwise Operators.
Tokens in C Keywords Identifiers Constants
7. BASIC TYPES.
Fundamental Data Types
Beginning C Lecture 2 Lecturer: Dr. Zhao Qinpei
Lectures on Numerical Methods
Basic Types Chapter 7 Copyright © 2008 W. W. Norton & Company.
Fundamental Data Types
Data Type.
Module 2 Variables, Data Types and Arithmetic
DATA TYPES There are four basic data types associated with variables:
Programming Fundamental-1
Presentation transcript:

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 in memory to hold values associated with variables. –Enables the compiler to perform operations using the declared variables.

3 Basic Data Types charcharacter intinteger floatfloating-point doubledouble floating-point voidvalueless

4 Modifying the Basic Types Type modifier: signed unsigned longshort When a type modifier is used by itself, then int is assumed. ModifierSame As signed signed int unsignedunsigned int longlong int shortshort int The char type can be modified: char signed char unsigned char

5 Data Type char Have Seen:Chars are treated as small integers & conversely small ints are treated as chars. char c = ‘a’; printf (“%c”, c + 1)b printf (“%d”, c + 2)99 Each char variable stored in 1 Byte 8 Bits: a

6 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 a This bit string has the value 1    2 0 = 97

7 3 types: 1) char2) unsigned char 3) signed char Each uses 1 Byte. Typically char is equivalent to either signed or unsigned char. Signed char -128 to 127 Unsigned char 0 to 255

8 INTEGERS : Include the natural counting numbers and their negatives. INTEGRAL DATA TYPES: int short long unsigned Data Type int

9 The magnitude of the number that an int variable can hold depends on ___________? The range, u, of values: 2 wordsize-1  u  2 wordsize Byte word ~ -32 Thousand To +32 Thousand -2 15, , …, -3, -2, -1, 0, 1, 2, 3, …, Byte word ~ -2 Billion To+2 Billion -2 31, , …, -3, -2, -1, 0, 1, 2, 3, …, word size(bits) of the machine is machine dependent

10 Integer overflow: –Value too large for defined Storage Location. –Typically program continues to run but incorrect results. –The programmer must strive at all times to avoid integer overflow.

11 Types short, long, unsigned short - used where conserving storage is a concern (usually 2 Bytes). -32 Thousand To +32 Thousand -2 15, , …, -3, -2, -1, 0, 1, 2, 3, …, long - needed for larger integers (usually 4 bytes). -2 Billion To+2 Billion -2 31, , …, -3, -2, -1, 0, 1, 2, 3, …,

12 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, …, Byte word ~ 0 To +4 Billion 0, 1, 2, 3, …,

13 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.

14 floatdouble long double Hold Real Values. Suffixes for constants f or Ffloat3.7 F l or Llong double 3.7 L Any unsuffixed floating constant is of type double (working type). The Floating Types

15 Notation: exponentialdecimal Must have Exponent or Dec pt or Both e e e0 correct 0.0.e0 wrong May not contain any blanks or special characters. Typical Storage: Float < Double

16 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. Double: 8 Bytes - about 15 decimal places of accuracy- double precision.

17 Internal Representation FLOAT: SEEEEEEEE FFFFFF…FF DOUBLE: SEEEEEEEEEEE FFFFFFFF…FFF

18 Float: Precision: 6 significant digits. Range: to d 1 d 2 d 3 d 4 d 5 d 6  10 n d i is a significant digit, N is range. Double: Precision: 15 significant digits (2 52  ) Range: to ( to )  10 3 (15 sig. digits)

19 Note: 1. Not all Real numbers are exactly representable in binary memory. 2. Floating Arithmetic ops, unlike integer arithmetic, may not be exact.

20 Unary operator used to find the number of Bytes needed to store an object. sizeof(object) Object –Data typeint, float, … –Expressiona + b Array Will cover later Structure Compile-Time Operator sizeof

21 Assuming that integers are 4 bytes and doubles are 8 bytes. double f; printf("%d ",sizeof (f)); 8 printf(''%d", sizeof(int)); 4

22 Sizeof primarily helps to generate portable code that depends upon the size of the built-in data types. /* Write 6 integers to a disk file. */ void put_rec(int rec[6], FILE *fp) { int len; len = write(fp, rec, sizeof(int)*6); if(len != 1) printf(''Write Error"); }0

23 sizeof(char) = 1 sizeof(short)  sizeof(int)  sizeof(long) sizeof(signed) = sizeof(unsigned) = sizeof(int) sizeof(float)  sizeof(double)  sizeof(long double)

24 Conversions When constants and variables of different types are mixed in an expression, 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 -Arithmetic Conversion (See pg. 217)

25 char ch; int i; float f; double d; result= (ch / i) + (f * d) – (f + i); int double float double

26 Casts - Explicit conversions. (type) expression If i is int: (float) i will change expression value to float, i is not changed. Casts (float) i/2

27 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

28 /* 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; }

29 General forms: (int) char expression-ordinal value of char expression (char) int expression-character with the ordinal value of int expression (int) float expression-truncates the float expression (float) int expression- converts int to float (double) float exp-converts float to double