C Programming Language Tutorial
Variables and Data Types
Input/Output
Looping and Selection Structures
Array
Functions
Preprocessing Command
Pointer
Structure
File Operations
Important Knowledge
In this tutorial, we'll explore string input and output operations in the C programming language. We'll cover the basic syntax and provide examples to demonstrate their usage.
String Input and Output Functions
Here are some of the most commonly used string input and output functions available in the stdio.h
header:
printf()
: Prints formatted data to the standard output (stdout).int printf(const char *format, ...);
scanf()
: Reads formatted input from the standard input (stdin).int scanf(const char *format, ...);
fgets()
: Reads a line from the specified stream into a string (up to a specified limit).char *fgets(char *str, int n, FILE *stream);
fputs()
: Writes a string to the specified stream.int fputs(const char *str, FILE *stream);
Including the stdio.h
Header
Before using the string input and output functions, you should include the stdio.h
header at the beginning of your C program:
#include <stdio.h>
Examples
Here are some examples demonstrating the use of string input and output functions in C:
printf()
:#include <stdio.h> int main() { char str[] = "Hello, World!"; printf("%s\n", str); return 0; }
scanf()
:#include <stdio.h> int main() { char str[50]; printf("Enter a string: "); scanf("%49s", str); printf("You entered: %s\n", str); return 0; }
Note that scanf()
stops reading input at the first whitespace character. This behavior can be problematic when reading strings with spaces.
fgets()
:#include <stdio.h> int main() { char str[50]; printf("Enter a string: "); fgets(str, sizeof(str), stdin); printf("You entered: %s", str); return 0; }
fgets()
reads up to n - 1
characters from the specified stream and appends a null character \0
at the end of the string. It also includes the newline character \n
if the input is shorter than the specified limit.
fputs()
:#include <stdio.h> int main() { char str[] = "Hello, World!"; fputs(str, stdout); fputs("\n", stdout); return 0; }
Conclusion
In this tutorial, we explored string input and output operations in the C programming language. We covered basic syntax and usage for printf()
, scanf()
, fgets()
, and fputs()
. These functions are fundamental to working with strings and interacting with users, making them essential tools for C programmers.
Reading strings from the keyboard in C language:
scanf
, fgets
, or other methods.scanf
:#include <stdio.h> int main() { char name[50]; // Reading a string using scanf printf("Enter your name: "); scanf("%s", name); printf("Hello, %s!\n", name); return 0; }
Using scanf
and printf
for string input/output in C:
scanf
and printf
are commonly used for basic string input and output.#include <stdio.h> int main() { char greeting[50]; // Using scanf and printf for string input/output printf("Enter a greeting: "); scanf("%s", greeting); printf("You entered: %s\n", greeting); return 0; }
fgets
and fputs
functions for line-based I/O in C programming:
fgets
reads a line from a file, and fputs
writes a line to a file.#include <stdio.h> int main() { char sentence[100]; // Using fgets and fputs for line-based I/O printf("Enter a sentence: "); fgets(sentence, sizeof(sentence), stdin); fputs(sentence, stdout); return 0; }
Handling whitespace and newline characters in string input:
scanf
can be limited in handling whitespace. fgets
is suitable for reading entire lines.fgets
:#include <stdio.h> int main() { char sentence[100]; // Handling whitespace with fgets printf("Enter a sentence: "); fgets(sentence, sizeof(sentence), stdin); printf("You entered: %s\n", sentence); return 0; }
String formatting options and specifiers in C:
#include <stdio.h> int main() { char name[20] = "John"; int age = 25; // String formatting options and specifiers printf("Name: %-10s\n", name); // Left-aligned, width 10 printf("Age: %03d\n", age); // Zero-padded, width 3 return 0; }
C code examples demonstrating string input and output:
#include <stdio.h> int main() { char city[30]; // String input and output examples printf("Enter your favorite city: "); fgets(city, sizeof(city), stdin); printf("Your favorite city is: %s", city); return 0; }
Error handling and validation with string input in C:
#include <stdio.h> int main() { char password[10]; // Error handling and validation with string input printf("Enter your password (max 9 characters): "); if (fgets(password, sizeof(password), stdin) != NULL) { printf("Password accepted: %s", password); } else { printf("Error reading password.\n"); } return 0; }
Writing strings to files and standard output in C:
fprintf
for formatted output to a file or printf
for standard output.#include <stdio.h> int main() { FILE *file = fopen("output.txt", "w"); if (file != NULL) { fprintf(file, "Hello, File!\n"); fclose(file); } else { printf("Error opening the file.\n"); } return 0; }Example writing to standard output:
#include <stdio.h> int main() { printf("Hello, World!\n"); return 0; }