C Programming Strings

In this tutorial, you will learn the concept of C programming strings with relevant examples.

c programming strings

C Strings

A string is a sequence of characters which is treated as a single data item in C. It can also be said as an array of characters and any group of characters defined between double quotations is string constants.

“trytoprogram” is an example of string

Now, try2program is a string with a group of 11 characters.

Each character occupies 1 byte of memory.

These characters are placed in consecutive memory locations; after all, string is an array of characters.

  address of "t" = 1000 (say) 
  address of "r" = 1001  
  address of "y" = 1002
  address of "2" = 1003
  address of "p" = 1004
  address of "r" = 1005
  address of "o" = 1006
  address of "g" = 1007
  address of "r" = 1008
  address of "a" = 1009
  address of "m" = 1010

String and memory


As we know string is the sequence of arrays and is placed in consecutive memory locations.

To indicate the termination of string a null character is always placed at the end of the string in the memory. Now above string will be stored in memory location like this:

c memory string

Declaring and Initializing string variables


String is not a data type in C, so character arrays are used to represent strings in C and are declared as:

char string_name [size];

The length of a string is always greater than the number of string characters by one because when a compiler assigns a character string to a character array, it automatically supplies a null character (‘\o’) at the end of the string.

Initializing string arrays


Strings in C can be initialized in following ways:

  char string_name [12] = "try2program";

  char string_name [12] = {'t','r','y','2','p','r','o','g','r','a','m','\0'};

In the above example, the size of an array will be determined automatically by the compiler.

Note: Difference between 0, ‘0’, ‘\0’, “0”.

0       //an integer value
'0'     //character value
'\0'    //an escape sequence representing null character
"0"     //string representation

 

[adsense1]

How to read strings from users?


Basically, there are two ways for reading the string from users:

  • using scanf function
  • using getchar and gets functions

using scanf function


The method of reading string using input function scanf with %s format specifications is the most infamous method.

However, with this scanf function, we can only read a word because this function terminates its input on the first white space it encounters.

For example:

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

Note: Normally, an ampersand is used before scanf function while reading the value of variables but in the case of character arrays, we don’t need an ampersand (&) because string name itself acts as a pointer to the first variable location.

using getchar and gets functions


scanf function is used to read only a word and to read a whole line of text either getchar or gets function is used.

This function can be used to read successive single characters from the input until a new line character '\n' is encountered and then null character is inserted at the end of the string.

Syntax of getchar function

char name;
name = getchar( );

Note: getchar function has no parameters.

Syntax of gets function

char name;
getchar(name);

Example: C program to read a word and a line of text entered by the user

#include <stdio.h>
int main()
{
 char word[20];
 char line[50], ch;
 int a=0;

 printf("Enter name :");
 while(ch!='\n')        //terminates if user hit enter
 {
  ch=getchar();
  line[a]=ch;
  a++;
 }
 line[a]='\0';
 printf("Name =%s",line);
 printf("Enter name :");
 scanf("%s",word);   //only reads a word and terminates at whitespace
 printf("Name = %s \n",word);
 return 0;
}

Output

Enter name: Denise Ritchie
Name = Denise Ritchie
Enter name : Denise Ritchie
Name = Denise

Explanation of the program

In above program, while loop will continue until the compiler encounters new line command i.e \n .

While user enters strings, getchar and gets also reads white spaces and only terminates when user will hit enter. So a whole line will be printed.

However, when we read input with scanf function no matter how long line user enters it will terminate once it encounters white space.

As a result with getchar function compiler can read whole name Denise Ritchie and with scanf function compiler can only read Denise as there is white space after it.

Moreover, there are certain string handling functions in C to manipulate strings and these functions are explained in our next chapter.