Chapter 5 - PowerPoint PPT Presentation

Chapter 5 l.jpg
1 / 31

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

Chapter 5

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 l.jpg

Chapter 5

Character Processing

The data type char l.jpg

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 l.jpg

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=‘*’;

Slide4 l.jpg

In C, a character is considered to have the integer value

corresponding to its ASCII encoding. See page 609.


ASCII value979899...122


ASCII value65666790


ASCII value484950...57


ASCII value384243

Characters and integers l.jpg

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 l.jpg

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 CInteger Value

    alert \a 7

    backslash \\ 92

    double quote \” 34

    horizontal tab \t 9

Input and output of characters l.jpg

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 */

Slide8 l.jpg

/* 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.

Slide9 l.jpg

/* 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 l.jpg

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

Slide11 l.jpg

/* 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 l.jpg

Chapter 6

The Fundamental Data Types

Size of data types in unix l.jpg

Size of Data Types in Unix

  • char 1 byte

  • short int2 bytes

  • int4 bytes

  • long int4 bytes

  • unsigned int4 bytes

  • float4 bytes

  • double8 bytes

  • long double16 bytes

    * 8 bits = 1 byte

Data type and sizes l.jpg

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 l.jpg

Fundamental Data Types

  • There are two types of representation for integer numbers:

    • unsigned

    • signed

charunsigned charsigned char

0: positive


intsigned intunsigned int

0: positive


Data types and sizes l.jpg

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 l.jpg

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 l.jpg

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 l.jpg

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 l.jpg

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.

Conversions l.jpg


  • 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 l.jpg

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.

Examples l.jpg


Type casting l.jpg

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 l.jpg

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 l.jpg

Chapter 7

Enumeration Types and typedef

Enumeration types l.jpg

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 l.jpg

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 l.jpg

The Use of typedef

color red, blue, green;

enumbool {false, true};


Slide30 l.jpg

/* 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);


Slide31 l.jpg

/* 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));


  • Login