C Strings Lect 1-3
C Strings Lect 1-3
The string can be defined as the one-dimensional array of characters terminated by a null ('\0'). The character
array or the string is used to manipulate text such as word or sentences. Each character in the array occupies
one byte of memory, and the last character must always be 0.
There are two ways to declare a string in c language.
• By char array
• By string literal
Let's see the example of declaring string by char array in C language.
1- char ch[10]={'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't', '\0'};
As we know, array index starts from 0, so it will be represented as in the figure given below.
2- char ch[]={'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't', '\0'};
3- char ch[]="javatpoint";
In such case, '\0' will be appended at the end of the string by the compiler.
• We need to add the null character '\0' at the end of the array by ourself whereas, it is appended internally by
the compiler in the case of the character array.
• The string literal cannot be reassigned to another set of characters whereas, we can reassign the characters
of the array.
String Example in C
#include<stdio.h>
#include <string.h>
int main(){
char ch[11]={'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't', '\0'};
char ch2[11]="javatpoint";
Output