C (programming language)

C program to accept string from user & print it on the output

Accepting user input and displaying it back is a fundamental task in C programming. This guide explores different ways to achieve this with strings, delving into the functions and considerations involved.

1. Understanding the Challenge

The objective is to:

  • Prompt the user to enter a string.
  • Capture the user’s input using appropriate C functions.
  • Store the input string in memory.
  • Print the stored string back to the console or desired output channel.

2. Two Commonly Used Functions

Two main functions come into play for accepting and printing strings:

2.1. scanf Function:

  • Used for formatted input, allowing you to specify the format type of the expected input (e.g., “%s” for string).
  • Requires passing a pointer to the memory location where the string will be stored.
  • Useful for capturing a single word or a sentence ending with whitespace.

2.2. fgets Function:

  • Reads a line of text from the standard input stream up to the first newline character or EOF.
  • Stores the entire line, including whitespace, in the specified memory location.
  • More suitable for capturing whole sentences or phrases that might contain spaces.

3. Code Examples

Here are two code snippets demonstrating both approaches:

3.1. Using scanf:

C

#include <stdio.h>

int main() {
  char name[50]; // Array to store the user's input

  printf("Enter your name: ");
  scanf("%s", name); // Capture a single word (no spaces)

  printf("Hello, %s!\n", name); // Print the stored name

  return 0;
}

Explanation:

  • name is a character array with size 50 to hold the user’s input.
  • scanf("%s", name) reads a single word into the name array until it encounters whitespace.
  • The printf statement then greets the user using the stored name.

3.2. Using fgets:

C

#include <stdio.h>

int main() {
  char sentence[100]; // Array to store the user's input

  printf("Enter a sentence: ");
  fgets(sentence, sizeof(sentence), stdin); // Capture a line of text

  printf("You said: %s\n", sentence); // Print the stored sentence

  return 0;
}

Explanation:

  • sentence is a character array with size 100 to hold the user’s input.
  • fgets(sentence, sizeof(sentence), stdin) reads a line from the standard input (up to the first newline or EOF) into the sentence array.
  • The printf statement then displays the entire stored sentence, including any whitespace.

4. Considerations and Enhancements

  • Array size: Choose an appropriate size for the character array based on the expected length of user input.
  • Error handling: Consider validating user input to ensure it fits within the array bounds and handle potential errors like exceeding the size limit.
  • Dynamic allocation: For capturing unpredictable length strings, dynamically allocate memory using malloc instead of relying on statically defined arrays.
  • Alternative functions: Explore other functions like gets (deprecated due to security concerns) and getline (available in some C libraries) for specific use cases.

5. Conclusion

Accepting and printing user input strings form the basis for interactive C programs. Understanding the functionalities of scanf and fgets, choosing the appropriate one based on your needs, and incorporating error handling considerations will equip you to handle user input effectively in your C programs. Remember to experiment, modify the code examples, and explore further resources to deepen your understanding and tackle similar challenges confidently.

I hope this guide provides a comprehensive overview of how to accept and print strings in C!

CodeForHunger

Learn coding the easy way. Find programming guides, examples and solutions with explanations.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button