single character value (stored in 1 byte) as the ASCII value for a.
“a” is an array with two characters, the first is ‘a’, the second is the character value ‘\0’.
String literal is an array, can refer to a single character from the literal as a character.
Example:
printf(“%c”, ”hello”[1]);
outputs the character ‘e’
Examples of strings literals and strings variables: “hello” //string literal
“Pune and Mumbai” //string literal
char name[20]; //string variable
Initializing String Variables
Allocate an array of a size large enough to hold the string (plus 1 extra value for the delimiter)
Examples (with initialization):
char str1[6] = “Hello”;
char str2[] = “Hello”;
char *str3 = “Hello”;
char str4[6] = {‘H’,’e’,’l’,’l’,‘o’ ’\0’};
The compiler automatically stores the null characters at the end of the strings.
We can also use an pointer to point a string. Consider the following two declarations. Both are valid however there is a difference between the two.
char amessage[]=“c programming”;
char *pmessage=“c programming”;
A message is an array big enough to hold the sequence of characters and ‘\0’.Even if the characters are later changed ,amessege will always refer to the same storage.
On the other hand, pmessege is a pointer, which points to a string constants. pmessege may be later made to points elsewhere.
Comments
Post a Comment
Please give us feedback through comments