C Strings
In this tutorial, we will learn about strings their declaration and initialization in C programming with the help of examples.
Strings
String is an one-dimesional array of char type terminated by a null character \0 .
Syntax
char array_name[] = "character_array elements";
Note : We have write the string in double quotes "".
Declaring String
char array_name[array_size];
Example
char name[10];
Initializing String
Different ways to initialize strings in C programming -
char name[] = "Albert";
char name[50] = "Albert";
char name[] = {'A', 'l', 'b', 'e', 'r', 't','\0'};
char name[5] = {'A', 'l', 'b', 'e', 'r', 't', '\0'};
Common Mistakes while initializing strings
char name[6] = "Albert";
Here, we are trying to assign 7 characters in which Albert has 6 character and the last or 7th character is null character \0 in array name of char type. But we have initialize the size of array as 6 but we are trying to assign 7 characters. So, this condition will provide us with an error.
Assigning Values to Strings
In C programming, Arrays and Strings do not support the assignment operator once they declared.
Example : Assigning values to strings
char name[50];
name = "Albert"; // Error
Intead of using assignments operators, we can use strcpy() function to copy the string.
Note: Use the strcpy() function is used to copy one string to another.
Read Values from the user
We can use the scanf() to function to read a string value from the user.
The scanf() function can only read the sequence of characters until it encounters a whitespace (tab, newline, etc).
Example : scanf() to read a string
#include <stdio.h>
int main() {
char name[10];
printf("Enter your name : ");
scanf("%s", &name);
printf("Your name is %s.", name);
return 0;
}
Output
Enter your name : Nikola Tesla
Your name is Nikola.
In the above example, The variable name is only printed until a whitespace encounters.
In C programming, we are provided with a format specifier %s for string.
Note : It is not necessary to use ampersand & sign while taking a string input in scanf() function.
fgets() & puts() : Read and print a line of Text
We are provided with fgets() and puts() functions to read a line of string and to display the string respecitvely.
Example : fgets() and puts()
#include <stdio.h>
int main() {
char name[20];
printf("Enter your name: ");
fgets(name, sizeof(name), stdin); // read string
printf("Hello ");
puts(name); // display string
return 0;
}
Output
Enter your name: Carl Johnson
Hello Carl Johnson
Here, we have used fgets() function to read a string from the user and puts() to printstring.
In fgets() and puts() functions it is not required to use format specifiers.
Passing Strings to Functions
#include <stdio.h>
void displayString(char name[]) {
printf("Output : ");
puts(name);
}
int main() {
char name[50];
printf("Enter something about yourself : ");
fgets(name, sizeof(name), stdin);
// Passing string to a function
displayString(name);
return 0;
}
Output
Enter something about yourself : My name is Tony Stark.
Output : My name is Tony Stark.
Pointers and Strings
#include <stdio.h>
int main(void) {
char favMovie[] = "Avengers";
printf("%c", *favMovie); // Print staring character of array
printf("%c", *(favMovie+1)); // Print first Character after array starting character
printf("%c", *(favMovie+3)); // Print third Character after array starting character
char *moviePtr;
moviePtr = favMovie;
printf("%c", *moviePtr);
printf("%c", *(moviePtr+1));
printf("%c", *(moviePtr+7));
}
Output
A
v
n
A
v
s
Here, we have used %c to print the characters from the character array favMovie[].
Next Tutorial
We hope that this tutorial helped you develop better understanding of the concept of Strings in C.
Keep Learning : )
In the next tutorial, you'll learn about C String Functions
.