C: Intro to Data Types. Department of Computer Science College of Engineering Boise State University. January 14, /17

Similar documents
Oct: 50 8 = 6 (r = 2) 6 8 = 0 (r = 6) Writing the remainders in reverse order we get: (50) 10 = (62) 8

Pemrograman Dasar. Basic Elements Of Java

AT&T Global Network Client for Windows Product Support Matrix January 29, 2015

Number Representation

Binary Numbers. Binary Octal Hexadecimal

Analysis One Code Desc. Transaction Amount. Fiscal Period

LSN 2 Number Systems. ECT 224 Digital Computer Fundamentals. Department of Engineering Technology

The string of digits in the binary number system represents the quantity

COMPARISON OF FIXED & VARIABLE RATES (25 YEARS) CHARTERED BANK ADMINISTERED INTEREST RATES - PRIME BUSINESS*

COMPARISON OF FIXED & VARIABLE RATES (25 YEARS) CHARTERED BANK ADMINISTERED INTEREST RATES - PRIME BUSINESS*

The programming language C. sws1 1

Numbering Systems. InThisAppendix...

DNA Data and Program Representation. Alexandre David

C++ Language Tutorial

CSI 333 Lecture 1 Number Systems

Sources: On the Web: Slides will be available on:

Numeral Systems. The number twenty-five can be represented in many ways: Decimal system (base 10): 25 Roman numerals:

Variables, Constants, and Data Types

Binary Representation. Number Systems. Base 10, Base 2, Base 16. Positional Notation. Conversion of Any Base to Decimal.

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Today. Binary addition Representing negative numbers. Andrew H. Fagg: Embedded Real- Time Systems: Binary Arithmetic

2011, The McGraw-Hill Companies, Inc. Chapter 3

Solution for Homework 2

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

Case 2:08-cv ABC-E Document 1-4 Filed 04/15/2008 Page 1 of 138. Exhibit 8

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

Lecture 2. Binary and Hexadecimal Numbers

Computer Science 281 Binary and Hexadecimal Review

About The Tutorial. Audience. Prerequisites. Copyright & Disclaimer

6.087 Lecture 2 January 12, 2010

Levent EREN A-306 Office Phone: INTRODUCTION TO DIGITAL LOGIC

NUMBER SYSTEMS. 1.1 Introduction

Chapter Binary, Octal, Decimal, and Hexadecimal Calculations

Binary Representation

CDA 3200 Digital Systems. Instructor: Dr. Janusz Zalewski Developed by: Dr. Dahai Guo Spring 2012

Enhanced Vessel Traffic Management System Booking Slots Available and Vessels Booked per Day From 12-JAN-2016 To 30-JUN-2017

Informatica e Sistemi in Tempo Reale

Objective-C Tutorial

So far we have considered only numeric processing, i.e. processing of numeric data represented

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Chapter 7D The Java Virtual Machine

Chapter 2. Binary Values and Number Systems

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

Verilog - Representation of Number Literals

Useful Number Systems

1. Give the 16 bit signed (twos complement) representation of the following decimal numbers, and convert to hexadecimal:

Ashley Institute of Training Schedule of VET Tuition Fees 2015

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

How To Write Portable Programs In C

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

Goals. Unary Numbers. Decimal Numbers. 3,148 is s 100 s 10 s 1 s. Number Bases 1/12/2009. COMP370 Intro to Computer Architecture 1

C / C++ and Unix Programming. Materials adapted from Dan Hood and Dianna Xu

Systems I: Computer Organization and Architecture

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

CS101 Lecture 11: Number Systems and Binary Numbers. Aaron Stevens 14 February 2011

EE 261 Introduction to Logic Circuits. Module #2 Number Systems

The New IoT Standard: Any App for Any Device Using Any Data Format. Mike Weiner Product Manager, Omega DevCloud KORE Telematics

Arithmetic in MIPS. Objectives. Instruction. Integer arithmetic. After completing this lab you will:

ECE 0142 Computer Organization. Lecture 3 Floating Point Representations

Programming languages C

Number and codes in digital systems

Everything you wanted to know about using Hexadecimal and Octal Numbers in Visual Basic 6

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

Java Interview Questions and Answers

Binary Division. Decimal Division. Hardware for Binary Division. Simple 16-bit Divider Circuit

C++ Programming Language

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

The Answer to the 14 Most Frequently Asked Modbus Questions

