Download presentation
Presentation is loading. Please wait.
Published byMatthew Stevenson Modified over 9 years ago
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 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 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 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.
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.