c - How do I use scanf to limit the user from entering a string no larger than the array? -


how use scanf limit user entering string no larger array?

what i've tried far is:

#include<stdio.h>  int main() { const maxstring = 5;  char arr[maxstring] = {};  printf("enter string: \n");  scanf("%*s", maxstring-1, arr);  return 0; } 

but doesn't seem work.

if want have user enter string terminated newline, it's idea use fgets instead of scanf. function fgets receives parameter length of buffer , won't overrun buffer. can check result figure out if truncation occured. consult documentation of c language implementation more details.

fgets(arr, maxstring, stdin);  

Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -