Strings
Working with strings in C. String manipulation functions from the `string.h` library.
String Input and Output in C
Explanation: String Input and Output
In C, strings are essentially arrays of characters terminated by a null character ('\0'). Handling strings involves reading them from the user (input) and displaying them back (output). Unlike some languages with built-in string types, C relies on character arrays and functions to manage strings.
Using `scanf`, `fgets`, `printf`, and `puts`
Here's how you can use these functions to read and display strings:
`scanf`
scanf
can read strings, but it has a major drawback: it stops reading at the first whitespace character (space, tab, newline). This makes it unsuitable for reading sentences or strings containing spaces.
#include <stdio.h>
int main() {
char str[50]; // Allocate space for the string (up to 49 characters + null terminator)
printf("Enter a string (using scanf): ");
scanf("%s", str); // Reads until a whitespace is encountered. DANGEROUS without size specification.
printf("You entered: %s\n", str);
return 0;
}
Important: Using scanf("%s", str)
without specifying a maximum width is dangerous because it can lead to buffer overflows if the input string is longer than the allocated size of `str`. A safer alternative is `scanf("%49s", str)` which limits the input to 49 characters. Even then, `fgets` is generally preferred.
`fgets`
fgets
is the preferred function for reading strings in C. It reads an entire line of input, including spaces, until a newline character or the specified maximum number of characters is read. The newline character is usually included in the string, but a null terminator is always appended.
#include <stdio.h>
int main() {
char str[50];
printf("Enter a string (using fgets): ");
fgets(str, sizeof(str), stdin); // Reads up to sizeof(str) - 1 characters from standard input (stdin)
printf("You entered: %s", str); // Might include the newline character at the end.
// Remove trailing newline, if present.
for(int i = 0; str[i] != '\0'; i++) {
if(str[i] == '\n') {
str[i] = '\0';
break;
}
}
printf("You entered (without newline): %s\n", str);
return 0;
}
The fgets
function takes three arguments: a pointer to the character array (string), the maximum number of characters to read (usually the size of the array), and the input stream (stdin
for standard input, i.e., the keyboard). The newline character read by `fgets` is often unwanted, and the code demonstrates how to remove it. If the input exceeds `sizeof(str) -1` characters, then only that many characters are read, and subsequent calls to fgets might read the remaining characters from the same line until the newline is encountered or end-of-file is reached.
`printf`
printf
is used for formatted output. To print a string, you use the %s
format specifier.
#include <stdio.h>
int main() {
char str[] = "Hello, world!";
printf("The string is: %s\n", str);
return 0;
}
`puts`
puts
is a simpler function specifically for outputting strings to the console. It automatically appends a newline character after the string.
#include <stdio.h>
int main() {
char str[] = "Hello, world!";
puts(str); // Outputs the string followed by a newline.
return 0;
}
puts
is generally faster than printf
for simple string output because it doesn't require any formatting.
Summary
- Use
fgets
for safe and reliable string input, especially when dealing with strings containing spaces. Always specify the size of the buffer to prevent buffer overflows. Remember to remove the newline character if needed. scanf
can be used for simple cases where whitespace is not expected, but it requires careful size control to avoid buffer overflows. Consider `fgets` as the default option.printf
provides formatted output, whileputs
offers a simpler and potentially faster way to output strings followed by a newline.