46
Basic Type (2) Program Design (I) 2021 Fall Fu-Yin Cherng Dept. CSIE, National Chung Cheng University 1

W8 - Basic Type (2)

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Basic Type (2)Program Design (I)

2021 Fall Fu-Yin Cherng

Dept. CSIE, National Chung Cheng University

1

REGISTRATION OF THE TALK "THINKING STRATEGY OF CS PEOPLE" AT 11/25 (THUR.)

● We have invited the podcasters from "上學之Why" to share their experience in learning computer science. They will also share the experience of education and working in the US. Please see the poster below for more information.

● This talk will be given in Mandarin, but the presenters are welcome to any question and discussion in English

● Please go to check the announcement in eCourse2

2

No Quiz Section next week

● So you can have more time to prepare the midterm exam● You will demo the program exercise of this week in the week after the

midterm exam (Nov. 15)

3

Outline

● Character Type● Type Conversion● Type Definitions (Ch 7.5)● The sizeof Operator (Ch 7.6)● 7.5 and 7.6 will not included in midterm exam

4

5https://justdocodings.blogspot.com/2018/03/c-data-types.html

long long intlong long int

Character Types

● The only remaining basic type is char, the character type.● The values of type char can vary from one computer to another, because different

machines may have different underlying character sets.

6

Character Types

● Today’s most popular character set is ASCII○ American Standard Code for Information Interchange

● a 7-bit code capable of representing 128 (27) characters.● ASCII is often extended to a 256-character code known as Latin-1 that provides the

characters necessary for Western European and many African languages.

7

7-bit code

Are there other character set?

● Yes!● For other langauges (like Chinese), ASCII code (128 (27) characters) is not enough.● Unicode (or Universal Coded Character Set) Transformation Format (UTF)● UTF-8, consists of 8 bits● UTF-16, consists of 16 bits

Extra reading

● https://www.itread01.com/content/1548441494.html

8https://en.wikipedia.org/wiki/Character_encoding

嚴 E4B8A5 (UTF-8)

Character Sets

● A variable of type char can be assigned any single character○ use char to declare a character variabel

● Notice that character constants are enclosed in single quotes, not double quotes.

9

char ch;

ch = 'a'; /* lower-case a */

ch = 'A'; /* upper-case A */

ch = '0'; /* zero */

ch = ' '; /* space */

Operations on Characters

● Working with characters in C is simple, because of one fact: C treats characters as small integers (7-bit integer).

● In ASCII, character codes range from 0000000 to 1111111, which we can think of as the integers from 0 to 127.

● For example, The character 'a' has the value 97, 'A' has the value 65, '0' has the value 48, and ' ' has the value 32.

● Character constants actually have int type rather than char type.

10

'a'; /* a character constant, stored as int type (97) */

printf("%d\n", 'A');

printf("%d\n", '0');

printf("%d\n", ' ');

Operations on Characters

● When a character (char) appears in a computation, C uses its integer value.● Consider the following examples, which assume the ASCII character set:

11

char ch;

int i;

i = 'a'; /* i is now 97 */

ch = 65; /* assign value to char, ch is now 'A' */

ch = ch + 1; /* ch is now 'B' */

ch++; /* ch is now 'C' */

Operations on Characters

● Characters can be compared, just as numbers can.● Below is the example that an if statement that converts a lower-case letter to upper

case● 'a' <= ch are done using the integer values of the characters involved.● These values depend on the character set in use (we use ASCII code here),

○ so programs that use <, <=, >, and >= to compare characters may not be portable.

12

if ('a' <= ch && ch <= 'z')

ch = ch - 'a' + 'A';

Operations on Characters

● The fact that characters have the same properties as numbers has advantages.● For example, it is easy to write a for statement whose control variable steps through

all the upper-case letters:

13

char ch;

for (ch = 'A'; ch <= 'Z'; ch++){

...

}

Operations on Characters

● However, there are also some disadvantages of treating characters as numbers● Can lead to errors that won’t be caught by the compiler.● Allows meaningless expressions such as'a' * 'b' / 'c'. ● Can reduce the portability, since programs may rely on assumptions about the

underlying character set.○ For example, we assume that the the character set we used is ASCII

14

Signed and Unsigned Characters

● The char type—like the integer types—exists in both signed and unsigned versions.● Signed characters normally have values between –128 and 127. Unsigned characters

have values between 0 and 255.● Some compilers treat char as a signed type, while others treat it as an unsigned type. ● Most of the time, it doesn’t matter.

15

signed char sch;

unsigned char uch;

toupper Functions and ctype.h

● Calling C’s toupper library function is a fast and portable way to convert case

● toupper returns the upper-case version of its argument.

● Need to include #include <ctype.h>

○ many other useful character-handling functions.

16

#include <stdio.h>

#include <ctype.h>

int main()

{

char ch = 'a';

ch = toupper(ch);

printf("%c", ch);

return 0;

}

A

Reading & Writing Characters Using scanf and printf

● The %c conversion specification allows scanf and printf to read and write single characters:

17

char ch;

scanf("%c", &ch); /* reads one character */

