/* * Author: D. Sheets * Description: String input example program */ #include int main() { /* Which of these are valid ways to declare strings? */ char str1[] = "string one"; char str2[10] = "string2"; char str3[20]; /* Print initial string values */ printf("1=%s\n", str1); printf("2=%s\n", str2); printf("3=%s\n", str3); /* Get string input */ /* Note the first input isn't safe, we may write outside our memory */ printf("input string 1: "); scanf("%s", &str1); printf("1=%s\n", str1); printf("2=%s\n", str2); printf("3=%s\n", str3); /* Again, we fail to ensure the user doesn't input too many characters */ printf("input string 2: "); scanf("%s", &str2); printf("1=%s\n", str1); printf("2=%s\n", str2); printf("3=%s\n", str3); /* We use the value 19 between the % and s to indicate the max # of characters */ printf("input string 3: "); scanf("%19s", &str3); printf("1=%s\n", str1); printf("2=%s\n", str2); printf("3=%s\n", str3); return(0); }