C examples for String:char array
There is no string type in C.
Strings are commonly assigned to a character array as shown here.
char myString[] = "12";
Strings in C are terminated with a null character \0.
The null character is added automatically by the compiler for quoted strings.
The above statement can be written using regular array initialization syntax as follows.
char myString[3] = { '1', '2', '\0' };
To print a string the format specifier %s is used with the printf function.
%s outputs the literals of the string until the null character is encountered.
#include <stdio.h> int main(void) { char myString[] = "12"; printf("%s", myString); /* "Hi" */ }
A char pointer may be set to point to a string.
#include <stdio.h> int main(void) { char* ptr = "Hi"; printf("%s", ptr); /* "Hi" */ }