printf("%c", ch); /* writes one character */

Reading & Writing Characters Using scanf and printf

● It’s easy to detect the end of an input line: check to see if the character just read is the new-line character \n

● A loop that reads and ignores all remaining characters in the current input line● In other words, you can continously input characters in terminal

○ but these characters will not be stored (except the final \n which is stored in ch after leaving the loop)

18

char ch;

do {

scanf("%c", &ch);

} while (ch != '\n');

● try to run this code and see what happen● also, try to replace \n with other character to see

what happen?● add a printf(“%c”, ch); at the end, to see

what is output

Reading & Writing Using getchar and putchar

● For single-character input and output, getchar and putchar are an alternative to scanf and printf.

● putchar writes a character:● Each time getchar is called, it reads one character, which it returns and we can use

a variable to save it.

19

char ch = 'a';

putchar(ch);

ch = getchar();

a

Reading & Writing Using getchar and putchar

● Why not just keep using scanf and printf?● Using getchar and putchar (rather than scanf and printf) saves execution

time.● getchar and putchar are much simpler than scanf and printf, which are

designed to read and write many kinds of data in a variety of formats.● getchar has another advantage. Because it returns the character that it reads,

getchar is useful in various C idioms.

20

Reading & Writing Using getchar and putchar

● getchar has another advantage. Because it returns the character that it reads, getchar is useful in various C idioms.

● Consider the scanf loop that we used to skip the rest of an input line:● Rewriting this loop using getchar gives us the following:

21

char ch;

do {

scanf("%c", &ch);

} while (ch != '\n');

char ch;

do {

ch = getchar();

} while (ch != '\n');

Reading & Writing Using getchar and putchar

• Moving call of getchar into the controlling expression allows us to shorten the code• The ch variable isn’t even needed; we can just compare the return value of getchar

with the new-line character• The following three loop produce the same effect

22

char ch;

do {

ch = getchar();

} while (ch != '\n');

while ((ch = getchar()) != '\n');

while (getchar() != '\n');

Reading & Writing Using getchar and putchar

● getchar is useful in loops that skip characters as well as loops that search for characters.

● A statement that uses getchar to skip an indefinite number of blank characters:● When the loop terminates, ch will contain the first nonblank character that getchar

encountered.

23

char ch;

while ((ch = getchar()) == ' ');

putchar(ch);

24

char ch;

while ((ch = getchar()) == ' ');

putchar(ch);

๏๏๏๏Hello\nH

๏: space

Reading & Writing Using getchar and putchar

1

1

● the first input char is ๏ (space), read by getchar() and store in ch● make the control expression of the while loop become true● start the first iteration, since no statement in loop body, come back to while

25

char ch;

while ((ch = getchar()) == ' ');

putchar(ch);

๏๏๏๏Hello\nH

๏: space

Reading & Writing Using getchar and putchar

1

1 2

2

● the second input char is ๏ (space), read by getchar() and store in ch● make the control expression of the while loop become true● start the second iteration, since no statement in loop body, come back to while

26

char ch;

while ((ch = getchar()) == ' ');

putchar(ch);

๏๏๏๏Hello\nH

๏: space

Reading & Writing Using getchar and putchar

1

1 2

2

● the fifth input char is H, read by getchar() and store in ch● make the control expression of the while loop become false● so no more new getchar() will be called. ● This getchar() will be ended after user press enter (input \n)3 4

3 4

5

5

27

char ch;

while ((ch = getchar()) == ' ');

putchar(ch);

๏๏๏๏Hello\nH

๏: space

Reading & Writing Using getchar and putchar

1

1 2

2

● print the last character stored in ch, which is H3 4

3 4

5

5

Reading & Writing Using getchar and putchar

● Since we use auto-grading system, we need to introduce EOF (end-of-file) here○ More content about File I/O will be covered in the course in the next semester

● \n is a new-line character, which indicate the end of a line● However, when you need to reading a file how would you know when to stop reading

○ there are multiple new-line characters in a file

● Hence, we need to know when we reach the end-of-file (EOF)

28

1

3

4

100

...https://stackoverflow.com/questions/4358728/end-of-file-eof-in-c

Reading & Writing Using getchar and putchar

● The example below will continue let user input characters and count the number until the input is EOF

● If you're typing at the terminal and you want to provoke an end-of-file, use CTRL-D (unix-style systems) or CTRL-Z (Windows).

29

char ch;

int num_c = 0;

while ((ch = getchar()) != EOF) num_c++;

printf("word num: %d", num_c);

https://stackoverflow.com/questions/4358728/end-of-file-eof-in-c

press CTRL-D or CTRL-Z on terminal to stop the while look

Reading & Writing Using getchar and putchar

● If you want to exit the while loop when you finish reading a line or finishe reading the last line in the file (i.e., end of file)

● You can use the example below

30

char ch;

int num_c = 0;

while( (ch = getchar()) != EOF && ch != '\n')

num_c++;

printf("word num: %d", num_c);

https://stackoverflow.com/questions/4358728/end-of-file-eof-in-c

