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:
- The #include <stdio.h> line includes the standard input/output library, which provides functions like printf() and scanf().
- 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.
- The main() function is the entry point of the program.
- 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).
- The printf() function displays the message “Enter a string: ” to prompt the user for input.
- 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.
- The printf() function displays the message “You entered: ” followed by the value stored in the input array, using the %s format specifier.
- 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