ok...i'm new to this c language and i was wondering what am i supposed to do if the user enters a string that is longer than my array size. for our assignment, we're supposed to ask for the user's name and our array size is supposed to be 20 characters. if the user enter something more than 20, we're supposed to give an error message saying that only the first 20 characters will be stored. for my code, if the user enters more than 20 characters, the additional character would be assigned to another variable. what am i supposed to do.
i'm using fscanf to accept the string but i read that it's better to use gets.
Jump to Post
fgets() is always better than scanf() because fgets() will not accept more characters than will fit in the buffer. But neither scanf() nor fgets() will meet the requirements of your assignment. You need a function that will get only one key at a time from the keyboard so that your …
Jump to Post
you're trying to put too much at the top of that loop. Break it down, pub the getchar() inside the loop and on each loop iteration check if the value of i exceeds 19 (need one byte for the string's null terminator, or make the size of the buffer 21.…
All 10 Replies
We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.