String Basics- Initialization and Declaration – C Tutorials

A string is basically a character array which terminates with a ‘\0’. It can have alphabets in both cases, digits and special characters. It is like having a group of characters that forms words or sentences.

The ‘\0’ is also known as the null character. It is placed at the end of any character array or string. For example:

char name={‘C’,’O’,’D’,’I’,’N’,’G’,’E’,’E’,’K’,’\0′};

For some declarations, even if the programmer doesn’t put the ‘\0’ at the end of the character array the compiler will automatically do that. ‘\0’ is only one character, although it might look like two. The ASCII code of ‘\0’ is 0 which is not the same as ‘0’ which has an ASCII code of 48. ‘\0’ is what distinguishes a string from a normal character array. The string related functions identify ‘\0’ and recognize where a string ends. The figure shown below shows how a string is stored in a memory:

String memory allocation

Note that the memory locations are random and it might differ for different compilers and systems.


Declaration and initialization of String

Before starting our operations on strings we need to declare a string. There are two ways declaring a string. As a character array and as a pointer:

char name[10];//as a character array
char *name;//as a pointer

A string can be initialized in many different ways. Here are those ways:


char name[]={'C','O','D','I','N','G','E','E','K','\0'}; //as an unsized array. This method requires the user to put a '\0' at the end
char name[10]={'C','O','D','I','N','G','E','E','K','\0'}; //as a sized array.
char name[]="CODINGEEK"; //unsized array. Puts '\0' automatically
char name[10]="CODINGEEK"; //sized array.

Taking string input from a user

Scanf() is used to take inputs from the user.Strings can be taken as input using scanf() function. The format is given below:

char name[10];
scanf("%s", name);

As we already know that string might contain blank spaces, one limitation with scanf() is that it terminates as soon as it encounters a blank space. Example:

We enter: String input

Output: String

To avoid this complication we enter the elements of a string like any normal character array.Do not forget to add ‘\0’ at the end of the string. The syntax is given below:

char name[20],ch;//declaration
int i=0;
while(ch!="\n")
{
ch=getchar();
name[i]=ch;
i++;
}

Simple Input Program on string

Here is a simple program on string which takes input from the user and displays the string:

#include <stdio.h>
int main() {
  char name[20], ch; // declaration of a character array
  int i = 0;
  printf("Enter a string:\n");
  // taking string input from the user
  while (ch != '\n') {
    ch = getchar();
    name[i] = ch;
    i++;
  }
  name[i] = '\0'; // ending the string
  i = 0;
  // display the string
  printf("The string is:\n");
  while (name[i] != '\0') {
    printf("%c", name[i]);
    i++;
  }
  return 0;
}

Output:-
Enter a string:
The bell rang
The string is:
The bell rang

Do come back for more. Hope this helps and you like the tutorial. Do ask for any queries in the comment box and provide your valuable feedback.

Share and subscribe.

Keep Coding!! Happy Coding!! 🙂

Recommended -

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Index