Princeton University. Computer Science 217: Introduction to Programming Systems. Data Types in C

Similar documents
Princeton University Computer Science 217: Introduction to Programming Systems The C Programming Language Part 1

2/12/17. Goals of this Lecture. Historical context Princeton University Computer Science 217: Introduction to Programming Systems

Princeton University COS 217: Introduction to Programming Systems C Primitive Data Types

Goals of C "" The Goals of C (cont.) "" Goals of this Lecture"" The Design of C: A Rational Reconstruction"

The Design of C: A Rational Reconstruction (cont.)

The Design of C: A Rational Reconstruction (cont.)" Jennifer Rexford!

THE FUNDAMENTAL DATA TYPES

Standard 11. Lesson 9. Introduction to C++( Up to Operators) 2. List any two benefits of learning C++?(Any two points)

Basic Types and Formatted I/O

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

Java Basic Datatypees

Java Notes. 10th ICSE. Saravanan Ganesh

The Design of C: A Rational Reconstruction"

Getting started with Java

Programming. Syntax and Semantics

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

The Design of C: A Rational Reconstruction

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

Pointers (continued), arrays and strings

Pointers (continued), arrays and strings

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

Basic data types. Building blocks of computation

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

The Design of C: A Rational Reconstruction" Jennifer Rexford!

Chapter 6 Primitive types

Reserved Words and Identifiers

The Design of C: A Rational Reconstruction

Chapter 3. Fundamental Data Types

Chapter 2 Elementary Programming

Chapter 4: Computer Codes. In this chapter you will learn about:

Data encoding. Lauri Võsandi

CMPT 125: Lecture 3 Data and Expressions

Exercises Software Development I. 03 Data Representation. Data types, range of values, internal format, literals. October 22nd, 2014

COMP6700/2140 Data and Types

Java Programming Fundamentals. Visit for more.

Visual C# Instructor s Manual Table of Contents

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

Chapter 7. Basic Types

Programming Fundamentals and Methodology. COMP104: C++ Basics

Number Systems Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute of Technology Kharagpur Number Representation

JAVA Programming Fundamentals

Work relative to other classes

CS112 Lecture: Primitive Types, Operators, Strings

Primitive Data Types: Intro

CS149: Elements of Computer Science. Fundamental C++ objects

Declaration and Memory

C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga

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

CSC 1107: Structured Programming

Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. C Data Types

Java Language Basics: Introduction To Java, Basic Features, Java Virtual Machine Concepts, Primitive Data Type And Variables, Java Operators,

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

Chapter 2: Data and Expressions

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

A Java program contains at least one class definition.

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

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

Program Fundamentals

VARIABLES AND CONSTANTS

SWEN-250 Personal SE. Introduction to C

B.V. Patel Institute of BMC & IT, UTU 2014

Variables and Values

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Homework 1 graded and returned in class today. Solutions posted online. Request regrades by next class period. Question 10 treated as extra credit

Lecture 2 Tao Wang 1

Full file at

THE INTEGER DATA TYPES. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

10/9/2012. Computers are machines that process data. assignment in C# Primitive Data Types. Creating and Running Your First C# Program

Number Systems, Scalar Types, and Input and Output

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

Variables and Constants

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

Variables, Constants, and Data Types

Data Types Literals, Variables & Constants

Variables and literals

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Programming with Java

Memory, Data, & Addressing II CSE 351 Spring

Types and Expressions. Chapter 3

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

TCL - STRINGS. Boolean value can be represented as 1, yes or true for true and 0, no, or false for false.

Chapter 2: Introduction to C++

Exercise: Using Numbers

Princeton University Computer Science 217: Introduction to Programming Systems The Design of C

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Chapter 2: Using Data

Integer Representation Floating point Representation Other data types

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

ME240 Computation for Mechanical Engineering. Lecture 4. C++ Data Types

Basic Types, Variables, Literals, Constants

3 The Building Blocks: Data Types, Literals, and Variables

Zheng-Liang Lu Java Programming 45 / 79

Integer Data Types. Data Type. Data Types. int, short int, long int

M1 Computers and Data

Data Types, Literals, Operators

Data Representa5on. CSC 2400: Computer Systems. What kinds of data do we need to represent?

MACHINE LEVEL REPRESENTATION OF DATA

Data Representa5on. CSC 2400: Computer Systems. What kinds of data do we need to represent?

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

Transcription:

Princeton University Computer Science 217: Introduction to Programming Systems Data Types in C 1

Goals of C Designers wanted C to: Support system programming Be low-level Be easy for people to handle But also: Support application programming Be portable Be easy for computers to handle Conflicting goals on multiple dimensions! Result: different design decisions than Java 2

Primitive Data Types integer data types floating-point data types no character data type (use small integer types instead) no character string data type (use arrays of small ints instead) no logical or boolean data types (use integers instead) 3

Integer Data Types Integer types of various sizes: signed char, short, int, long char is 1 byte Number of bits per byte is unspecified! (but in the 21 st century, pretty safe to assume it s 8) Sizes of other integer types not fully specified but constrained: int was intended to be natural word size 2 sizeof(short) sizeof(int) sizeof(long) On CourseLab Natural word size: 8 bytes ( 64-bit machine ) char: 1 byte short: 2 bytes int: 4 bytes (compatibility with widespread 32-bit code) long: 8 bytes What decisions did the designers of Java make? 4

