ECE 103 Engineering Programming Chapter 8 Data Types and Constants

Slides:



Advertisements
Similar presentations
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Advertisements

Constants and Data Types Constants Data Types Reading for this class: L&L,
Types and Variables. Computer Programming 2 C++ in one page!
Constants Variables change, constants don't final = ; final double PI = ; … area = radius * radius * PI; see Liang, p. 32 for full code.
Chapter 2: Introduction to C++.
Fundamental data types Horstmann Chapter 4. Constants Variables change, constants don't final = ; final double PI = ; … areaOfCircle = radius *
String Escape Sequences
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Basic Elements of C++ Chapter 2.
CS 161 Introduction to Programming and Problem Solving Chapter 13 Console IO Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from.
ECE 103 Engineering Programming Chapter 10 Variables, AKA Objects Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103.
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
Input & Output: Console
System development with Java Lecture 2. Rina Errors A program can have three types of errors: Syntax and semantic errors – called.
C-Language Keywords(C99)
Constants in C A Presentation On Department of Computer & Information Technology, M.S.P.V.L. Polytechnic College, Pavoorchatram.
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
CSE 1301 Lecture 2 Data Types Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Copyright © – Curt Hill Types What they do.
C++ Basics Tutorial 5 Constants. Topics Covered Literal Constants Defined Constants Declared Constants.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
CS 2130 Lecture 23 Data Types.
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
Primitive Data Types 1 In PowerPoint, point at the speaker icon, then click the "Play" button.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
ECE 103 Engineering Programming Chapter 3 Numbers Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Basic Data Types อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 4.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 2.
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
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.
PRIMITIVE TYPES IN JAVA Primitive Types Operations on Primitive Types.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.
Chapter # 2 Part 2 Programs And data
Chapter Topics The Basics of a C++ Program Data Types
The Machine Model Memory
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
ECE Application Programming
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Computing Fundamentals
Chapter 2 Data Types and Representations
Basic Elements of C++.
TMF1414 Introduction to Programming
Variables and Primative Types
ECE 103 Engineering Programming Chapter 3 Numbers
Basic Elements of C++ Chapter 2.
C++ Basics.
Introduction to Java Programming
C++ Data Types Data Type
Program Breakdown, Variables, Types, Control Flow, and Input/Output
Chapter # 2 Part 2 Programs And data
Chapter 2: Introduction to C++.
C++ Programming Lecture 3 C++ Basics – Part I
ECE 103 Engineering Programming Chapter 12 More C Statements
ECE 103 Engineering Programming Chapter 25 C Strings, Part 1
ECE 103 Engineering Programming Chapter 46 argc, argv, envp
Chapter 2 Primitive Data Types and Operations
EECE.2160 ECE Application Programming
Variables and Constants
Presentation transcript:

ECE 103 Engineering Programming Chapter 8 Data Types and Constants Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed by Professor Phillip Wong @ PSU ECE

Syllabus What’s This Blue Code? Data Types Intro to Char Constant Numeric Constants Char Constant String Constant

What’s This Blue Code? #include <stdio.h> // for printf() // return smaller of ints a and b int min( int a, int b ) { // min if ( a < b ) { return a; }else{ return b; } //end if } //end min int main( /* no params */ ) { // main printf( ”min(-12,100) = %d\n”, min( -12, 100 ) ); return 0; } //end main

Data Types Data is stored on a computer as a sequence of bits These bits have no intrinsic meaning until given a context by specifying the type of the value The type specifier determines: How the data should be interpreted Amount of memory reserved for storing the data Explicitly declaring the type allows the compiler to enforce type conversions 3

Fundamental C data types char : character → single alphanumeric text (e.g., 'A' , '$' , '7') → numeric integer value (e.g. -128 to 127) int : integer → numeric integer value float : single-precision floating point → numeric real value (e.g. -1.0, 0.0, 3.14, -5.1e8) double : double-precision floating point → numeric real value (extra precision/range) No string type → is implemented as array of characters Scientific notation m×10 p is expressed in C as mep Therefore: -5.1e8 = -5.1×108 4

Interpretation of a value depends on its type Example: What is this 32-bit value in decimal? If the type is integer → 65 (decimal) If the type is char → 'A’ (low 8 bits, rest ignored) If the type is float → 9.1084400e-44 (approx.) 31 00000000000000000000000001000001 5

Qualifiers modify basic data types short int : short integer long int : long integer long double : extended-precision floating point char and int types can be signed or unsigned signed char, unsigned char signed int, unsigned int signed short int, unsigned short int signed long int, unsigned long int 6