Chapter 4: Computer Codes

This Unit: Floating Point Arithmetic. CIS 371 Computer Organization and Design. Readings. Floating Point (FP) Numbers

COMPSCI 210. Binary Fractions. Agenda & Reading

HOMEWORK # 2 SOLUTIO

CS 106 Introduction to Computer Science I

Introduction to Java

Object Oriented Software Design

To convert an arbitrary power of 2 into its English equivalent, remember the rules of exponential arithmetic:

3. Convert a number from one number system to another

CENTERPOINT ENERGY TEXARKANA SERVICE AREA GAS SUPPLY RATE (GSR) JULY Small Commercial Service (SCS-1) GSR

Stacks. Linear data structures

This 3-digit ASCII string could also be calculated as n = (Data[2]-0x30) +10*((Data[1]-0x30)+10*(Data[0]-0x30));

plc numbers Encoded values; BCD and ASCII Error detection; parity, gray code and checksums

Chapter 1: Digital Systems and Binary Numbers

MISRA-C:2012 Standards Model Summary for C / C++

Object Oriented Software Design

C Programming. for Embedded Microcontrollers. Warwick A. Smith. Postbus 11. Elektor International Media BV. 6114ZG Susteren The Netherlands

Programming Languages CIS 443

C Programming Tutorial

Java Types and Enums. Nathaniel Osgood MIT April 25, 2012

Keil C51 Cross Compiler

The C Programming Language course syllabus associate level

Computers. Hardware. The Central Processing Unit (CPU) CMPT 125: Lecture 1: Understanding the Computer

Caml Virtual Machine File & data formats Document version: 1.4

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

2010/9/19. Binary number system. Binary numbers. Outline. Binary to decimal

Computer Programming Tutorial

PROGRAMMING IN C PROGRAMMING IN C CONTENT AT A GLANCE

Binary storage of graphs and related data

Chapter 2: Elements of Java

Transcription:

Department of Computer Science College of Engineering Boise State University January 14, 2017 1/17

Basic data types. There are four basic data types: char one byte character in the local character set (typically, ASCII code) int an integer, in the natural representation of the host machine float single precision floating point double double precision floating point 2/17

Modifiers for The basic int type can be qualified by prefixing short and long. We can use short in place of short int and long in place of long int. The type long double can also be used and represents extended double precision value but it is implementation dependent. unsigned or signed. For example unsigned int would have a range of [0..2 32 1] machine where int is of size 4 bytes.. And signed int would have a range of [ 2 31...2 31 1] 3/17

Data Type Sizes Basic data types. The following are typical sizes but beware that the sizes are machine dependent! short 2 bytes signed int 4 bytes signed (but 2 bytes on some systems... argh! ) long 8 bytes signed (but is 4 bytes on older systems) char 1 byte ASCII code (unlike 2 byte Unicode in Java) float 4 bytes IEEE 754 format (same as in Java) double 8 bytes IEEE 754 format (same as in Java) 4/17

Determining types on a system We can use the sizeof operator to determine the size (in bytes) of any type. C-examples/intro/width.c Here is the output on onyx. [amit@onyx C-examples]: width size of char = 1 size of short = 2 size of unsigned short = 2 size of int = 4 size of unsigned int = 4 size of long = 8 size of unsigned long = 8 size of float = 4 size of double = 8 size of long double = 16 5/17

Range of Data Type Values The header file <limits.h> defines limits on integer types whereas the header file <float.h> defines limits on floating point types. Exercise 2-1 (modified). Write a program to determine the ranges of char, short, int, and long variables, both signed and unsigned, by printing appropriate values from standard headers. Determine the ranges of the various floating-point types. Solution. See example C-examples/intro/range.c. 6/17

Typical ranges for C data types Output of range program on the onyx server. TYPE MIN MAX char -128 127 unsigned char 0 255 short -32768 32767 unsigned short 0 65535 int -2147483648 2147483647 unsigned int 0 4294967295 long -9223372036854775808 9223372036854775807 unsigned long 0 18446744073709551615 float 1.175494e-38 3.402823e+38 double 2.225074e-308 1.797693e+308 long double 3.362103e-4932 1.189731e+4932 7/17

