Characters and Strings
A character is a single letter, symbol, or number (from -128 to +127). The value of the character is stored in one byte of memory, so it must be very small. A string, however, can be any length of letters, words, or symbols, like a name or a sentence. Let’s learn how to handle both characters and strings in C.
Characters
In C, every letter and symbol has a corresponding number. Many computers use the ASCIIformat to convert letters into their equivalent numbers. ASCII stands for American Standard Code for Information Interchange. For example, if your computer uses the ASCII standard, then the letter K is equivalent to the number 75. Likewise, the symbol # is equivalent to the number 35.
Let’s try an example which shows us the conversion of letters on your computer. We will use a character variable char letter; to store the information, and the format specifier %c to print the value.
#include <stdio.h> void main( ) { char letter; printf(“\nType a letter: “); scanf(“%c”, &letter); printf(“\nOn my computer, the numeric value of %c is %d.\n”, letter, letter); } |
Type this source code in your editor and save it as char.c then compile it, link it, and run it. Depending on whether your computer uses the ASCII standard, you may get different answers to this question.
In this example, we used %d to convert your letter into a number. Actually, we simply used the integer (digit) format to display your letter.
String
When you need to store more than one letter in memory, use a string. Strings can hold letters, words, phrases, sentences.
For this type of variable, we must specify how many characters are in the string, plus one extra. The extra character is the \0 or NULL, which the computer uses to mark the end of the string. For example, if we expect someone’s first name to be less than 10 letters, we write:char first[10]. Then we use the format specifier %s to print the value of the first name, as in this example:
#include <stdio.h> void main( ) { char first[10]; printf(“\nType your first name: “); scanf(“%s”, first); printf(“\nYour name is %s.”, first); } |
Save this as school.c then compile it, link it, and run it.
Multiple Strings
Now, if we ask for the first and last names, we should increase the number of characters in the last name (since some of us have extremely long last names!):
#include <stdio.h> void main( ) { char first[10]; char last[20]; printf(“\nType your first name: “); scanf(“%s”, first); printf(“\nType your last name: “); scanf(“%s”, last); printf(“\nYour name is %s %s.”, first, last); } |
Type this source code in your editor and save it as string.c then compile it, link it, and run it. If one of your names is longer than the number of characters declared, then go back and change the numbers in your declaration lines. Remember to give one extra character for the \0 or NULL.
Strings with Spaces
If you initialize a string that has spaces in it, like Integrated Development Environment, leave the character number blank, like this: [ ]. Type the variable value in double quotes, like this: ” “.
#include <stdio.h> void main( ) { char editor[ ] = “Integrated Development Environment”; printf(“\nMost C compilers come with an editor, called an IDE.”); printf(“\nIDE stands for %s.”, editor); } |
Type this source code in your editor and save it as editor.c then compile it, link it, and run it.
Continue on to Simple & Nested Conditionals.
Leave a comment