By now you should be wondering about these. C knows about strings (printf() prints a string, for example), but there is no such thing as a string data type. Here's the answer: in C, strings are represented as character arrays. Here's a little program to demonstrate the use of strings.
#include <stdlib.h> #include <stdio.h> #include <string.h> void main() { char s[14]; strcpy (s, "Hello World!\n"); printf (s); }
strcpy() copies the source string (the second argument) to the target string (the first argument). For extra functionality, it also returns the resultant string. The target string is modified anyway, so we don't need to use the function's result. printf() works as usual, and prints the string.
As you can see, we use a simple character array to hold the string. Note the length necessary to store the string: "Hello World!\n" is 13 characters long (\n is only one character, ASCII 13 or Newline/Return). Yet, s is 14 characters long. This is because C uses null-terminated strings. That is, there is an ASCII 0 after the string's last character so that the language knows where the string ends. If the terminating null is missing, you'll get weird behaviour 1
Version 0.5 of the Oric C compiler includes a complete implementation of the ANSI C string library (the header file for this is string.h). I'd like to describe some of the string-related functions, but it's impossible because of the lack of space. Please refer to the comments included with the header file, they are very helpful.
Initialising Arrays and Strings
We've seen that you can initialise C variables as you declare them. You can do the same for arrays:
int random_numbers[7] = {1,2,3,17,21,42,666}; int coord_list[4][2] = {0,0, 1,0, 1,1, 0,1}; char hello[6] = {'h','e','l','l','o',0}; char foobar[7] = "foobar";
The constants inside the curly brackets are stored in the array elements starting from element 0. You don't have to provide values for all elements, but uninitialised elements will have random values. Values are stored according to their sequence in memory (so, for the multi-dimensional array, things can be slightly more complicated — I've grouped elements together to show how it's done).
In the case of character arrays, you can either use character constants inside curly brackets (hello demonstrates this. Note the terminating null, the last element). Alternatively, you can just do what I've done with foobar and initialise the array with a string (C adds the terminating null automatically).
If you don't want to adjust the number of elements every time you change the initial values, you can declare the array in this fashion:
int random_numbers[] = {1, 2, 3, 17, 21, 42, 666}; char foobar[] = "foobar";
C will set the number of elements to fit the required elements (if you use a string initialiser, the terminating null is counted as well). Please note that this only works if you initialise an array. The size of the array is still constant; the only difference is that its size is calculated by the compiler during compilation.
- 1. Two strings walk into a bar. The first string says: ‘I think I'll have a beer quag fulk boorg jdk^CjfdLk jk3s d%f67howe%^U r89nvy~owmc63^Dz x.xvc’. ‘Please excuse my friend’, the second string says, ‘he isn't null-terminated.’




Add new comment