• admin@embedclogic.com

string In C

string

As we learnt, the array is the collection of homogeneous data type elements.the data type may be int, char, float ,double,etc.

when we collect character data type then it is known as an array of characters while if this array of characters is written inside the ” ” instead of [ ] then it is known as string.the last element of an array of character element written inside ” ” is ‘\0’ by default.

Following points makes string different from array-

— The dataType of a string is always char.

— By default, the last element of a string is always a null character(‘\0’).

— String Constant is written in double quote(“<string_Constant>”).

— For complete string input or output, %s data specifier is used.

Syntax

Note : +1 for the last default ‘\0’ character in string-literal.

Example   

 char name[20];

where name = string_name

[20] = input string size must be less than 20 character

String-Initialization

String Initialization is a process to assign a value to the string at the time of declaration.Size is not necessary to give if we initialize a string with some constant.

string variable Initialization

In above string initialization “hello” is a constant string literal and store into read-only memory. and it saves a copy into the stack when it assigns to the ‘str’ array. So all the operation will perform on the stack copy and the change in the string is possible-

string pointer Initialization

In above string initialization “hello” is a constant string literal and store into read-only memory while str is a pointer to that string means str has the address of a constant string literal which is read-only memory so any write is not acceptble only read is allowed-

that’s why to avoid confusion always use const in string pointer declartaion like this –

Example:           

Representation of a string into Array

String Representation :   char name[ ]=”Amit”;

Array Representation : char name[ ]={ ‘A’,’m’,’i’,’t’ ,’\0′};

So you can see that in the array we have to insert ‘\0’ character manually while string has its default as the last element of the string.

Use of %s specifier in C

Although the string is an array of character elements so we can use %c to read or write the character element but we have to use the loop for the number of times because %c will read or write a single character at a time.

%s data specifier saves us from looping and it can read or write a complete string in one go. Actually, it takes the advantage of Null character present in the string.

when we use %s then it started to read or write from the file until a null character or space is not found.

Program

Output:

Need of gets() function 

As we have seen in above example, The scanf() function terminate the input if space is available in between the string constant.

scanf will terminate string when space is found. In above example, we got only Hello instead of Hello India because scanf will accept the data before space.

So instead of using scanf with the %s specifier for string input, we can use gets() function.it is library defined function(stdio.h).

Syntax

string_ptr: pointer to the string or string variable name.

gets() function doesn’t need %s specifier because it only accepts string nothing else.

This function takes the input from stdin file and copies into the addressed buffer(string_ptr) until it doesn’t get the null character.It will not terminate on space like scanf() function.

Return Value: 

Success: then it returns a pointer to the input string

Fail: Null Pointer (pointer that doesn’t have any address)

Program

Output:

Need of puts() function 

Although printf() doesn’t have any drawback like scanf and it can do the same work for string as puts() but puts is dedicated for string operation so if you use puts then you don’t need to tell compiler about format specifier %s.

Syntax

string_ptr: pointer to the string or string variable name.

puts() function doesn’t need %s specifier because it only accepts string nothing else.

puts() function takes the output from the buffer(string_ptr) and copies it to stdout file to display the output on the console.

Return Value:

Success: Number of bytes transfer to the stdout file.

Fail: 0

Program

Output:

Points to remember regarding string

I have mentioned some important points regarding string representation in below table to avoid the confusion.Look at these and try to apply in any string example to be more clear.