#include #include #include int main (int argc, char **argv) { char *c; //c is a pointer to a char; char d[10]; //statically allocated array // c = "HELLO"; //automatically terminated with a null byte c = malloc (6); //five bytes for HELLO and one byte for the null strcpy (c,"HELLO"); //makes a copy of the string and stores it in dynamic memory printf ("%d\n",strlen(c)); printf ("%c %d\n",c[0],c[0]); printf ("%c %d\n",c[1],c[1]); printf ("%c %d\n",c[2],c[2]); printf ("%c %d\n",c[3],c[3]); printf ("%c %d\n",c[4],c[4]); printf ("%c %d\n",c[5],c[5]); c[0] = 'J'; //single character in single quotes crashes because I tried to change read only memory. The string constant is stored in read only memory. printf ("%s\n",c); //prints out a string. c[3] = '\0'; //change byte 3 to the null byte. printf ("%s\n",c); free (c); //free up the memory leak strcpy (d,"BATMAN"); printf ("%s\n",d); // strcpy (d,"CONSTANTINOPLE"); //too long printf ("Enter a string: "); scanf ("%s",d); //scanf reads in a string and stores it in d printf ("%s\n",d); return 0; } /* Strings in C are not complex structures as they are in C++ (or Java or Python). In fact, C has no such thing as a "string". The word "string" is just a figure of speech. What C has are arrays of chars. Characters have values ("codepoint" "ASCII value"). The newline character is 10. A form feed is 12. A tab is 9. The visible characters go from 32 (the space) to 126 (the tilde ~). Characters beyond 127 are no longer ASCII, but there are plenty of character sets that extend ASCII to provide a wider range of characters. The most common extension of ASCII now is Unicode, which numbers in the thousands or tens of thousands of characters. The C language treats a character as a byte, so 0 to 255. Or, more accurately, -128 to 127, since chars can be negative. Since ASCII falls between 0 and 127, the negative chars are almost never used. But C is too crude to have other characters beyond the normal ASCII. C just has arrays of characters. And an array does not know its own length. (In C++, you can get the length of an array. C does not have this concept; you have to keep of that yourself. C knows when a string ends when it his a null byte (ASCII 0). Since the null byte is used as a terminator, it can't actually be used as a character of a string itself. Don't go out of bounds on a string. C will not stop you from doing this and you can crash your program. scanf is a dangerous function because you don't know how much input the user is going to give, and the user types more than the string length, your program might crash. Some people say the best way to use scanf is never. Since your program is only going to be using low-level I/O operations, you won't have to worry about scanf anyway. */