Open In App

Strings in C

Last Updated : 24 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

A String in C programming is a sequence of characters terminated with a null character ‘\0’. The C String is work as an array of characters. The difference between a character array and a C string is that the string in C is terminated with a unique character ‘\0’.

string in c

Declaration

Declaring a string in C is as simple as declaring a one-dimensional array of character type. Below is the syntax for declaring a string.

C
char string_name[size];

In the above syntax string_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.

Like array, we can skip the size in the above statement:

C
char array_name[];

Initialization

We can initialize a string either by specifying the list of characters or string literal.

C
// Using character list
char str[] = {'G', 'e', 'e', 'k', 's', '\0'};

// Using string literal
char str[] = "Geeks";

Note: 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.

Accessing

We can access any character of the string by providing the position of the character, like in array. We pass the index inside square brackets [] with the name of the string.

Example:

C
//Driver Code Starts{
#include <stdio.h>

int main() {
    
    char str[] = "Geeks";
    
//Driver Code Ends }

    // Access first character
    // of string
    printf("%c", str[0]);

//Driver Code Starts{
    return 0;
}

//Driver Code Ends }

Update

Updating a character in a string is also possible. We can update any character of a string by using the index of that character.

Example:

C
//Driver Code Starts{
#include <stdio.h>

int main() {
    char str[] = "Geeks";
    
//Driver Code Ends }

    // Update the first
    // character of string
    str[0] = 'R';
    printf("%c", str[0]);

//Driver Code Starts{
    return 0;
}

//Driver Code Ends }

Output
R

String Length

To find the length of a string, you need to iterate through it until you reach the null character (‘\0’). The strlen() function from the C standard library is commonly used to get the length of a string.

Example:

C
#include <stdio.h>

int main() {
    char str[] = "Geeks";
    
    printf("%d", strlen(str));
    return 0;
}

Output
5

In this example, strlen() returns the length of the string “Geeks“, which is 5, excluding the null character.

String Input

In C, reading a string from the user can be done using different functions, and depending on the use case, one method might be chosen over another. Below, the common methods of reading strings in C will be discussed, including how to handle whitespace, and concepts will be clarified to better understand how string input works.

Using scanf()

The simplest way to read a string in C is by using the scanf() function.

Example:

C
//Driver Code Starts{
#include<stdio.h>
  
int main() {
    char str[5];
      
//Driver Code Ends }

    // Read string
    // from the user
    scanf("%s",str);
      
    // Print the string
    printf("%s",str);

//Driver Code Starts{
    return 0;
}
//Driver Code Ends }

Output

Geeks (Enter by user)
Geeks

In the above program, the string is taken as input using the scanf() function and is also printed. However, there is a limitation with the scanf() function. scanf() will stop reading input as soon as it encounters a whitespace (space, tab, or newline).

Using scanf() with a Scanset

We can also use scanf() to read strings with spaces by utilizing a scanset. A scanset in scanf() allows specifying the characters to include or exclude from the input.

Example:

C
#include <stdio.h>

int main() {
    char str[20];

    // Using scanset in scanf 
    // to read until newline
    scanf("%[^\n]s", str);

    // Printing the read string
    printf("%s", str);

    return 0;
}

Output

Geeks For Geeks (Enter by user)
Geeks For Geeks

Using fgets()

If someone wants to read a complete string, including spaces, they should use the fgets() function. Unlike scanf(), fgets() reads the entire line, including spaces, until it encounters a newline.

Example:

C
//Driver Code Starts{
#include <stdio.h>

int main() {
    char str[20];

//Driver Code Ends }

    // Reading the string 
    // (with spaces) using fgets
    fgets(str, 20, stdin);

    // Displaying the string using puts
    printf("%s", str);

//Driver Code Starts{
    return 0;
}

//Driver Code Ends }

Output

Geeks For Geeks (Enter by user)
Geeks For Geeks

Passing Strings to Function

As strings are character arrays, we can pass strings to functions in the same way we pass an array to a function. Below is a sample program to do this: 

C
//Driver Code Starts{
#include <stdio.h>

//Driver Code Ends }

void printStr(char str[]) {
    printf("%s", str);
}

int main() {
    char str[] = "GeeksforGeeks";

    // Passing string to a 
    // function
    printStr(str);

//Driver Code Starts{
    return 0;
}
//Driver Code Ends }

Output
GeeksforGeeks

Strings and Pointers in C

Similar to arrays, In C, we can create a character pointer to a string that points to the starting address of the string which is the first character of the string. The string can be accessed with the help of pointers as shown in the below example.

C
//Driver Code Starts{
#include <stdio.h>

int main(){

//Driver Code Ends }

    char str[20] = "Geeks";

    // Pointer variable which stores
    // the starting address of
    // the character array str
    char* ptr = str;

    // While loop will run till 
    // the character value is not
    // equal to null character
    while (*ptr != '') {
        printf("%c", *ptr);
        ptr++;
    }

//Driver Code Starts{
    return 0;
}
//Driver Code Ends }

Output
Geeks

memory representation of strings

Standard C Library – String.h  Functions

The C language comes bundled with <string.h> which contains some useful string-handling functions. Some of them are as follows:

FunctionDescription
strlen()Returns the length of string name.
strcpy()Copies the contents of string s2 to string s1.
strcmp()Compares the first string with the second string. If strings are the same it returns 0.
strcat()Concat s1 string with s2 string and the result is stored in the first string.
strlwr()Converts string to lowercase.
strupr()Converts string to uppercase.
strstr()Find the first occurrence of s2 in s1.


Next Article
Article Tags :

Similar Reads