String is an array of characters that is terminated by \0 (null character). This null
character indicates the end of the string. Strings are always enclosed by double quotes ( "
" ). Whereas, character is enclosed by single quotes.
Or
C PROGRAMMING Page 111
, In „C‟ language the group of characters, digits, and symbols enclosed within double
quotation ( " " ) marks are called as string otherwise a string is an array of characters and
terminated by NULL character which is denoted by the escape sequence „\0‟.
C Strings
Declaration of String: C does not support string as a data type. However, it allows us to
represent strings as character arrays. In C, a string variable is any valid C variable name and it is
always declared as an array of characters.
The general form of declaration of a string variable is :
Syntax: char string_name[size];
The size determines the number of characters in the string name.
Note: In declaration of string size must be required to mention otherwise it gives an error.
Ex: char str[]; // Invalid
char str[0]; // Invalid
char str[-1]; // Invalid
char str[10]; // Valid
char a[9]; //Valid
Using this declaration the compiler allocates 9 memory locations for the variable a
ranging from 0 to 8.
0 1 2 3 4 5 6 7 8
Here, the string variable a can hold maximum of 9 characters including NULL(\0)
character.
Initializing Array string
Syntax : char string_name[size]={“string” };
Note: In Initialization of the string if the specific number of character is not initialized it then
rest of all character will be initialized with NULL.
C PROGRAMMING Page 112
, char str[5]={'5','+','A'};
str[0]; ---> 5
str[1]; ---> +
str[2]; ---> A
str[3]; ---> NULL
str[4]; ---> NULL
Note: In initialization of the string we can not initialized more than size of string elements.
Ex:
char str[2]={'5','+','A','B'}; // Invalid
Different ways of initialization can be done in various ways :
1 : Initilizing locations character by character.
2 : Partial array initialization.
3 : Intilization without size.
4 : Array initialization with a string .
1 : Initilizing locations character by character
Consider the following declaration with initialization,
Char b[9]={„C‟,‟O‟,‟M‟,‟P‟,‟U‟,‟T‟,‟E‟,‟R‟};
The compiler allocates 9 memory locations ranging from 0 to 8 and these locations are
initialized with the characters in the order specified. The remaining locations are automatically
initialized to null characters.
C O M P U T E R \0
0 1 2 3 4 5 6 7 8
2 : Partial Array Initilization : If the characters to be initialized is less than the size of the
array, then the characters are stored sequentially from left to right.The remaining locations will
be initialized to NULL characters automatically.
C PROGRAMMING Page 113
The benefits of buying summaries with Stuvia:
Guaranteed quality through customer reviews
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller akhilagouri. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for £15.79. You're not tied to anything after your purchase.