Chapter 2 Simple Data Types

40
Chapter 2 Simple Data Types By C. Shing ITEC Dept Radford University

description

Chapter 2 Simple Data Types. By C. Shing ITEC Dept Radford University. Objectives. Understand How to declare a variable Describe C simple data types Understand C expressions, operators and their priority level Understand how to use format output (printf) - PowerPoint PPT Presentation

Transcript of Chapter 2 Simple Data Types

Page 1: Chapter 2 Simple Data Types

Chapter 2 Simple Data Types

By C. Shing

ITEC Dept

Radford University

Page 2: Chapter 2 Simple Data Types

Slide 2

Objectives Understand How to declare a variable Describe C simple data types Understand C expressions, operators

and their priority level Understand how to use format output (printf) Understand how to use keyboard input: scanf Understand how to design a small pogram

Page 3: Chapter 2 Simple Data Types

Slide 3

Variable Declaration Declaration form:

Type identifier [= initial value];

Note: In ANSC C, you must declare all variables

before using statements

Identifier: begins with a letter or underscore

Begins with underscore: for system Begins with letter: for user

The rest either letter, digit or underscore Up to 255 characters

Page 4: Chapter 2 Simple Data Types

Slide 4

Types Simple data type

Modifier: (signed, unsigned), (long, short) except on char char, int (default), float, double Sizes are system dependent

int (16 or 32 bots), float (32 or 64 bits) Use sizeof (type) operator to find the data type size sizeof (char)=1<=sizeof (short)<=sizeof (int)<=sizeof (long) sizeof (signed)=sizeof (unsigned)=sizeof (int) sizeof (float) <= sizeof (double)<=sizeof (long double)

Structured data type char[] (array, string), type * (pointer) struct (record)

Page 5: Chapter 2 Simple Data Types

Slide 5

Simple Data TypesType Size (bit) Range

unsigned char 8 0~255

char, signed char 8 -128~127

unsigned int (16 or ) 32 0~2^32 -1

int, signed int (16 or) 32 -2^31 ~2,147,483,647

unsigned short int 16 0~65535

short int, signed short int

16 -32768~32767

Page 6: Chapter 2 Simple Data Types

Slide 6

Simple Data TypesType Size (bit) Range

float 32 0.xxxxxxE-38 ~ 0.xxxxxxE+38,6 significant digits

double 64 E-308~E+308,15 significant digits

long int (32 or)64 -2^63~ 2^63-1

long long int 64 Same as above (by C99)

unsigned long int

(32 or)64 0~2^64-1

Page 7: Chapter 2 Simple Data Types

Slide 7

Initial Values char: ‘A’, ‘\a’ (or ‘\7’,’\07’,’\007’ alert), ‘\\’,

‘\b’ (backspace), ‘\r’ (carriage return),

’\”’, ‘\f’ (form feed), ‘\t’, ‘\n’, ‘\0’ (null), ‘\’’,

‘\v’ (vertical tab), ‘\?’ all characters are stored as ASCII integers No constants of character type in C

(However, yes in C++) int: with or without sign

Decimal: -3 Octal: 023 Hexadecimal: 0x123

Page 8: Chapter 2 Simple Data Types

Slide 8

Initial Values (Cont.)

long: 1234567L float: 0.123F double:

Any number with decimal point is treated as

double, e.g. 0.123 Number in scientific notation, e.g. -123.45e-

1

Page 9: Chapter 2 Simple Data Types

Slide 9

Expression Combination of variables, constants

and operators

Example: x-1 > 5 is an expression

Page 10: Chapter 2 Simple Data Types

Slide 10

Operators Increment and decrement: Prefix and postfix

++, -- Arithmetic

*, /, %, +, - Relational

==, !=, >, <, >=, <= (no space in between) Logic

! (not), && (and), || (or) Conditional

Expr1 ? expr2 : expr3 Comma (used in variable declaration)

Page 11: Chapter 2 Simple Data Types

Slide 11

Operators – Increment and Decrement

++ variable/ -- variable Increment(or decrement) variable by 1

before evaluating the current statement variable ++/ variable –

Increment(or decrement) variable by 1

after evaluating the current statement

Page 12: Chapter 2 Simple Data Types

Slide 12

Operators - Arithmetic / : quotient of 2 integers division

Example:

5/9=0

9/5=1

Note: in order to get the desired result of 5/9,

Use either one below:

5.0/9, 5/9.0. 5.0/9.0

Page 13: Chapter 2 Simple Data Types

Slide 13

Operators – Arithmetic (Cont.) %: remainder of 2 integers division

If negative integer is in exactly one of numerator

