Presentation is loading. Please wait.

Presentation is loading. Please wait.

ECE 103 Engineering Programming Chapter 8 Data Types and Constants

Similar presentations


Presentation on theme: "ECE 103 Engineering Programming Chapter 8 Data Types and Constants"— Presentation transcript:

1 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 PSU ECE

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

3 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

4 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

5 Fundamental C data types
char : character → single alphanumeric text (e.g., 'A' , '$' , '7') → numeric integer value (e.g to 127) int : integer → numeric integer value float : single-precision floating point → numeric real value (e.g. -1.0, 0.0, , e8) 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

6 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 → e-44 (approx.) 31 5

7 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

8 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 unsigned int float approx -1038 approx 1038 double 64 approx approx 10308 7

9 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 : 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

10 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

11 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

12 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

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

14 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

15 "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


Download ppt "ECE 103 Engineering Programming Chapter 8 Data Types and Constants"

Similar presentations


Ads by Google