Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors

C String Input

Program to take string as input in c programming.

C
#include <stdio.h>

#define MAX_LENGTH 100

int main() {
    char input[MAX_LENGTH];

    printf("Enter a string: ");
    scanf("%s", input);

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

    return 0;
}

Explanation:

  1. The #include <stdio.h> line includes the standard input/output library, which provides functions like printf() and scanf().
  2. The #define MAX_LENGTH 100 line is a preprocessor directive that defines a constant named MAX_LENGTH with a value of 100. This constant will be used to specify the maximum length of the string.
  3. The main() function is the entry point of the program.
  4. The char input[MAX_LENGTH]; line declares an array named input with a size of MAX_LENGTH. This means the input array can hold a string of up to 100 characters (including the null terminator).
  5. The printf() function displays the message “Enter a string: ” to prompt the user for input.
  6. The scanf() function with the %s format specifier is used to read a string from the user’s input. The %s format specifier is used to read a string without spaces. The string is stored in the input array.
  7. The printf() function displays the message “You entered: ” followed by the value stored in the input array, using the %s format specifier.
  8. The return 0; statement indicates the successful completion of the main() function, and the program terminates.

C Output
Enter a string: EasyExamNotes.com
You entered: EasyExamNotes.com