or denominator, then do division as in all positive

Numbers and just add a negative sign in front of

the result. For example: (-40)/11=- (40/11)=-3

Note: Do not use negative numbers in % since it is

implementation dependent.

Page 14: Chapter 2 Simple Data Types

Slide 14

Operators - Relational Do not compare float or double number

using == or !=

Example:

double x=5.0;

if (x == 5.0)

{

printf (“x=5.0\n”); // this line may not be

//printed due to round off error

}

Page 15: Chapter 2 Simple Data Types

Slide 15

Operators - Logic

Example:

int x=2;

printf (“%d\n”, 1<x<3); // 1 is printed

Page 16: Chapter 2 Simple Data Types

Slide 16

Operators PriorityOperator Same Priority Rule

(), postfix++/-- Left to right

Unary +/-, prefix++/--, ! Right to left

*, /, % Left to right

+, - Left to right

<, >, <=, >= Left to right

==, != Left to right

&& Left to right

|| Left to right

?: Right to left

Assignment operator Right to left

, Left to right

Page 17: Chapter 2 Simple Data Types

Slide 17

Example Given a=2, b=-3, c=5, d=-7, e=11(1) a/b/c=(2) 7+c* -- d/e = (3) 2* a%-b+c+1=(4) 7+ ++a%4=

Page 18: Chapter 2 Simple Data Types

Slide 18

Example Given a=2, b=-3, c=5, d=-7, e=11(1) a/b/c=(2/-3/5)=(-(2/3))/5=(-0)/5=-(0/5)=-0=0(2) 7+c* -- d/e = 7+5*(-8)/11=7+(-40)/11=7+(-(40/11))=7+(-3)=4(3) 2* a%-b+c+1=2*2%3+5+1=4%3+5+1=1+5+1=7(4) 7+ ++a%4=7+3%4=7+3=10

Page 19: Chapter 2 Simple Data Types

Slide 19

Operators Exercises:Given int a=1,b=2,c=3,d=4; Find the values of the following table: Expression Value ________ _____ a*b/c a*b%c+1 ++a*b-c7 - - b * ++ d a/b/ca/b % c'A'+1 ('A'+1) < ba>b && c<da< ! b || ! ! ‘A’a + b < !c + ca - d || b*c && b/a

Page 20: Chapter 2 Simple Data Types

Slide 20

Operators Exercises: AnswerGiven int a=1,b=2,c=3,d=4; Find the values of the following table: Expression Value ________ _____ a*b/c 0a*b%c+1 3++a*b-c 17 - - b * ++ d 17a/b/c 0a/b % c 0'A'+1 66('A'+1) < b 0a>b && c<d 0a< ! b || ! ! ‘A’ 1a + b < !c + c 0a - d || b*c && b/a 1

Page 21: Chapter 2 Simple Data Types

Slide 21

Format Output - printf Form:

printf(“%modifier format_code characters”,

variables);

Example:

printf(“%6d\t%d\n”,number1, number2);

Page 22: Chapter 2 Simple Data Types

Slide 22

Format Output – printf (Cont.)Format Code Explain

%d, %i Signed decimal

%u Unsigned decimal

%f Floating point

%e, %E Scientific notation (lower/upper case e)

%g, %G Shorter of either %f or %e (%f or %E for exponent < -4)

%s string

Page 23: Chapter 2 Simple Data Types

Slide 23

Format Output – printf (Cont.)Format Code Explain

%x, %X unsigned hexadecimal (lower/upper case output)

%o Unsigned octal

%c character

%p pointer

%% Print a % sign

Page 24: Chapter 2 Simple Data Types

Slide 24

Format Output – printf (Cont.)Format Code Explain

%n Number of characters up to %n, argument must be a pointer to an integer

%lf Used for a double

%a, %A Hexadecimal form 0xy.yyyyP+y (C99 only)

Page 25: Chapter 2 Simple Data Types

Slide 25

Format Output – printf (Cont.) Modifier:

printf(“%modifier format_code characters”, variables);

-: left justified +: display + for positive, - for negative value 0m: padded with leading 0s when less than m digits

printed n(integer): min number of spaces

Page 26: Chapter 2 Simple Data Types

Slide 26

Format Output – printf (Cont.) Modifier : (Cont.)

p.q:min: %f, %e, %E:

min total p spaces, q digits after decimal point %g, %G:

min total p spaces, q significant digits %s:

At least p total spaces, at most q spaces of the string %d:

At least p spaces, min q digits displayed; padded with 0 if not enough digits

#: hexadecimal with 0x prefix *.*: specify min spaces and # of digits after decimal