press enter press CTRL-D or CTRL-Z

Let’s take a short break

31

Type Conversion

● For a computer to perform an arithmetic operation, the operands must usually be of the same size (the same number of bits) and be stored in the same way.

● For example, a computer may be able to add two 16-bits integers directly● but not a 16-bit integer and a 32-bit integer

32

1111100000111110

1111100000101110

1111100000101110

11111000001011101111100000101110

?

Type Conversion

● On the other hand, C allows the basic types to be mixed in expression● So, When operands of different types are mixed in expressions, the C compiler may

have to generate instructions that change the types of some operands so that hardware will be able to evaluate the expression. For example, ○ If we add a 16-bit short and a 32-bit int, the compiler will arrange for the

short value to be converted to 32 bits.○ If we add an int and a float, the compiler will arrange for the int to be

converted to float format.

33

Type Conversion

● Because the compiler handles these conversions automatically, without the programmer’s involvement, they’re known as implicit conversions.

● C also allows the programmer to perform explicit conversions, using the cast operator.

● The rules for performing implicit conversions are somewhat complex, primarily because C has so many different arithmetic types.

34

Type Conversion

● Implicit conversions are performed (there are totally 4 cases, but the last two will be discussed in chapter 9)○ When the operands in an arithmetic or logical expression don’t have the same

type. (C performs what are known as the usual arithmetic conversions.)○ When the type of the expression on the right side of an assignment doesn’t

match the type of the variable on the left side.

35

int i = 1;

float f = 0.1;

f + i; //usual arithmetic conversions

i = f; //conversion during assignment

The Usual Arithmetic Conversions

● The usual arithmetic conversions are applied to the operands of most binary operators (e.g., +, -, *).

● If f has type float and i has type int, the usual arithmetic conversions will be applied to the operands in the expression f + i○ should C convert i to type float or f to type int?

36

int i = 1;

float f = 0.1;

f + i; //usual arithmetic conversions

The Usual Arithmetic Conversions

● it’s safer to convert i to type float (matching f’s type) ● When an integer is converted to float, the worst that can happen is a minor loss of

precision. ● Converting a floating-point number to int, on the other hand, causes the fractional

part of the number to be lost.

37

int i = 1;

float f = 0.1;

f + i; //the result is 1.1

The Usual Arithmetic Conversions

● Two general Strategies of usual arithmetic conversions

38

The type of either operand is a floating type

Neither operand type is a floating type

long double

double

float

unsigned long int

long int

unsigned int

int int

The Usual Arithmetic Conversions

● When a signed operand is combined with an unsigned operand, the signed operand is converted to an unsigned value.

● This rule can cause programming errors.● It’s best to use unsigned integers as little as possible and, especially, never mix

unsigned with signed integers.

39

Conversion During Assignment

● The usual arithmetic conversions don’t apply to assignment.● Instead, the expression on the right side of the assignment is converted to the type of

the variable on the left side:

40

int i = 1;

float f = 0.1;

f + i; //usual arithmetic conversions

i = f; //conversion during assignment; f is convert to int

Conversion During Assignment

● Assigning a floating-point number to an integer variable drops the fractional part of the number

● Assigning a value outside the range of the variable’s type to a variable is meaningless

41

int i;

i = 842.97; /* i is now 842 */

i = -842.97; /* i is now -842 */

i = 1.0e20; /*** WRONG ***/

Casting

● Although C’s implicit conversions are convenient, we sometimes need a greater degree of control over type conversion.

● For this reason, C provides casts.● A cast expression has the form

42

( type-name ) expression type-name specifies the type to which the expression should be converted.

Casting

● Using a cast expression to compute the fractional part of a float value:● The difference between f and (int) f is the fractional part of f, which was

dropped during the cast.● Cast expressions enable us to document (implicit) type conversions that would take

place anyway

43

int i;

float f = 1.123, frac_part = 0.123;

frac_part = f - (int) f;

i = (int) f; /* f is converted to int (even without (int) */

Casting

● Cast expressions also let us force the compiler to perform conversions.● To avoid truncation during division, we need to cast one of the operands:● Casting dividend to float causes the compiler to convert divisor to float

also.

44

float quotient;

int dividend = 3, divisor = 2;

quotient = dividend / divisor; // quotient is 1

quotient = (float) dividend / divisor; // quotient is 1.5

Casting

● C regards ( type-name ) as a unary operator. ● Unary operators have higher precedence than

binary operators, so the compiler interprets● Other ways to accomplish the same effect:

45

float quotient;

int dividend = 3, divisor = 2;

quotient = dividend / divisor;

quotient = (float) dividend / divisor; // ((float) dividend) / divisor

quotient = dividend / (float) divisor; // quotient is 1.5quotient = (float) dividend / (float) divisor; // quotient is 1.5

Summary

● Character Type○ What is a character○ character set is ASCII○ Operations on Characters○ toupper Functions and ctype.h○ Reading & Writing Characters Using scanf and printf○ getchar and putchar

● Type Conversion○ The Usual Arithmetic Conversions○ Conversion During Assignment○ Casting

46