Strings in C Programming

strigs in c programming

Unlike other data items, there is no primitive data type in C Programming which is used to store Strings.

Developers of C Programming believes that String is nothing but an array of characters.

So in C Programming, we store String in the array of characters.

When we declare and initialize an array of character with a String, Just as
char myString[]="BLUE";

An array of the character of size 5 will be formed in memory. As shown in the figure
memory representation of a string in c programming

In an array of characters(used to store the string), the last index of the array will always have '\0' known as NULL. It shows that the String is ending here.

Format specifier for Strings in C Programming is %s. We can use it in print as well as in scan statement. Just as we use %d to get an integer from the user or to print an integer. 

Getting String From User:

char name[50];
printf("Enter your name: ");
printf("Your name: %s",name);

Enter your name: Jhon Bravo
Your name: Jhon

As you see, there is one problem in getting String from the user by using scanf statement. The scanf statement considers the space as NULL. And when we have printed the name it only prints the first name Jhon and ignores Bravo. Because after the first name, there is a NULL.

To overcome this situation, we use gets to read the complete line of text from the user and puts to print the String on to the console.

String operations using gets and puts:

char name[50];
printf("Enter your name: ");
printf("Your name: ");

Enter your name: Jhon Bravo
Your name: Jhon Bravo

1 comment:

  1. History coursework writing services has become very popular among students seeking History Essay Writing Services and history research paper writing services.