Title: Character Processing
1Chapter 5
2The Data Type char
- Each character is stored in a machine in one byte
(8 bits) - 1 byte is capable of storing 28 or 256 distinct
values. - When a character is stored in a byte, the
contents of that byte can be thought of as either
a character or as an integer.
3The Data Type char
- A character constant is written between single
quotes. - a
- b
- A declaration for a variable of type char is
- char c
- Character variables can be initialized
- char c1A, c2B, c3
4In C, a character is considered to have the
integer value corresponding to its ASCII
encoding. See page 609.
lowercase a b c ... z ASCII
value 97 98 99 ... 122 uppercase A B C ...
Z ASCII value 65 66 67 90 digit 0 1 2
... 9 ASCII value 48 49 50 ... 57 other
... ASCII value 38 42 43
5Characters and Integers
- There is no relationship between the character
2 (which has the ASCII value 50) and the
constant number 2. - 2 is not 2.
- A to Z 65 to 90
- a to z 97 to 112
- Examples
- printf(c,a)
- printf(c,97) have similar output.
- Printf(d,a)
- printf(d,97) have also similar output.
6The Data Type char
- Some nonprinting and hard-to-print characters
require an escape sequence. - For example, the newline character is written as
\n and it represents a single ASCII
character.(page 176) - Name of character Written in C Integer Value
- alert \a 7
- backslash \\ 92
- double quote \ 34
- horizontal tab \t 9
7Input and Output of Characters
- getchar ( ) reads a character from the keyboard.
- c getchar() / variable c contains the
next
character of input / - putchar ( ) prints a character to the screen.
- putchar(c) / prints the contents of the
variable c as a character /
8/ Illustrating the use of getchar( ) and
putchar( ) / include ltstdio.hgt int main
(void) char c while ((cgetchar()) !
EOF) putchar(c) putchar(c)
abcdef aabbccddeeff
EOF It is control-d in Unix control-z in DOS.
9/ Capitalize lowercase letters and double
space / include ltstdio.hgt int main(void)
int c while ((cgetchar()) ! EOF) if
(a lt c c lt z) putchar(cA-a)
/convert to uppercase/ else if (c
\n) putchar (\n) putchar (\n)
else putchar (c)
cop3223!c C
10Character Functions
- Function Nonzero (true) is returned if
- isalpha(c) c is a letter
- isupper(c) c is an uppercase letter
- islower(c) c is a lowercase letter
- isdigit(c) c is a digit
- isalnum(c) c is a letter or digit
- isspace(c) c is a white space character
- Function
Effect_____________ - toupper(c) changes c to uppercase
- tolower(c) changes c to lowercase
- toascii(c) changes c to ASCII code
11/ Capitalize lowercase letters and double space
/ include ltstdio.hgt includeltctype.hgt int
main(void) int c while ((cgetchar()) !
EOF) if (islower(c)) putchar(toupper(c))
/convert to uppercase / else if (c
\n) putchar (\n) putchar (\n)
else putchar (c)
12Chapter 6
- The Fundamental Data Types
13Size of Data Types in Unix
- char 1 byte
- short int 2 bytes
- int 4 bytes
- long int 4 bytes
- unsigned int 4 bytes
- float 4 bytes
- double 8 bytes
- long double 16 bytes
-
- 8 bits 1 byte
14Data Type and Sizes
- Characters are treated as small integers.
- char c A / A has ASCII encoding 65
/ - int i 65 / 65 is ASCII encoding for
A /
0
1
0
0
0
0
0
1
15Fundamental Data Types
- There are two types of representation for integer
numbers - unsigned
- signed
char unsigned char signed char
0 positive 1negative
int signed int unsigned int
0 positive 1negative
16Data Types and Sizes
- int between 2,147,483,648 and 2,147,483,647 if
(4-bytes) - int between 32,768 and 32,767 if (2-bytes)
- There are a number of qualifiers that can be
applied to these basic types. short and long can
be applied to integers. - short int sh
- long int counter
- The intent is that short and long should provide
different lengths of integers where practical
(i.e to save memory). - Other qualifiers such as unsigned and signed may
be applied to char or int.
17The Floating Types
- A float on many machines has an approx. range of
10-38 to 1038. - A double on many machines has an approx. range of
10-308 to 10308. - The working floating type in C is double.
18The sizeof Operator
include ltstdio.hgt int main() printf("Size
of some fundamental types computed. \n\n")
printf(" char3d byte \n",sizeof(char))
printf(" short3d byte \n",sizeof(short))
printf(" int3d byte
\n",sizeof(int)) printf(" long3d byte
\n",sizeof(long)) printf(" unsigned3d
byte \n",sizeof(unsigned)) printf("
float3d byte \n",sizeof(float)) printf("
double3d byte \n",sizeof(double))
printf("long double3d byte \n",sizeof(long
double)) return 0
19Mathematical Functions
- There are no built-in mathematical functions in
C. - Functions such as
- sqrt( ) pow( ) exp( ) log( ) sin( )
cos( ) - are available in the mathematical library
(math.h). - include ltmath.hgt
- The -lm option or some other option may be needed
to compile a program that uses mathematical
functions.
20Mathematical Functions
- All of the functions listed on the previous
slide, except the pow ( ) function, take a single
argument of type double and return a value of
type double. - The pow ( ) function takes 2 arguments of type
double and returns a value of type double.
21Conversions
- Consider the following expressions.
- x y where x and y are of type int
- x y where x and y are of type short
- In both situations, xy is converted to int.
- If all the values of the original type can be
represented by an int, the value is converted to
an int otherwise it is converted to an unsigned
int. (The integral promotion)
22Arithmetic Conversions
- Arithmetic conversions can occur when the
operands of a binary operator are evaluated. - i f where i is an int and f is a float, the
result is float - See page 217 for a list of arithmetic conversion
rules.
23Examples
24Type Casting
- In addition to implicit conversions, there are
explicit conversions called casts. - If i is an int, then
- (double) i
- will cast the value of i so that the expression
has type double. - Casts can be applied to expressions.
- x (float)((int)y1)
- y (float) i 3
25Common errors
- Assume a 4-byte word machine
- int a 1, b 2147483648
- ab ? -2147483647
- Dividing two integers will always give an integer
- int j 2, k 5
- j/k ? 0
- (double) j/k ? 0.4
26Chapter 7
- Enumeration Types and typedef
27Enumeration Types
- Allows you to name a finite set and to declare
identifiers, called enumerators, elements of the
set. - Consider the declaration
- enum day sun, mon, tue, wed, thu, fri, sat
-
0 1 2
6 - To declare variables of type enum day
- enum day d1, d2
- To assign the value fri to d1
- d1 fri
- We can declare variables directly after the
enumerator declaration. - enum suit clubs1, diamonds, hearts,
spades a, b, c
28The Use of typedef
- C provides the typedef facility so that an
identifier can be associated with a specific
type. - For example,
- typedef int color
- makes color a type that is synonymous with int,
and it can be used in declarations just as other
types are used.
29The Use of typedef
- color red, blue, green
- enum bool false, true
- typedef enum bool boolean
30/ Compute the next day / enum daysun, mon,
tue, wed, thu, fri, sat typedef enum day
day day find_next_day (day d) day
next_day switch(d) case
sun next_day mon
break case mon
next_day tue break
case sat
next_day sun break
return (next_day)
31 / Compute the next day with a cast / enum day
sun, mon, tue, wed, thu, fri, sat typedef
enum day day day find_next_day (day d)
return ((day) ((int) d 1) 7))