C Programming Language Tutorial

Variables and Data Types

Input/Output

Looping and Selection Structures

Array

Functions

Preprocessing Command

Pointer

Structure

File Operations

Important Knowledge

Strings Input And Output in C Programming Language

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:

  • Print a string using printf():
#include <stdio.h>

int main() {
    char str[] = "Hello, World!";
    printf("%s\n", str);
    return 0;
}
  • Read a string using 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.

  • Read a string with spaces using 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.

  • Write a string to the standard output using 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.

  1. Reading strings from the keyboard in C language:

    • Description: Reading strings from the keyboard involves functions like scanf, fgets, or other methods.
    • Example using 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;
      }
      
  2. Using scanf and printf for string input/output in C:

    • Description: scanf and printf are commonly used for basic string input and output.
    • Example:
      #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;
      }
      
  3. fgets and fputs functions for line-based I/O in C programming:

    • Description: fgets reads a line from a file, and fputs writes a line to a file.
    • Example:
      #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;
      }
      
  4. Handling whitespace and newline characters in string input:

    • Description: scanf can be limited in handling whitespace. fgets is suitable for reading entire lines.
    • Example using 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;
      }
      
  5. String formatting options and specifiers in C:

    • Description: String formatting options include width, precision, and alignment specifiers.
    • Example:
      #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;
      }
      
  6. C code examples demonstrating string input and output:

    • Example:
      #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;
      }
      
  7. Error handling and validation with string input in C:

    • Description: Validate user input to handle errors and prevent buffer overflows.
    • Example with validation:
      #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;
      }
      
  8. Writing strings to files and standard output in C:

    • Description: Use fprintf for formatted output to a file or printf for standard output.
    • Example writing to a file:
      #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;
      }