chapter 5
Skip this Video
Download Presentation
Chapter 5

Loading in 2 Seconds...

play fullscreen
1 / 31

Chapter 5 - PowerPoint PPT Presentation

  • Uploaded on

Chapter 5. Character Processing. The Data Type char. Each character is stored in a machine in one byte (8 bits) 1 byte is capable of storing 2 8 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.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about 'Chapter 5' - LionelDale

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
chapter 5

Chapter 5

Character Processing

the data type char
The 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.
the data type char3
The Data Type char
  • A character constant is written between single quotes.



  • A declaration for a variable of type char is

char c;

  • Character variables can be initialized

char c1=‘A’, c2=‘B’, c3=‘*’;


In 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

characters and integers
Characters 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.
the data type char6
The 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

input and output of characters
Input 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 */


/* Illustrating the use of getchar( ) and putchar( ) */

#include <stdio.h>

int main (void)


char c;

while ((c=getchar()) != EOF) {







EOF : It is control-d in Unix; control-z in DOS.


/* Capitalize lowercase letters and * double space */

#include <stdio.h>

int main(void)

{ int c;

while ((c=getchar()) != EOF){

if (‘a’ <= c && c <= ‘z’)

putchar(c+’A’-’a’); /*convert to uppercase*/

elseif (c == ‘\n’){

putchar (‘\n’);

putchar (‘\n’);


else putchar (c);



cop3223!c C

character functions
Character 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


/* Capitalize lowercase letters and double space */

#include <stdio.h>


int main(void)

{ int c;

while ((c=getchar()) !=EOF){

if (islower(c))

putchar(toupper(c)); /*convert to uppercase */

elseif (c == ‘\n’){

putchar (‘\n’);

putchar (‘\n’);


else putchar (c);



chapter 6

Chapter 6

The Fundamental Data Types

size of data types in unix
Size 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

data type and sizes
Data 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’ */









fundamental data types
Fundamental Data Types
  • There are two types of representation for integer numbers:
    • unsigned
    • signed

char unsigned char signed char

0: positive


int signed int unsigned int

0: positive


data types and sizes
Data 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.
the floating types
The 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.
the sizeof operator
The sizeof Operator

#include <stdio.h>

int main()


printf("Size of some fundamental types computed. \n\n");

printf(" char:%3d byte \n",sizeof(char));

printf(" short:%3d byte \n",sizeof(short));

printf(" int:%3d byte \n",sizeof(int));

printf(" long:%3d byte \n",sizeof(long));

printf(" unsigned:%3d byte \n",sizeof(unsigned));

printf(" float:%3d byte \n",sizeof(float));

printf(" double:%3d byte \n",sizeof(double));

printf("long double:%3d byte \n",sizeof(long double));

return 0;


mathematical functions
Mathematical 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 <math.h>

  • The -lm option or some other option may be needed to compile a program that uses mathematical functions.
mathematical functions20
Mathematical 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.
  • 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, x+y 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)
arithmetic conversions
Arithmetic 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 resultis float

  • See page 217 for a list of arithmetic conversion rules.
type casting
Type 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)y+1);

y = (float) i + 3;

common errors
Common errors
  • Assume a 4-byte word machine
    • int a =1, b= 2147483648;
    • a+b  -2147483647;
  • Dividing two integers will always give an integer
    • int j = 2, k =5;
    • j/k  0;
    • (double) j/k  0.4;
chapter 7

Chapter 7

Enumeration Types and typedef

enumeration types
Enumeration 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 {clubs=1, diamonds, hearts, spades} a, b, c;

the use of typedef
The Use of typedef
  • C provides the typedef facility so that an identifier can be associated with a specific type.
  • For example,


makes color a type that is synonymous with int, and it can be used in declarations just as other types are used.

the use of typedef29
The Use of typedef

color red, blue, green;

enumbool {false, true};



/* Compute the next day */

enumday{sun, mon, tue, wed, thu, fri, sat};


day find_next_day (day d)


day next_day;

switch(d) {

case sun:

next_day = mon;


case mon:

next_day = tue;


case sat:

next_day = sun;



return (next_day);



/* Compute the next day with a cast */

enumday {sun, mon, tue, wed, thu, fri, sat};


day find_next_day (day d)


return ((day) ((int) d + 1) % 7));