C program to ask a string input from keyboard and print it

You can read strings from the keyboard using several methods in c programming. Each method has some advantages and disadvantages. For example, you can tell your computer memory to store strings in a reserved memory using the scanf() function. As well as you can also read input with the gets() and fgets() function.

The variations of those three methods are, the function scanf() reads the first word of a given space separated sentence. On the other hand, if you use gets to read a string, then the function will read space-separated words also. fgets() also works almost the same as gets(), but the only differences is, fgets() takes a newline character automatically after every string you input.

Sample Input/Output

Input string using fgets():
Input: Welcome to progracoding!!
Output: Welcome to progracoding!!
Input: Programming world
Output: Programming world

Logic to input a string

First of all write down the basic skeleton of  C source code with the standard input-output library definition, main function definition and prepare for taking string input from the keyboard. Now;

  • Declare a character type array str with size 100 something like this char str[100] which is called especially string data type in C. This data type will store your said string.
  • Now after printing the input message to the user using printf() function, write the fgets() function for taking input from the keyboard.
  • The fgets() function consists of the following syntax, fgets(string_name, sizeof string_name, stdin). In the following case, string_name is str we used. And here, stdin means the standard input-output format.
    Thas all.
  • Finally, print the string using the simple printf() function.

C program to ask a string input and print it

#include <stdio.h>
#include <stdlib.h>
int main() {
    char str[100]; //maximum character to be hold

    /* Read multi-line string */
    printf("Enter desired string: ");
    fgets(str, sizeof str, stdin);

    /* Or use the following two syntax by swaping comment */
    // gets(str);
    // scanf("%s", str);

    printf("You entered: %s\n", str);

    return 0;
}

Happy coding!

  1. C Program to open a integer number as a output to p
  2. C Program to open a integer number as a output to p
  3. C Program to open a integer number as a output to p
  4. C Program to open a integer number as a output to p
  5. C Program to open a integer number as a output to p
  6. C Program to open a integer number as a output to p
  7. C Program to open a integer number as a output to p
  8. C Program to open a integer number as a output to p
  9. C Program to open a integer number as a output to p
  10. C Program to open a integer number as a output to p
  11. C Program to open a integer number as a output to p
  12. C Program to open a integer number as a output to p
  13. C Program to open a integer number as a output to p
  14. C Program to open a integer number as a output to p
  15. C Program to open a integer number as a output to p
  16. C Program to open a integer number as a output to p
  17. C Program to open a integer number as a output to p
  18. C Program to open a integer number as a output to p
  19. C Program to open a integer number as a output to p
  20. C Program to open a integer number as a output to p
  21. C Program to open a integer number as a output to p
  22. C Program to open a integer number as a output to p
  23. C Program to open a integer number as a output to p
  24. C Program to open a integer number as a output to p
  25. C Program to open a integer number as a output to p
  26. C Program to open a integer number as a output to p
  27. C Program to open a integer number as a output to p
  28. C Program to open a integer number as a output to p
  29. C Program to open a integer number as a output to p
  30. C Program to open a integer number as a output to p
  31. C Program to open a integer number as a output to p
  32. C Program to open a integer number as a output to p
  33. C Program to open a integer number as a output to p
  34. C Program to open a integer number as a output to p
  35. C Program to open a integer number as a output to p
  36. C Program to open a integer number as a output to p
  37. C Program to open a integer number as a output to p
  38. C Program to open a integer number as a output to p
  39. C Program to open a integer number as a output to p
  40. C Program to open a integer number as a output to p
  41. C Program to open a integer number as a output to p
  42. C Program to open a integer number as a output to p
  43. C Program to open a integer number as a output to p
  44. C Program to open a integer number as a output to p
  45. C Program to open a integer number as a output to p
  46. C Program to open a integer number as a output to p
  47. C Program to open a integer number as a output to p
  48. C Program to open a integer number as a output to p
  49. C Program to open a integer number as a output to p
  50. C Program to open a integer number as a output to p
  51. C Program to open a integer number as a output to p
  52. C Program to open a integer number as a output to p
  53. C Program to open a integer number as a output to p
  54. C Program to open a integer number as a output to p
  55. C Program to open a integer number as a output to p
  56. C Program to open a integer number as a output to p
  57. C Program to open a integer number as a output to p
  58. C Program to open a integer number as a output to p
  59. C Program to open a integer number as a output to p
  60. C Program to open a integer number as a output to p
  61. C Program to open a integer number as a output to p
  62. C Program to open a integer number as a output to p
  63. C Program to open a integer number as a output to p
  64. C Program to open a integer number as a output to p
  65. C Program to open a integer number as a output to p
  66. C Program to open a integer number as a output to p
  67. C Program to open a integer number as a output to p
  68. C Program to open a integer number as a output to p
  69. C Program to open a integer number as a output to p
  70. C Program to open a integer number as a output to p
  71. C Program to open a integer number as a output to p
  72. C Program to open a integer number as a output to p
  73. C Program to open a integer number as a output to p
  74. C Program to open a integer number as a output to p
  75. C Program to open a integer number as a output to p
  76. C Program to open a integer number as a output to p
  77. C Program to open a integer number as a output to p
  78. C Program to open a integer number as a output to p
  79. C Program to open a integer number as a output to p
  80. C Program to open a integer number as a output to p
  81. C Program to open a integer number as a output to p
  82. C Program to open a integer number as a output to p
  83. C Program to open a integer number as a output to p
  84. C Program to open a integer number as a output to p