Strings

                                                                                        Strings in C

Strings in C programming is a sequence of character terminated with a null character '\0'. Strings are defined as an array of characters . The difference between a character array and a string is the string is terminated with a unique character '\0'. 

Example of C String:


Strings are used for storing text/characters.

For example  "Hello World" is a string of characters.

Unlike many other programming languages, C does not have a String type to easily create string variables. Instead, you must use the char type and create an array of characters to make a string in C:


Declaration of Strings


Declaring a string is as simple as declaring a one-dimensional array. Below is the basic syntax for declaring a string.


In the above syntax str_name is any name given to the string variable and size is used to define the length of the string, i.e the number of characters strings will store. 
Note: There is an extra terminating character which is the Null character (‘\0’) used to indicate the termination of a string that differs strings from normal character arrays. When a Sequence of characters enclosed in the double quotation marks is encountered by the compiler, a null character ‘\0’ is appended at the end of the string by default.

Initializing a String


1. A string can be initialized in different ways. We will explain this with the help of an example. Below are the examples to declare a string with the name str and initialize it with “CodeWithSahil”.


2. Assigning a string literal with a predefined size: String literals can be assigned with a predefined size. But we should always account for one extra space which will be assigned to the null character. If we want to store a string of size n then we should always declare a string with a size equal to or greater than n+1.

3. Assigning character by character with size: We can also assign a string character by character. But we should remember to set the end character as ‘\0’ which is a null character.

4. Assigning character by character without size: We can assign character by character without size with the NULL character at the end. The size of the string is determined by the compiler automatically.

How to Read a String From User?

C

// C program to read string from user
#include<stdio.h>
    
int main()
{   
    // declaring string
    char str[50];
        
    // reading string
    scanf("%s",str);
        
    // print string
    printf("%s",str);
    
    return 0;
}

Input :
CodeWithSahil

Output :
CodeWithSahil
You can see in the above program that the string can also be read using a single scanf statement. Also, you might be thinking that why we have not used the ‘&’ sign with the string name ‘str’ in scanf statement! To understand this you will have to recall your knowledge of scanf.

How to Read a Line of Text in C?


Content is coming Soon Stay Tuned.....

Brand creation, trend analysis & style consulting

Our content is highly curated—in partnership with industry leaders from Google, Amazon, and Microsoft—to advance expertise in data science, ML, cloud computing, cybersecurity, and more. From our individual courses to our highly valued Nanodegree credential, each program is deeply focused on a single concept, eliminating guesswork in selecting the right course to overcome talent gaps.

Contact