getc, fgetc, getchar

Function Prototype

int getc(FILE *fp);
int fgetc(FILE *fp);
int getchar(void);

Difference between getc and fgetc

  • getc might be a macro based on the system
  • macro might run inefficiently compared with function

Warning

We should use int instead of char to receive return value of character-at-a-time I/O. This is because there is no guarantee whether char is signed or unsigned

Character-at-a-time I/O returns -1 for EOF and error, so

  • unsigned char - loops never terminates since character’s value never equals -1
  • signed char - the character value equals -1 when reading 255, so loop terminates before EOF

putc, fputc, putchar

Function Prototype

int putc(int c, FILE *fp);
int fputc(int c, FILE *fp);
int putchar(int c);
  • putc() can be macro and fputc always function