A String in C programming is a sequence of characters terminated with a null character ‘\0’. The C String is stored 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’.

C String Declaration Syntax
Declaring a string in C is as simple as declaring a one-dimensional array. Below is the basic syntax for declaring a string.
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.
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.
C String Initialization
A string in C 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 “GeeksforGeeks”.
We can initialize a C string in 4 different ways which are as follows:
1. Assigning a String Literal without Size
String literals can be assigned without size. Here, the name of the string str acts as a pointer because it is an array.
char str[] = "GeeksforGeeks";
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.
char str[50] = "GeeksforGeeks";
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.
char str[14] = { 'G','e','e','k','s','f','o','r','G','e','e','k','s','\0'};
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.
char str[] = { 'G','e','e','k','s','f','o','r','G','e','e','k','s','\0'};
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.
Let us now look at a sample program to get a clear understanding of declaring, and initializing a string in C, and also how to print a string with its size.
C String Example
C
// C program to illustrate strings
#include <stdio.h>
#include <string.h>
int main()
{
// declare and initialize string
char str[] = "Geeks";
// print string
printf("%s\n", str);
int length = 0;
length = strlen(str);
// displaying the length of string
printf("Length of string str is %d", length);
return 0;
}
OutputGeeks
Length of string str is 5
We can see in the above program that strings can be printed using normal printf statements just like we print any other variable. Unlike arrays, we do not need to print a string, character by character.
Note: The C language does not provide an inbuilt data type for strings but it has an access specifier “%s” which can be used to print and read strings directly.
Read a String Input From the User
The following example demonstrates how to take string input using scanf() function in C
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
GeeksforGeeks
Output
GeeksforGeeks
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.
We know that the ‘&’ sign is used to provide the address of the variable to the scanf() function to store the value read in memory. As str[] is a character array so using str without braces ‘[‘ and ‘]’ will give the base address of this string. That’s why we have not used ‘&’ in this case as we are already providing the base address of the string to scanf.
Now consider one more example,
C
// C Program to take input string which is separated by
// whitespaces
#include <stdio.h>
// driver code
int main()
{
char str[20];
// taking input string
scanf("%s", str);
// printing the read string
printf("%s", str);
return 0;
}
Input
Geeks for Geeks
Output
Geeks
Here, the string is read only till the whitespace is encountered.
Note: After declaration, if we want to assign some other text to the string, we have to assign it one by one or use the built-in strcpy() function because the direct assignment of the string literal to character array is only possible in declaration.
How to Read a String Separated by Whitespaces in C?
We can use multiple methods to read a string separated by spaces in C. The two of the common ones are:
- We can use the fgets() function to read a line of string and gets() to read characters from the standard input (stdin) and store them as a C string until a newline character or the End-of-file (EOF) is reached.
- We can also scanset characters inside the scanf() function
1. Example of String Input using gets()
C
// C program to illustrate
// fgets()
#include <stdio.h>
#define MAX 50
int main()
{
char str[MAX];
// MAX Size if 50 defined
fgets(str, MAX, stdin);
printf("String is: \n");
// Displaying Strings using Puts
puts(str);
return 0;
}
Input
GeeksforGeeks
Output
String is:
GeeksforGeeks
2. Example of String Input using scanset
C
// C Program to take string separated by whitespace using
// scanset characters
#include <stdio.h>
// driver code
int main()
{
char str[20];
// using scanset in scanf
scanf("%[^\n]s", str);
// printing read string
printf("%s", str);
return 0;
}
Input
Geeks for Geeks
Output
Geeks for Geeks
C String Length
The length of the string is the number of characters present in the string except for the NULL character. We can easily find the length of the string using the loop to count the characters from the start till the NULL character is found.
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
// C program to illustrate how to
// pass string to functions
#include <stdio.h>
void printStr(char str[]) { printf("String is : %s", str); }
int main()
{
// declare and initialize string
char str[] = "GeeksforGeeks";
// print string by passing string
// to a different function
printStr(str);
return 0;
}
Output:
String is : GeeksforGeeks
Note: We can’t read a string value with spaces, we can use either gets() or fgets() in the C programming language.
Strings and Pointers in C
In Arrays, the variable name points to the address of the first element.
Below is the memory representation of the string str = “Geeks”.

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
// C program to print string using Pointers
#include <stdio.h>
int main()
{
char str[20] = "GeeksforGeeks";
// 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 != '\0') {
printf("%c", *ptr);
// moving pointer to the next character.
ptr++;
}
return 0;
}
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:
Must Read:
Similar Reads
C Arrays
Array in C is one of the most used data structures in C programming. It is a simple and fast way of storing multiple values under a single name. In this article, we will study the different aspects of array in C language such as array declaration, definition, initialization, types of arrays, array s
15+ min read
Properties of Array in C
An array in C is a fixed-size homogeneous collection of elements stored at a contiguous memory location. It is a derived data type in C that can store elements of different data types such as int, char, struct, etc. It is one of the most popular data types widely used by programmers to solve differe
8 min read
Length of Array in C
The Length of an array in C refers to the maximum number of elements that an array can hold. It must be specified at the time of declaration. It is also known as the size of an array that is used to determine the memory required to store all of its elements. In C language, we don't have any pre-defi
3 min read
Multidimensional Arrays in C - 2D and 3D Arrays
Prerequisite: Arrays in C A multi-dimensional array can be defined as an array that has more than one dimension. Having more than one dimension means that it can grow in multiple directions. Some popular multidimensional arrays are 2D arrays and 3D arrays. In this article, we will learn about multid
10 min read
Initialization of Multidimensional Array in C
In C, multidimensional arrays are the arrays that contain more than one dimensions. These arrays are useful when we need to store data in a table or matrix-like structure. In this article, we will learn the different methods to initialize a multidimensional array in C. The easiest method for initial
4 min read
Jagged Array or Array of Arrays in C with Examples
Prerequisite: Arrays in CJagged array is array of arrays such that member arrays can be of different sizes, i.e., we can create a 2-D array but with a variable number of columns in each row. These type of arrays are also known as Jagged arrays. Example: arr[][] = { {0, 1, 2}, {6, 4}, {1, 7, 6, 8, 9}
3 min read
Pass Array to Functions in C
Passing an array to a function allows the function to directly access and modify the original array. In this article, we will learn how to pass arrays to functions in C. In C, arrays are always passed to function as pointers. They cannot be passed by value because of the array decay due to which, wh
3 min read
How to pass a 2D array as a parameter in C?
This post is an extension of How to dynamically allocate a 2D array in C? A one dimensional array can be easily passed as a pointer, but syntax for passing a 2D array to a function can be difficult to remember. One important thing for passing multidimensional arrays is, first array dimension does no
4 min read
How to pass an array by value in C ?
In C programming, arrays are always passed as pointers to the function. There are no direct ways to pass the array by value. However, there is trick that allows you to simulate the passing of array by value by enclosing it inside a structure and then passing that structure by value. This will also p
2 min read
Variable Length Arrays (VLAs) in C
In C, variable length arrays (VLAs) are also known as runtime-sized or variable-sized arrays. The size of such arrays is defined at run-time. Variably modified types include variable-length arrays and pointers to variable-length arrays. Variably changed types must be declared at either block scope o
2 min read
What are the data types for which it is not possible to create an array?
In C, an array is a collection of variables of the same data type, stored in contiguous memory locations. Arrays can store data of primitive types like integers, characters, and floats, as well as user-defined types like structures. However, there are certain data types for which arrays cannot be di
2 min read
Strings in C
A String in C programming is a sequence of characters terminated with a null character '\0'. The C String is stored 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'. C String Declaration SyntaxDeclar
8 min read
Array of Strings in C
In C, an array of strings is a 2D array where each row contains a sequence of characters terminated by a '\0' NULL character (strings). It is used to store multiple strings in a single array. Let's take a look at an example: [GFGTABS] C #include <stdio.h> int main() { // Creating array of stri
3 min read
C Library - <string.h>
string.h is a standard header file in the C language that contains functions for manipulating strings (arrays of characters). <string.h> header file contains some useful string functions that can be directly used in a program by invoking the #include preprocessor directive. Syntax: #include
3 min read
C String Functions
The C string functions are built-in functions that can be used for various operations and manipulations on strings. These string functions make it easier to perform tasks such as string copy, concatenation, comparison, length, etc. The <string.h> header file contains these string functions. Th
6 min read
What is the difference between single quoted and double quoted declaration of char array?
In C programming, the way we declare and initialize a char array can differ based on whether we want to use a sequence of characters and strings. They are basically same with difference only of a '\0' NULL character. Double quotes automatically include the null terminator, making the array a string
2 min read
Array C/C++ Programs
C Program to find sum of elements in a given arrayC program to find largest element in an arrayC program to multiply two matricesC/C++ Program for Given an array A[] and a number x, check for pair in A[] with sum as xC/C++ Program for Majority ElementC/C++ Program for Find the Number Occurring Odd N
6 min read
String C/C++ Programs
C program to swap two StringsC Program to Sort an array of names or stringsC Program to Check if a Given String is PalindromeC/C++ Program for Return maximum occurring character in the input stringC/C++ Program for Remove all duplicates from the input string.C/C++ Program for Print all the duplicate
3 min read