Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: tweaked CS to null-terminate buf

...

Code Block
bgColor#FFCCCC
langc
#include <stdio.h>
#include <string.h>
 
#define BUFFER_SIZE 128
void func(void) {
  char buf[BUFSIZBUFFER_SIZE];

  if (fgets(buf, sizeof(buf), stdin) == NULL) {
    /* Handle error */
  }
  buf[strlen(buf) - 1] = '\0'; /* discard newline */
}

...

Code Block
bgColor#ccccff
langc
#include <stdio.h>
#include <string.h>
 
#define BUFFER_SIZE 128

void func(void) {
  char buf[BUFSIZBUFFER_SIZE];
  char *p;

  if (fgets(buf, sizeof(buf), stdin)) {
    buf[BUFFER_SIZE - 1] = '\0';
    p = strchr(buf, '\n');
    if (p) {
      *p = '\0';  /* discard newline */
    }
  } else {
    /* Handle error */
  }
}

...