English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, you will learn about strings in C language programming. You will learn to declare them, initialize them, and use them in various I / O(Input/Output)operation.
In C language programming, strings are character sequences that end with a null character \0. For example:
char c[] = "c string";
When the compiler encounters a character sequence \0 enclosed in double quotes, it will default to append a null character at the end.
The method to declare a string is as follows:
char s[5];
Here, we declared a5A string of characters.
You can initialize strings in many ways.
char c[] = "abcd"; char c[50] = "abcd"; char c[] = {'a', 'b', 'c', 'd', '\0'}; char c[5] = {'a', 'b', 'c', 'd', '\0'};
Let's take another example:
char c[5] = "abcde";
Here, we try to assign6Characters (the last character is '\0') allocated to char have5An array of characters. This is a wrong practice, you should never do this.
Once arrays and strings are declared in C language, they do not support assignment operators.
char c[100]; c = "C programming"; // Error! Array type cannot be allocated.
Note:Usestrcpy() functionInstead of copying strings.
You can use the scanf() function to read strings.
scanf() function reads a character sequence until it encounters a space (space, newline, tab, etc.).
#include <stdio.h> int main() { char name[20]; printf("Input name: "); scanf("%s", name); printf("Your name is %s.", name); return 0; }
Output result
Enter name: Dennis Ritchie Your name is Dennis.
Even if 'Dennis Ritchie' is entered in the above program, only 'Dennis' is in the name string. Because there is a space after Dennis.
You can use the fgets() function to read a line of string. And you can use puts() to display the string.
#include <stdio.h> int main() { char name[30]; printf("Input name: "); fgets(name, sizeof(name), stdin); //Reading a string printf("Name: "); puts(name); //Display string return 0; }
Output result
Input name: Tom Hanks Name: Tom Hanks
Here, we have used the fgets() function to read a string from the user.
fgets(name, sizeof(name), stdlin); //Reading a string
The result of sizeof(name) is30. Therefore, we can accept up to30 characters as input, which is the size of the name string.
To print a string, we used put (name);
Note:The gets() function can also accept user input. However, it has been removed from the C standard.
This is because gets() allows you to input characters of arbitrary length. Therefore, buffer overflow may occur.
A string can be passed to a function in a way similar to an array. Learn more aboutPassing an array to a functionMore information.
#include <stdio.h> void displayString(char str[]); int main() { char str[50]; printf("Enter string: "); fgets(str, sizeof(str), stdin); displayString(str); //Passing a string to a function return 0; } void displayString(char str[]) { printf("String output: "); puts(str); }
Like an array, the string name "decays" to a pointer. Therefore, you can use pointers to manipulate the elements of a string. We recommend that you check the example before you proceed.C Arrays and Pointers.
#include <stdio.h> int main(void) { char name[] = "Harry Potter"; printf("%c,"}} *name); // Output: H printf("%c,"}} *(name+1)); // Output: a printf("%c,"}} *(name+7)); // Output: o char *namePtr; namePtr = name; printf("%c,"}} *namePtr); // Output: H printf("%c,"}} *(namePtr+1)); // Output: a printf("%c,"}} *(namePtr+7)); // Output: o }