char hello[] = {'h', 'e', 'l', 'l', 'o', '!'};
\n a ``newline'' character \b a backspace \r a carriage return (without a line feed) \' a single quote (e.g., in a character constant) \" a double quote (e.g., in a string constant) \\ a single backslash
printf("Hello world.\n");
char word[] = { "Hello!" }; char word[7] = "Hello!"; char word[] = "Hello!"; char word[] = { 'H', 'e', 'l', 'l', 'o', '!', '\0' }; printf("She said %s\n", word);
#include <string.h> char string1[] = "Hello, world!"; char string2[20]; strcpy(string2, string1);
char string3[] = "this is"; char string4[] = "a test"; if(strcmp(string3, string4) == 0) printf("strings are equal\n"); else printf("strings are different\n");This code fragment will print "strings are different". Notice that strcmp does not return a boolean result.
char string7[] = "abc"; int len = strlen(string7); printf("%d\n", len);Which might be implemented as
int mystrlen(char str[]) { int i; for(i = 0; str[i]; i++) ; return i; }
int *ip;Which tells the compiler that variable ip is of type (int *) or equivalently that *ip is of type int.
int i = 5; ip = &i;At this point, the contents of ip and the address of i are the same -- they both refer to the same memory location, which contains the number 5.
printf("%d\n", *ip);
*ip = 4;So now the value of i will also be 4.
printf("%uld\n", (unsigned long) ip);Note that here we will see the 32-bit value of ip (that is, the memory location), and not the value to which ip points.
int *ip = &i;but you cannot initialize the value of the memory location to which it points, as something like
int *ip = 5;will only tell the compiler to use address 5 as the initial value for ip (and the contents of address 5 are undefined, and probably off-limits to your program anyway).
int *j; int* j;the latter leads to possible problems later, such as writing
int* i,j;when you wanted two pointers.