In this tutorial, you’ll find out about strings in C programming. You’ll learn to declare them, instate them, and use them for different I/O activities with the help of examples.
In C programming, a string is an arrangement of characters terminated with a null character \0. For instance:
char c[] = "c string";
When the compiler encounters a sequence of characters enclosed in the double quotation marks, it appends a null character \0 at the end by default.
In this article, you will learn-
- 1 How to declare a string?
- 2 How to initialize strings?
- 3 Assigning Values to Strings
- 4 Read String from the user
- 5 Example 1: scanf() to read a string
- 6 How to read a line of text?
- 7 Example 2: fgets() and puts()
- 8 Passing Strings to Functions
- 9 Example 3: Passing string to a Function
- 10 Strings and Pointers
- 11 Example 4: Strings and Pointers
How to declare a string?
Here’s how you can declare strings:
char s[5];
Here, we have declared a string of 5 characters.
How to initialize strings?
You can initialize strings in a number of 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 are attempting to allot 6 characters (the last character is ‘\0’) to a char array having 5 characters. This is awful and you ought to never do this.
Assigning Values to Strings
Arrays and strings are second-class citizens in C; they do not support the assignment operator once it is declared. For example,
char c[100]; c = "C programming"; // Error! array type is not assignable.
Read String from the user
You can use the scanf() function to read a string.
The scanf() function reads the sequence of characters until it encounters whitespace (space, newline, tab, etc.).
Example 1: scanf() to read a string
#include <stdio.h> int main() { char name[20]; printf("Enter name: "); scanf("%s", name); printf("Your name is %s.", name); return 0; }
Output
Enter name: salman Your name is salman.
Even though salman was entered in the above program, only “salman” was stored in the name string. It’s because there was a space after salman.
How to read a line of text?
You can use the fgets() capacity to peruse a line of string. Also, you can use puts() to show the string.
Example 2: fgets() and puts()
#include <stdio.h> int main() { char name[30]; printf("Enter name: "); fgets(name, sizeof(name), stdin); // read string printf("Name: "); puts(name); // display string return 0; }
Enter name: salman Name: salman
Here, we have used fgets() function to read a string from the user.
fgets(name, sizeof(name), stdlin); // read string
The sizeof(name) results to 30. Hence, we can take a maximum of 30 characters as input which is the size of the name string.
To print the string, we have used puts(name);.
Note: The gets() function can also be to take input from the user. However, it is removed from the C standard.
It’s because gets() allows you to input any length of characters. Hence, there might be a buffer overflow.
Passing Strings to Functions
Strings can be passed to a function in a similar way as arrays. Learn more about passing arrays to a function.
Example 3: Passing string to a Function
#include <stdio.h> void displayString(char str[]); int main() { char str[50]; printf("Enter string: "); fgets(str, sizeof(str), stdin); displayString(str); // Passing string to a function. return 0; } void displayString(char str[]) { printf("String Output: "); puts(str); }
Strings and Pointers
Comparable like arrays, string names are “decayed” to pointers. Thus, you can use pointers to control components of the string. We prescribed you to check C Arrays and Pointers before you check this example.
Example 4: Strings and Pointers
#include <stdio.h> int main(void) { char name[] = "salman"; 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
Please feel free to give your comment if you face any difficulty here.