Home » C programming language

Strings in C language programming

C language Strings Tutorial: In this article, we are going to learn the basics of Strings in C programming language with examples.
Submitted by Sneha Dujaniya, on July 22, 2018

What are Strings?

Strings are mostly considered difficult by many beginners but trust me, Strings are no big deal. A string is nothing but a group of characters stored in a character array. Character arrays are used in programming languages to manipulate words and sentences.

A string is a constant in the form of the one-dimensional array of characters terminated by a null character which is \0. Example,

 char city[] = {'T', 'O', 'K', 'Y', 'O', '\0'};

Points to be noted:

  • Memory space occupied by each character is one byte and the last character has to be null \0.
  • \0 should not be confused with the digit 0 since the ASCII value of null is 0 whereas that of ‘0’ is 48.
  • The members of the array are stored in contiguous memory locations as shown below.
  • string representation in C
  • The null character at the end is very important because that is the only way for the compiler to know where the string ends. But to be noted, the null declaration is not necessary. It is inserted by the compiler automatically as in the following example: char city[] = "TOKYO";
  • The format specification used for printing and receiving a string is "%s".

Now let us see some of the examples:

Example 1: Declare string and print character by character

#include <stdio.h>

int main()
{ 
	//declaring string
	char city[] = "Tokyo" ;
	
	//loop counter
	int i = 0 ;
	
	//printing string one by one character
	while ( i <= 4 )
	{ 
		printf ( "%c", city[i] ) ;
		i++ ;
	}
	return 0;
}

Output

    Tokyo

Simple program, where we are treating the string as a character array and we use %c in printf() statement. Also, the length of the word is taken to be 4 because the value of the first element in an array is always assigned 0, as we all know already. But this solution is not profitable if we have a bigger string.

So, here is another way to do it:


Example 2: Declare string and print character by character till NULL not found

#include <stdio.h>
int main()
{ 
	//declaring string
	char city[] = "Tokyo" ;
	
	//loop counter
	int i = 0 ;
	
	//printing string one by one character
	//till NULL ('\0') not found 
	while ( city[i] != '\0' )
	{ 
		printf ( "%c", city[i] ) ;
		i++ ;
	}
	return 0;
}

Output

    Tokyo

Example 3: Declaring string and printing as string (using "%s" format specifier)

#include <stdio.h>

int main()
{ 
	//declaring string
	char city[] = "Tokyo" ;
	
	//print as string
	//using "%s" format specifier
	printf("%s",city);
	return 0;
}

Output

    Tokyo

This was the simplest way to print a string using the general specification of %s.


Example 4: Reading string using scanf() function

#include <stdio.h>

int main()
{ 
	//declaring string/character array
	char city[15] ;
	
	//read string 
	printf ( "Enter a city: " ) ;
	scanf ( "%s", city ) ;
	
	//printing string
	printf ( "Good Morning %s!", city) ;
	
	return 0;
}

Output

    Enter a city: Mumbai
    Good Morning Mumbai!

Here, the declaration char city[15] stores 15 bytes of memory under the array city[]. Whereas, the scanf() function fills the characters entered till the enter key is pressed by the user. After that, scanf() automatically adds a null character leaving the further memory blank.

Points to be kept in mind while using scanf():

  1. The length of the string entered by the user should not exceed the value declared in the array since C compilers do not show any Array index out of bound error for this. It will simply result in the overwriting of important data.
  2. Using scanf(), we can’t enter a multi-word string, for example, "New York". This problem can be resolved by using the function gets() and puts() as shown below,

Use of gets( ) and puts( ):

#include <stdio.h>

int main()
{ 
	//declaring string/character array
	char city[15] ;
	
	//read the string
	printf("Enter a city: ");
	gets(city)
	
	//printing the string 
	puts("Good Morning!");
	puts(city);
	
	return 0;
}

Output

    Enter a city: New York
    Good Morning!
    New York

Here, puts() can display only one string at a time therefore, two puts() statements are used. Also, unlike printf(), it places the cursor automatically to the next line.

Though gets() can receive only one string at a time, still it is preferable as it can receive multi-word strings.

Author's note:

This was a basic of what strings actually are. In the next article, I will write about string manipulations and standard library string functions. For better understanding, practice these codes on your own by changing the values and outputs. I’m ready to help if the case of queries. Happy coding!





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.