Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Misusing scanf() in C
#1
Not handling newline characters left in the input buffer after using scanf().

// Incorrect
char name[50];
printf("Enter your name: ");
scanf("%s", name);
// If the user enters "John Doe", the newline character is left in the buffer

// Correct
char name[50];
printf("Enter your name: ");
scanf("%49[^\n]", name);
// The format specifier reads up to 49 characters or until a newline character
Also follow me on Youtube for videos about video games:
https://www.youtube.com/channel/UCxfkGVU...2mQ/videos
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Input and Output functions (printf and scanf) in C Qomplainerz 0 244 07-25-2023, 10:52 AM
Last Post: Qomplainerz

Forum Jump:


Users browsing this thread: 5 Guest(s)