C String

C  String:

• it is array of characters ended with null character (‘\0’).
• This null(\0) character indicates the end of the string.
• Strings are everytime enclosed by double quotes. Whereas, character is enclosed by single quotes in C.

EXAMPLES FOR C STRING:

• char string1[20] = {‘f’, ’r’, ‘e’, ‘s’, ‘h’, ‘2’, ‘r’, ‘e’, ‘f’, ’r’, ‘e’, ‘s’, ‘h’, ‘\0’};
(or)
• char string1[20] = “fresh2refresh”;
(or)
• char string1 [] = “fresh2refresh”;
• Difference between above declarations are, if we declare char as “string1[20]”, 20 bytes of memory space is allocated for holding the string value.
• If we declare char as “string1[]”, memory space will be allocated as per the requirement during execution of the program.

Example

#include<stdio.h>

int main ()

{

char string1[20] = “fresh2refresh.com”;

printf(“The string is : %s \n”, string1 );

return 0;

}

Output

The string is : fresh2refresh.com

Leave a Reply

Your email address will not be published. Required fields are marked *