Integer Literals Decimal: 123 Octal: 0173 = 123 Hexadecimal: 0x7B = 123 Use "L" suffix to indicate long literal No suffix to indicate short literal; instead must use cast Examples int: long: short: 123, 0173, 0x7B 123L, 0173L, 0x7BL (short)123, (short)0173, (short)0x7b 5

Unsigned Integer Data Types unsigned types: unsigned char, unsigned short, unsigned int, and unsigned long Conversion rules for mixed-type expressions (Generally, mixing signed and unsigned converts unsigned) See King book Section 7.4 for details 6

Unsigned Integer Literals Default is signed Use "U" suffix to indicate unsigned literal Examples unsigned int: 123U, 0173U, 0x7BU 123, 0173, 0x7B will work just fine in practice; technically there is an implicit cast from signed to unsigned, but in these cases it shouldn t make a difference. unsigned long: 123UL, 0173UL, 0x7BUL unsigned short: (unsigned short)123, (unsigned short)0173, (unsigned short)0x7b 7

Character Data Type The C char type char can hold an ASCII character And should be used when you re dealing with characters: character-manipulation functions we ve seen (such as toupper) take and return char char might be signed or unsigned, but since 0 ASCII 127 it doesn t really matter If you want a 1-byte type for calculation, you might (should?) specify signed char or unsigned char 8

Character Literals single quote syntax: 'a' Use backslash (the escape character) to express special characters Examples (with numeric equivalents in ASCII): 'a' the a character (97, 01100001 B, 61 H ) '\141' the a character, octal form '\x61' the a character, hexadecimal form 'b' the b character (98, 01100010 B, 62 H ) 'A' the A character (65, 01000001 B, 41 H ) 'B' the B character (66, 01000010 B, 42 H ) '\0' the null character (0, 00000000 B, 0 H ) '0' the zero character (48, 00110000 B, 30 H ) '1' the one character (49, 00110001 B, 31 H ) '\n' the newline character (10, 00001010 B, A H ) '\t' the horizontal tab character (9, 00001001 B, 9 H ) '\\' the backslash character (92, 01011100 B, 5C H ) '\'' the single quote character (96, 01100000 B, 60 H ) 9

Strings and String Literals Issue: How should C represent strings and string literals? Rationale: Natural to represent a string as a sequence of contiguous chars How to know where char sequence ends? Store length together with char sequence? Store special sentinel char after char sequence? 10

Strings and String Literals Decisions Adopt a convention String is a sequence of contiguous chars String is terminated with null char ( \0 ) Use double-quote syntax (e.g. "hello") to represent a string literal Provide no other language features for handling strings Delegate string handling to standard library functions Examples 'a' is a char literal "abcd" is a string literal "a" is a string literal How many bytes? What decisions did the designers of Java make? 11

Arrays of characters s H e l l o \0???? p char s[10] = {'H','e','l','l','o',0}; (or, equivalently) char s[10] = "Hello"; char *p = s+2; printf("je%s!", p); prints Jello!

Unicode Back in 1970s, English was the only language in the world [citation needed], so we only needed this alphabet: In the 21 st century, it turns out that there are other people and languages out there, so we need: ASCII: American Standard Code for Information Interchange

Modern Unicode When Java was designed, Unicode fit into 16 bits, so char in Java was 16 bits long. Then this happened: https://xkcd.com/1953/ 14

Unicode and UTF-8 Lots of characters in today s Unicode 100,000+ defined, capacity for > 1 million Can t modify size of char in C Solution: variable-length encoding (UTF-8) Standard ASCII characters use 1 byte Most Latin-based alphabets use 2 bytes Chinese, Japanese, Korean characters use 3 bytes Historic scripts, mathematical symbols, and emoji use 4 bytes This won t be on the exam!

Logical Data Types No separate logical or Boolean data type Represent logical data using type char or int Or any integer type Or any primitive type!!! Conventions: Statements (if, while, etc.) use 0 FALSE, 0 TRUE Relational operators (<, >, etc.) and logical operators (!, &&, ) produce the result 0 or 1 16

Logical Data Type Shortcuts Using integers to represent logical data permits shortcuts int i; if (i) /* same as (i!= 0) */ statement1; else statement2; It also permits some really bad code i = (1!= 2) + (3 > 4); 17

iclicker Question Q: What is i set to in the following code? i = (1!= 2) + (3 > 4); A. 0 B. 1 C. 2 D. 3 E. 4

Logical Data Type Dangers The lack of a logical data type hampers compiler's ability to detect some errors with certainty int i; i = 0; if (i = 5) statement1; What happens in Java? What happens in C? 19

Floating-Point Data Types C specifies: Three floating-point data types: float, double, and long double Sizes unspecified, but constrained: sizeof(float) sizeof(double) sizeof(long double) On CourseLab (and on pretty much any 21 st -century computer using the IEEE standard) float: 4 bytes double: 8 bytes long double: 16 bytes (but only 10 bytes used on x86-64) 20

Floating-Point Literals fixed-point or scientific notation Any literal that contains decimal point or "E" is floating-point The default floating-point type is double Append "F" to indicate float Append "L" to indicate long double Examples double: float: long double: 123.456, 1E-2, -1.23456E4 123.456F, 1E-2F, -1.23456E4F 123.456L, 1E-2L, -1.23456E4L 21

Data Types Summary: C vs. Java Java only boolean, byte C only unsigned char, unsigned short, unsigned int, unsigned long Sizes Java: Sizes of all types are specified, and portable C: Sizes of all types except char are system-dependent Type char Java: char is 2 bytes (to hold all 1995-era Unicode values) C: char is 1 byte 22