1 / 10

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

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. ASCII

jamieg
Download Presentation

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

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 single quotes, not double quotes.

  2. 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).

  3. 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); } Output letter_a is 97 and a decimal_a is 97 and a

  4. 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); } prints Adding 32 to A gives us a

  5. 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. Example char letters[10]; See example-char-array.c on the course website.

  6. Strings Storing each character individually is a tedious process. We can do this simultaneously by entering a string. Example char myName[] = “Preetam Ghosh"; This time we DO use double quotes.

  7. 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. Example char myName[] = “Preetam Ghosh"; printf("My name is %s", myName); /* print using %s */ will print Preetam Ghosh

  8. 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]); }

  9. 2D Array of Strings cont. Output of our program: Tom Dick Harry

  10. 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.

More Related