The char type:
A char is a whole number with the range of values -128 to 127 if it is signed or 0 to 255 if unsigned. This value can be used as a number as well as a single letter. ASCII is used to convert a number into a letter or symbol: 65-90 become upper case A-Z and 97-122 become a-z. A full table can be found at http://www.asciitable.com/.
To print the symbol or letter form of a character, use the %c marker with printf. The ASCII values 48-57 are used for the number symbols 0-9, so be careful not to confuse this output with the numeric value of the character. Also, the symbols with values outside the range 0-127 will depend upon the standard used by a country.
If you don't want to memorize the ASCII values of a symbol, C allows us to assign symbols straight to chars. Here is a demonstration.
#include <stdio.h>
int main() { char letter = 'g'; char symbol = 47; //no single-quotes char number = '3'; //single quotes //%c for the symbol, %d or %u for its value printf("%c in ASCII: %d\n", letter, letter); printf("%c in ASCII: %d\n", symbol, symbol); printf("%c in ASCII: %d\n", number, number); getchar(); return 0; } | output in prompt: g in ASCII: 103 / in ASCII: 47 3 in ASCII: 51 |
Strings:
By placing a collection of these characters into an array, we can create a string. This can hold a word, a sentence, a sequence of numbers, and anything else you can type into a text editor. Strings can be placed in either static or dynamic arrays.
Since strings can be placed in dynamic arrays, there exists a way to track their length. Every C string ends with an additional null character to indicate there is no more data. A null character has the numeric value 0 and can be represented as '\0' in symbol form. The \ in front is the same used by the character '\n'.
Once you have a sequence of characters in an array ending with the null character, you can print the string. Use the %s marker in printf to print the string, but it only stops at the end if there is a null character. Also, an additional string library (string.h) contains the strlen function which finds length of a string. It too stops at the null character, but does not count it.
Now, to demonstrate how strings can be created and printed, here are three ways to say hello:
#include <stdio.h>
#include <stdlib.h>
#include <string.h> //full of string functionschar* quotes = "hello"; char static_array[] = {'h','e','l','l','o','\0'}; char* dynamic; //will copy from static int main() { int index; int size = strlen(static_array); printf("string length = %d\n", size); size += 1; //size to include null char dynamic = (char*)malloc(size * sizeof(char)); for (index = 0; index < size; ++index) { dynamic[index] = static_array[index]; } //print all forms of hello printf("quotes: %s\n", quotes); printf("static: %s\n", static_array); printf("dynamic: %s\n", dynamic); //remove dynamic from memory free(dynamic); getchar(); return 0; } | output in prompt: string length = 5 quotes: hello static: hello dynamic: hello |
The static array is assigned a set of values. This method of assigning values does not require us to specify a size and only works for statically allocated arrays. Elements are between { and } and separated by commas. Basically, static_array is given the character symbols h, e, l, l, o, and null, making it six elements long. No additional work is needed and static_array is ready to print.
The elements of the dynamic array are given the characters in the static array. First, we find the size of the static_array string (which could be found with "sizeof(static_array) / sizeof(*static_array)", but strlen works too). Then, we allocate memory and copy over each character, including the null character. Finally, dynamic is ready to print and must be removed from memory before exiting.
The quotes are used to create a statically allocated array ending with '\0', which means the quotes array is ready to print. The quotes also make the array unmodifiable. Unlike static_array whose elements can be changed and used, the quotes array elements can only be read, not assigned values to. For example, the value quotes[3] can be assigned to another variable, but "quotes[3] = 'i';" is not valid.
This type of array is placed in a different part of memory which can only be read from. It is referred to as a non-volatile, or const, array. By placing variables in 'const' memory, you allow them to only be given a value once, but that value can be used at any time. For more information on const variables, check out the page http://publications.gbdirect.co.uk/c_book/chapter8/const_and_volatile.html. Remember that this is a property of the array itself, the variable quotes just happens to point to this array.
The quotes array is also ready to print without much work. In fact, the same type of strings are used as the first parameter in the printf commands above because of their easy setup. The const array strings can also be used in the strlen function to find their length (as well as strings in dynamic arrays). Like the statically allocated arrays, we do not need to remove them from memory.
For the final example, here is a program that merges two strings together with sprintf:
#include <stdio.h> //printf and sprintf #include <stdlib.h> //malloc and free #include <string.h> //strlen
//for multiple pointers, each needs an *
char *hello = "hello ", *world = "world!";char* text; //message to print int main() { int size = strlen(hello); size += strlen(world); size += 1; //for the null character text = (char*)malloc(size * sizeof(char)); //output goes to string, not prompt sprintf(text, "%s%s", hello, world); printf(text); //output message free(text); //remove from memory getchar(); return 0; } | output in prompt: hello world! |
The sprintf function allows us to place a string which would normally print in the prompt and place it into a string array. Here, the strings hello and world are printed side-by-side and put into the text array. The sprintf function places a null character at the end automatically, just be sure the array has enough space.
For more information about C strings, you can read the page http://www.cprogramming.com/tutorial/lesson9.html and see a reference of the functions found in string.h at http://www.cplusplus.com/reference/clibrary/cstring/. The next post will be about enumerated types and flags, then onto SDL!
No comments:
Post a Comment