point

Page 27: Chapter 2 Simple Data Types

Slide 27

Format Output – printf (Cont.) Examples:

printf (“%s\n”, “How are you?”); printf (“Beep me!%c\n”,’\a’); printf (“A right-justified number: %6d”, 5); printf (“A left-justified number: %-6d”, 5); printf (“%6.2f\n”, 12.3F); printf (“%*.*f\n”, 6, 2, 12.3F); printf (“%6.2lf\n”, 1.236); printf (“%#x\n”, 123); printf(“%2.6d\n”, 123); printf(“%2.4s\n”, “Morning”);

Page 28: Chapter 2 Simple Data Types

Slide 28

Format Output – printf (Cont.) Examples:

printf (“%s\n”, “How are you?”); // How are you? Is printed printf (“Beep me!%c\n”,’\a’); printf (“A right-justified number: %6d”, 5); // 5 is printed printf (“A left-justified number: %-6d”, 5); // 5 is printed printf (“%6.2f\n”, 12.3F); // 12.30 is printed printf (“%*.*f\n”, 6, 2, 12.3F); // similar to above printf (“%6.2lf\n”, 1.236); // 1.24 is printed printf (“%#x\n”, 123); // 0x7b is printed printf(“%2.6d\n”, 123); //000123 is printed printf(“%2.4s\n”, “Morning”); //Morn is printed

Page 29: Chapter 2 Simple Data Types

Slide 29

Format Output – printf (Cont.) Examples:

Hello World Example

Page 30: Chapter 2 Simple Data Types

Slide 30

Format Output – printf (Cont.) Practice:Given int i=123; double x=0.123456789;Show what to be printed? statement print out ________ _______ printf("%d",i) printf("%05d",i) printf("%7o",i) printf("%-9X",i) printf("%-#9x",i) printf("%10.5f",x) printf("%-12.5e",x)

Page 31: Chapter 2 Simple Data Types

Slide 31

Format Output – printf (Answer) Practice:Given int i=123; double x=0.123456789;Show what to be printed? statement print out ________ _______ printf("%d",i) 123printf("%05d",i) 00123printf("%7o",i) 0173 printf("%-9X",i) 0X7B printf("%-#9x",i) 0x7b printf("%10.5f",x) 0.12346 printf("%-12.5e",x) 1.23457e-01

Page 32: Chapter 2 Simple Data Types

Slide 32

Keyboard Input Format Function - scanf Form: scanf(“format”, variable_address);

Format: similar to those used in printf

Example:char c;int i;double db;

scanf(“%c%d%lf”, &c, &i, &db); // sample data:a 100 -1.23 This is a sample data// c=‘a’, i=100, db=-1.23

Page 33: Chapter 2 Simple Data Types

Slide 33

Small Program Design

1. Comment: describe assumption,

given input data, and output result.

Write the algorithm how to achieve the output

using the input

2. Define variables (use meaningful names

and data types) that used to store input,

output and intermediate results

Page 34: Chapter 2 Simple Data Types

Slide 34

Small Program Design (Cont.)

3. Input data: either store initial values

in variables, get data from files or

use keyboard input

4. Process data: use expression to calculate

Results and store them in variables

5. Output result: display results in screen

or store them in files

Page 35: Chapter 2 Simple Data Types

Slide 35

Small Program Design (Cont.)Example:/* Program Purpose:

assumption:input:output:algorithm:Programmer’s Name:

*/

Page 36: Chapter 2 Simple Data Types

Slide 36

Small Program Design (Cont.)Example: (Cont.)int main(void) {

double celsius, fahrenheit=…; // input

celsius= …; // process

printf (“…..”); // output

return 0;}

Page 37: Chapter 2 Simple Data Types

Slide 37

Small Program Design (Cont.) Example:

Yard-Meter Conversion

Page 38: Chapter 2 Simple Data Types

Slide 38

Class Example Example 1 Example 2 Example 3 Example 4

Page 39: Chapter 2 Simple Data Types

Slide 39

Class Example (Cont.) Use scanf function to read data from keyboard:

Example 1 Use redirect input (<) /output (>) from/to file:

a.out < inputdata.txt > output.txtinputdata.txtoutput.txt

Use “for loop”:Example 1

Interchange DOS and Unix format: dos2unix, unix2dos

dos2unix dosformatfile.c unixformatfile.c

Page 40: Chapter 2 Simple Data Types

Slide 40

References Herbert Schildt: C: The Complete Reference,

4th ed, Osborne Publishing Deitel & Deitel: C How to Program, 4th ed.,

Chapter 2 & 9, Prentice Hall