How do I handle an input thats more than the char size in c?

let's say I have this code

` #include #include char name[50]; int main(){ fgets(name,50,stdin); name[strcspn(name, "\n")] = '\0'; printf("hi %s", name); }

`

and I decide my name is "ewroiugheqripougheqpiurghperiugheqrpiughqerpuigheqrpiugherpiugheqrpiughqerpioghqe4r", my program will throw some unexpected behavior. How would I mitigate this?

2 points · 1 comments · view on lemmy.world

1 Comments

AustralianSimon@lemmy.world · 1 pts · 2y

Could do something basic like this or validate the input using a loop and if statements to apply rules.

https://stackoverflow.com/questions/7574606/left-function-in-c-sharp#7574615