Array and String
Array and String
float
Each character in the array occupies one byte of memory, and the last character
must always be null('\0').
The termination character ('\0') is important in a string to identify where the
string ends.
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
name[10] D A R S H A N \0
Initialization method 1:
char name[10]={'D','A','R','S','H','A','N','\0'};
Initialization method 2:
char name[10]="DARSHAN";
//'\
0' will be automatically inserted at the end in this type of declaration.
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
name[10] D A R S H A N \0
gets(): Reads characters from the standard input and stores them as a string.
puts(): Prints characters from the standard.
scanf(): Reads input until it encounters whitespace, newline or End Of File(EOF)
whereas gets() reads input until it encounters newline or End Of File(EOF).
gets(): Does not stop reading input when it encounters whitespace instead it
takes whitespace as a string.
Program
1 #include <stdio.h> Output
2 #include <string.h> //header file for string functions Enter string: CE Darshan
3 void main() 10
4 {
5 char s1[10];
6 printf("Enter string:");
7 gets(s1);
8 printf("%d",strlen(s1)); // returns length of s1 in integer
9 }
Thank you