Minimum and maximum values of type are HW and compiler dependent! GNU GCC running under Cygwin on a Microsoft Windows system Data Type Size (bits) Min Value Max Value char, signed char 8 -128 127 unsigned char 255 short int, signed short int 16 -32768 32767 unsigned short int 65535 int, signed int 32 -2147483648 2147483647 unsigned int 4294967295 float approx -1038 approx 1038 double 64 approx -10308 approx 10308 7

Intro to Char Constant Advise to name C “constants” C “literals”, since a variable not being modified is also constant The char data type can hold a restricted range of integer values: char : -128 to +127 –being mapped unsigned char : 0 to 255 The values from 0 to 127 can be mapped to a set of characters (digits, letters, punctuation) A common 7-bit character map is ASCII A char variable can be interpreted as either an integer number or as a character 8

ASCII character to decimal integer code Example: '$' ↔ 36 '2' ↔ 50 'A' ↔ 65 'z' ↔ 122 Note: A pair of single quotes define a character value. Dec Hex Char 00 Null 32 20 Space 64 40 @ 96 60 ‘ 1 01 Start of heading 33 21 ! 65 41 A 97 61 a 2 02 Start of text 34 22 " 66 42 B 98 62 b 3 03 End of text 35 23 # 67 43 C 99 63 c 4 04 End of transmit 36 24 $ 68 44 D 100 d 5 05 Enquiry 37 25 % 69 45 E 101 e 6 06 Acknowledge 38 26 & 70 46 F 102 f 7 07 Audible bell 39 27 ' 71 47 G 103 g 8 08 Backspace 28 ( 72 48 H 104 h 9 09 Horizontal tab 29 ) 73 49 I 105 i 10 0A Line feed 2A * 74 4A J 106 6A j 11 0B Vertical tab 2B + 75 4B K 107 6B k 12 0C Form feed 2C , 76 4C L 108 6C l 13 0D Carriage return 2D - 77 4D M 109 6D m 14 0E Shift out 2E . 78 4E N 110 6E n 15 0F Shift in 2F / 79 4F O 111 6F o 16 Data link escape 30 80 50 P 112 p 17 Device control 1 31 81 51 Q 113 q 18 Device control 2 82 52 R 114 r 19 Device control 3 83 53 S 115 s Device control 4 84 54 T 116 t Neg. acknowledge 85 55 U 117 u Synchronous idle 86 56 V 118 v End trans. block 87 57 W 119 w Cancel 88 58 X 120 x End of medium 89 59 Y 121 y 1A Substitution 3A : 90 5A Z 122 7A z 1B Escape 3B ; 91 5B [ 123 7B { 1C File separator 3C < 92 5C \ 124 7C | 1D Group separator 3D = 93 5D ] 125 7D } 1E Record separator 3E > 94 5E ^ 126 7E ~ 1F Unit separator 3F ? 95 5F _ 127 7F □ 9

Additional Data Types C99 C99 data type Description _Bool Boolean logic value: true (1) or false (0) Optional: #include <stdbool.h> Defines macros: bool (same as _Bool), true, false long long int signed or unsigned integer that is at least 64 bits wide /* C90 version */ #define TRUE 1 #define FALSE 0 int main (void) { int state = TRUE; return 0; } // C99 version 1 #define TRUE 1 #define FALSE 0 int main (void) { _Bool state = TRUE; return 0; } // C99 version 2 #include <stdbool.h> int main (void) { bool state = true; return 0; } 10

For floating point constants Numeric Constant A constant is a value that is fixed at compile-time and can’t be changed during program execution. A suffix can be appended to a literal numeric constant to designate its data type. For integer constants For floating point constants Suffix Data Type Example none int 123 L or l long 123L LL or ll long long 123LL U or u unsigned 123u UL or ul unsigned long 123ul Suffix Data Type Example none double 1.23 F or f float 1.23f L or l long double 1.23L C99 11

Bases 8, 10, 16 for numeric constants in C Use no prefix for base 10 (decimal) Example: 3868 → 386810 Prefix the constant with 0 (zero) for base 8 07434 → 74348 → 386810 Prefix the constant with 0x for base 16 0xF1C → F1C16 → 74348 → 386810 12

Char Constant A char constant is a single character surrounded by a pair of single (not double) quotes. Example: 'x' An escape sequence is the \ character followed by an additional character. Escape sequences perform special actions, such as starting a new line ('\n') or tabbing ('\t') . The combination is considered a single unit. Example: '\n' is one character in length. 13

"x" is a string that contains two characters String Constant C has no string data type A string literal is a sequence of characters embedded in double quotes, “. . .” Example: "x" "Hello, world!" A string is terminated by the NULL character ('\0'). Example: 'x' is not equivalent to "x". 'x' is a single character. "x" is a string that contains two characters 'x' 120 or 'x' '\0' or 120 14