the char data type a char is a one byte integer type typically used for storing characters. example:...

10
The char Data Type A char is a one byte integer type typically used for storing characters. Example: char oneLetter = ’D’; We enclose the character in single quotes, not double quotes.

Upload: matthew-stevenson

Post on 30-Dec-2015

217 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: The char Data Type A char is a one byte integer type typically used for storing characters. Example: char oneLetter = ’D’; We enclose the character in

The char Data Type

A char is a one byte integer type typically used for storing characters.

Example:char oneLetter = ’D’;

We enclose the character in single quotes, not double quotes.

Page 2: The char Data Type A char is a one byte integer type typically used for storing characters. Example: char oneLetter = ’D’; We enclose the character in

ASCII

Plain text is often stored in a format called ASCII (American Standard Code for Information Interchange). There are 128 characters in ASCII, which includes the standard alphanumeric characters as well as some non-printable characters (e.g., tab, newline, and so forth).

Page 3: The char Data Type A char is a one byte integer type typically used for storing characters. Example: char oneLetter = ’D’; We enclose the character in

char Example

ASCII characters can be stored using the character or the decimal value of the character.

#include <stdio.h>

int main(void){

char letter_a = ’a’; /* note the use of single quotes */char decimal_a = 97; /* 97 is the decimal value

for ’a’ in ASCII *//* note the %c to print characters */printf("letter_a is %d and %c\n", letter_a, letter_a);printf("decimal_a is %d and %c\n", decimal_a, decimal_a);

}

Outputletter_a is 97 and adecimal_a is 97 and a

Page 4: The char Data Type A char is a one byte integer type typically used for storing characters. Example: char oneLetter = ’D’; We enclose the character in

char cont.

If we check an ASCII chart, we see that the letters A–Z have ASCII values of 65–90 while a–z have values of 97–122. Because the letters are stored as numbers, we can perform numeric operations on them.

#include <stdio.h>

int main(void){

char uppercase = ’A’; /* 65 in ASCII */char lowercase;lowercase = uppercase + 32;

printf("Adding 32 to %c gives us %c\n", uppercase, lowercase);}

printsAdding 32 to A gives us a

Page 5: The char Data Type A char is a one byte integer type typically used for storing characters. Example: char oneLetter = ’D’; We enclose the character in

Array of char

C does not have a string type. Instead, we use an array of chars for storing a string.

We can declare this just like we did with other types.

Examplechar letters[10];

See example-char-array.c on the course website.

Page 6: The char Data Type A char is a one byte integer type typically used for storing characters. Example: char oneLetter = ’D’; We enclose the character in

Strings

Storing each character individually is a tedious process. We can do this simultaneously by entering a string.

Examplechar myName[] = “Preetam Ghosh";

This time we DO use double quotes.

Page 7: The char Data Type A char is a one byte integer type typically used for storing characters. Example: char oneLetter = ’D’; We enclose the character in

Strings cont.

There is a difference between the two approaches.

When we create a string, a null character (i.e., \0) is added to the end. Note that when writing the null character, you should use a zero, not the letter O.

A null allows the compiler to know where the string ends so that we can print it by name.

Examplechar myName[] = “Preetam Ghosh";printf("My name is %s", myName); /* print using %s */

will printPreetam Ghosh

Page 8: The char Data Type A char is a one byte integer type typically used for storing characters. Example: char oneLetter = ’D’; We enclose the character in

2D Array of Strings

We can create 2D arrays of characters, i.e., an array of strings.

#include <stdio.h>int main(void){

char text[][6] = {"Tom", "Dick", "Harry"};int i, j;

for(i = 0; i < 3; i++)for(j = 0; j < 6; j++)

if(text[i][j] == ’\0’){

printf("\n");break;

}else

printf("%c", text[i][j]);}

Page 9: The char Data Type A char is a one byte integer type typically used for storing characters. Example: char oneLetter = ’D’; We enclose the character in

2D Array of Strings cont.

Output of our program:TomDickHarry

Page 10: The char Data Type A char is a one byte integer type typically used for storing characters. Example: char oneLetter = ’D’; We enclose the character in

2D Array of Strings cont.

Our 2D array of characters allocated space for 18 bytes, which was more than necessary.

It’s unusual to create a 2D array of characters as we did. It’s more typical to create an array of pointers to strings, which we will learn to do later.