Hello Viewer! How are you I hope you well? In the previous lesson, we learned Function in C language. in this lesson we'll learn Strings in C Language ( Part 1 )...
As we know that the way a group of an integer can be stored in Integer Array. Similarly, a group of character can be stored in a Character Array. Character Array is many times also called Strings. Many programming languages internally treat String as Character Arrays. Character Arrays or Strings are used by Programming languages to manipulate text, such as word and sentences.
A string constant is a one-dimensional array of characters terminated by a ( \0 ) NULL.
example:-
char name [ ] = { 'B', 'A', 'B', 'Y', '\0' }
Each Character in Array occupies 1 byte of memory and the last character is always '\0' it looks likes two characters, but it is actually only one character, with the \ indicating.'\0' is called NULL character. Note that the elements of the character array stored in contiguous memory locations.
C concedes the fact that you would use string very often and hence provides a shortcut for initializing strings . for example, the string used above can also be initialized as,
char name [ ] = "BABY" ;
Note that, In this declaration ' \0 ' is not necessary. C insert the NULL character automatically.
More about String
In what way are Character Arrays different from numeric arrays? Understand with the following program.
#include
int main()
{
char name []= "BABY";
int i =0;
while (i<=7)
{
printf("%c",name[i]);
i++;
}
printf("\n");
return 0;
}
Output :
BABY
No, we have initialized a character array, and then printed out the elements of this array within a while loop.
#include
int main()
{
char name []= "BABY";
int i =0;
while (name[i]!='\0')
{
printf("%c",name[i]);
i++;
}
printf("\n");
return 0;
}
Output :
BABY
This program doesn't rely on the length of the string to print out its contents and hence is definitely more general then the earlier one.
As we know that the way a group of an integer can be stored in Integer Array. Similarly, a group of character can be stored in a Character Array. Character Array is many times also called Strings. Many programming languages internally treat String as Character Arrays. Character Arrays or Strings are used by Programming languages to manipulate text, such as word and sentences.
A string constant is a one-dimensional array of characters terminated by a ( \0 ) NULL.
example:-
char name [ ] = { 'B', 'A', 'B', 'Y', '\0' }
Each Character in Array occupies 1 byte of memory and the last character is always '\0' it looks likes two characters, but it is actually only one character, with the \ indicating.'\0' is called NULL character. Note that the elements of the character array stored in contiguous memory locations.
C concedes the fact that you would use string very often and hence provides a shortcut for initializing strings . for example, the string used above can also be initialized as,
char name [ ] = "BABY" ;
Note that, In this declaration ' \0 ' is not necessary. C insert the NULL character automatically.
More about String
In what way are Character Arrays different from numeric arrays? Understand with the following program.
#include
int main()
{
char name []= "BABY";
int i =0;
while (i<=7)
{
printf("%c",name[i]);
i++;
}
printf("\n");
return 0;
}
Output :
BABY
No, we have initialized a character array, and then printed out the elements of this array within a while loop.
#include
int main()
{
char name []= "BABY";
int i =0;
while (name[i]!='\0')
{
printf("%c",name[i]);
i++;
}
printf("\n");
return 0;
}
Output :
BABY
This program doesn't rely on the length of the string to print out its contents and hence is definitely more general then the earlier one.
In this part we complete the Strings in C. I hope you like it. we'll discuss Next Part in the future post.
Thank You 4 reading my blog! if you have any problem or issue with this post please comment/contact.
Important :
In this post, Article/Paragraph is taken from Let Us C book that is written by Yashavant Kanetkar.