- Strings are character array.
- Character array are stored in contiguous memory location.
- A string constant is a one-Dimensional array of characters terminated by a null character('\0').
- Each character in the array occupies one byte of memory and the last character is always '\0'
- The terminating null character('\0') is very important in a string because its the only way for the compiler to know where the string ends.
- '\0' is called null character.
- '\0' and 0 are not same because both has different ASCII value. ASCII value of '\0' is 0 but ASCII value of 0 is 48.
- A string can be initialized without adding the terminating null character as,
Here C inserts the NULL character automatically.
- Similar to integer array we can access array elements
- This can be done by using pointer also. Mentioning th name of the array we get the base address(zeroth element) of the array.
- For reading and writting a string the format specifier is %s and no & is needed
The scanf() function fills in the character typed at keyboard into the str array until the blank space or enter key is hit. If blank space or enter key is hit scanf() automatically place a '\0' in the array.
- Normally the %s will read a string upto a blank space. To read a muti word string untill the end of line including blank space use %[^\n]s
Enter the string
- Other way of reading multiword string is gets() and puts(). But it is dangerous to use gets() in programs so try to avoid gets()
- A string can be stored in 2 forms
- char str="Welcome"; //here Welcome is stored in a location called str
- char *str="Welcome"; //here Welcome is stored in some other location i memory and assign the address of the string in a char pointer
- The advantage of pointing a string using a character pointer is we cannot assign a string to another string but we can assign a char pointer to another char pointer
- Once a string has been defined it cannot be initialized to another set of characters but using char pointer we can redefine the string.
- The size of the string depends on the size of declaration not the number of characters in the string
The output here is 30 not 1