README.md
December 5, 2022 · View on GitHub
Description
This is in continuation of tutorials I am writing
about C. Todays topic is Character and Strings.
Experts please skip this tutorials.
More Info
| Submitted On | |
| By | Indee |
| Level | Beginner |
| User Rating | 3.0 (15 globes from 5 users) |
| Compatibility | C |
| Category | Documents |
| World | C / C++ |
| Archive File |
Source Code
CHARACTER AND STRINGS
Ok atlast after a long gap I am writing the third tutorial explaining the concept of C language. Today's topic is character and strings. Experts skip this articles.
Characters
Another very imortant data in c is char, which stands for character. A character is a 1-byte value that can be used to hold printable character or integers in the range -128 to +127. A character constant is enclosed between single quotes. Ok let's see a sample program.
A Simple Example using characters
#include |
Although it is possible to use scanf() to read a single character from the keyboard, a more common way is to use library function getche(). The getche() function waits until a key is pressed and then returns the result. Let see another sample program. This program will display "you pressed my magic key" if I is pressed. (Tip: another use of getche() function is to hold the output displayed on the screen until a key is pressed.) This program also illustrates that character can be used in if statements.
Another Simple Example using characters
#include |
Strings
In C, a string is an array of characters terminated by a null.(Tip: in C, a null is essentially the same as 0.)C does not have a "string" type. Instead, it is declared as an array of characters and use the various string function found in the library to manipulate them. Array will come later in my tutorials but a few basic principles are required here.
Well array can be of number of dimensions but for this tutorial I am going to stick to one-dimensional array. A one-dimensional array is a list of variables of the same type. We can create such an array by placing the size of the array, enclosed between square brackets, after the array name. For example following fragment declares an 80-element character array called str.
char str[80];To reference a specific element, place the index between square brackets after the array name. All arrays in C are indexed from zero. Therefore, str[0] is the first element, str[1] is the second element, with str[79] being the 80th and last element.
It is most important to remember that no bounds-checking performed. If not careful, it is possible for program to "run off the end" of an array. A simple method of preventing this is to always use an array that is large enough to hold whatever will be put into it. Remember that all strings end in a null. Therefore array must be atleast one character larger than the largest string so that it can accommodate the null terminator. A null in C is specified as the character constant '\0'. As an example, a character array large enough to hold the word"hello" will need at least six characters long, five for the string and one for the null terminator, as shown below:
| h | e | l | l | o | '\0' |
To read a string from the keyboard, first create a character array to hold the string and then use the library function gets(). The gets() function takes the name of the string as an argument and reads characters from the keyboard . let's see an example program.
Simple Example using strings
#include |
Notice that the format command %s is used to cause printf() to print a string.
There are several standard functions used to manipulate strings. The two most common are strlen(), which returns the length of a string, and strcpy(), which copies a string from one string to another. They have the syntax strlen(string) and strcpy(destination, source) respectively. Copying a larger string to a smaller string, or reading a string into variable that is not large enough to store it can cause unpredictable results. Always check that the destination string is large enough to accommodate the source string. Let see an example showing these two library functions.
Another Simple Example using strings
#include |
Ok guys! thats enough for this tutorial next tutorial will be about OPERATORS IN C