How to store signed integers? (1) Storing unsigned integers is simple in binary. For example, we can use 3 bits to represent the integers 0-7 as follows: bits value bits value 000 0 100 4 001 1 101 5 010 2 110 6 011 3 111 7 Note that a k-bit integer is stored as X k 1 X k 2...X 2 X 1 X 0 where X k 1 is the most significant bit (MSB) and X 0 is the least significant bit (LSB). In general, for an unsigned k-bit number, we can store the range 0 to 2 k 1. 8/17

How to store signed integers? (2) But what about negative numbers?? We could use sign-magnitude representation, where the first bit is the sign (0 for positive, 1 for negative) and the rest is the magnitude. Problems? bits value bits value 000 +0 100-0 001 +1 101-1 010 +2 110-2 011 +3 111-3 Two zeros... Arithmetic operations are complicated... Try 1 + (-1) 9/17

How to store signed integers? (3) The 2 s complement representation solves both problems! Store positive numbers directly in binary For negative numbers, write the number in binary ignoring the sign. Then complement the number by flipping 0 s to 1 s and vice versa. Finally add 1 to get the 2 s complement. Example: Two s complement representation of -3. 011 (3 in binary) 100 (1's complement) + 1 (add 1 to get 2's complement) --- 101 (result) bits value bits value 000 +0 100-4 001 +1 101-3 010 +2 110-2 011 +3 111-1 10/17

How to store signed integers? (4) Note the positive integers always start with a zero and negative integers always start with 1 in the 2 s complement representation! Note that 2 s complement for a k-bit number is the same as subtracting the number from 2 k. This simplifies arithmetic. Try 1 + (-1) 11/17

8-bit two s-complement integers Bits Unsigned value 2 s complement value 0111 1111 127 127 0111 1110 126 126 0000 0010 2 2 0000 0001 1 1 0000 0000 0 0 1111 1111 255-1 1111 1110 254-2 1000 0010 130-126 1000 0001 129-127 1000 0000 128-128 12/17

Other Modifiers const Constant or read-only. Similar to final in Java. static Similar to static in Java but not the same. Here is an example for its use in a C function. It creates a private persistent variable. int foo ( int x) { static int y =0; /* value of y is saved */ y = x + y + 7; /* across invocations of foo */ return y; } The static modifier has another meaning that we will see later. extern. The variable is declared external to the source file. volatile. Ask the compiler to not apply optimizations to the variable. register. Variables declared with qualifier register are (if possible) stored in fast registers of the machine. It can only be used for variables declared inside a function and the address operator & cannot be applied to such variables. 13/17

Constants Use suffix L or l for a long int constant and the suffix U or u for an unsigned constant. int i = 1234; long j = 2147483648 L; unsigned short k = 65535; /* 2^16-1 */ unsigned long m = 123456789 UL; Real numbers are double by default. Use suffix F or f for a float constant and the suffix L or l for long double constant. double x = 1E +25; float y = 1.14 F; Integers can be specified in octal or hexadecimal instead of decimal. A leading 0 in an integer constant means octal; a leading 0x or 0X means hexadecimal. These can have a U or L suffix like other integer constants. int n1 = 037; /* 31 in decimal */ int n2 = 0 x1f ; /* 31 in decimal */ 14/17

Enumeration constants A enumeration is a list of constant integer values. enum bool { false, true }; enum bool flag ; // a Boolean flag variable where the first name in the list has the value 0, the next 1 and so on unless explicit values are specified. if not all values are specified, unspecified values continue the progression from the last specified value. enum escape { BACKSPACE = '\b', TAB = '\t', NEWLINE = '\n', RETURN = '\r'}; enum month { JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC }; /* FEB is 2, MAR is 3 and so on */ Names in different enumerations must be distinct. Values need not be distinct in the same enumeration. 15/17

Type Conversion "Narrower" types can be converted to "wider" type without losing information. E.g., an int can be assigned to a long, a float to a double A "wider" type can be assigned to a "narrower" type without casting but information may be lost. int m; long n = 10000000000; float x; double y = 2 E300 ; m = n; x = y; printf ("% ld %d % le %e\n", n, m, y, x); Note that the compiler gives no warning (even with -Wall flag) on the above. However, using the -Wconversion flag does give a warning. See the example C-examples/intro/conv.c Forced casting works using the following syntax (similar to Java): (type-name) expression 16/17

Reading Assignment and Exercises Read Chapter 2 of the C book (skipping Section 2.9 on bitwise operators for now). Attempt Exercises 2-2, 2-4 and 2-10. 17/17